Skip to content

Commit 4b08468

Browse files
committed
Merge pull request #60 from JonathanO/master
Made configuration more like Jackson.
2 parents 26a43a8 + bda0253 commit 4b08468

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* @author Jonathan Oddy <[email protected]>
4+
* @copyright Copyright (c) 2012, Moo Print Ltd.
5+
* @license ISC
6+
*/
7+
namespace Weasel\JsonMarshaller\Config\Deserialization;
8+
9+
/**
10+
* Features for config general mapper behaviour.
11+
* These are the keys to be passed to the mapper configure() method.
12+
*/
13+
interface Feature {
14+
/**
15+
* bool. When true, throw an exception (UnknownPropertyException) on encountering an unknown property in the JSON and
16+
* there's no AnySetter configured (or ignoreUnknown isn't set.) Default: false.
17+
*/
18+
const FAIL_ON_UNKNOWN_PROPERTIES = "deserialization::fail_on_unknown";
19+
/**
20+
* bool. When true trigger an E_USER_WARNING on encountering an unknown property in the JSON and there's no AnySetter
21+
* configured (or ignoreUnknown isn't set.) Default: true.
22+
*/
23+
const WARN_ON_UNKNOWN_PROPERTIES = "deserialization::warn_on_unknown";
24+
25+
/**
26+
* bool. When true apply strict type checking. JSON types encountered are expected to match the PHP types we're
27+
* going to deserialize to. If they don't, throw an exception. Default true.
28+
*/
29+
const STRICT_TYPES = "deserialization::strict_types";
30+
31+
}

lib/Weasel/JsonMarshaller/Exception/BadConfigurationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Weasel\JsonMarshaller\Exception;
99

10-
class BadConfigurationException extends \Exception
10+
class BadConfigurationException extends JsonMarshallerException
1111
{
1212

1313
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Weasel\JsonMarshaller\Exception;
4+
5+
6+
class UnknownPropertyException extends JsonMarshallerException {
7+
8+
}

lib/Weasel/JsonMarshaller/JsonMapper.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Weasel\JsonMarshaller\Exception\BadConfigurationException;
1717
use Weasel\JsonMarshaller\Exception\InvalidTypeException;
1818
use InvalidArgumentException;
19+
use Weasel\JsonMarshaller\Exception\UnknownPropertyException;
1920
use Weasel\JsonMarshaller\Types;
2021
use Weasel\JsonMarshaller\Types\JsonType;
2122
use Weasel\JsonMarshaller\Types\OldTypeWrapper;
@@ -36,9 +37,9 @@ class JsonMapper
3637
protected $typeHandlers = array();
3738

3839
/**
39-
* @var bool Should we use strict mode unless told otherwise.
40+
* @var array Configuration parameters.
4041
*/
41-
protected $strict = true;
42+
protected $mapperConfig = array();
4243

4344
/**
4445
* Setup a JsonMapper from a config provider
@@ -49,7 +50,28 @@ public function __construct(JsonConfigProvider $configProvider, $strict = true)
4950
{
5051
$this->configProvider = $configProvider;
5152
$this->_registerBuiltInTypes();
52-
$this->strict = $strict;
53+
if (isset($strict)) {
54+
// Legacy way to configure strict mode.
55+
$this->configure(Config\Deserialization\Feature::STRICT_TYPES, $strict);
56+
}
57+
}
58+
59+
/**
60+
* @param string $key One of the Config\*\Feature:: constants.
61+
* @param mixed $value Appropriate value for the config option. See their documentation.
62+
*/
63+
public function configure($key, $value)
64+
{
65+
$this->mapperConfig[$key] = $value;
66+
}
67+
68+
protected function getConfigOption($key, $default, $permitsNull = false)
69+
{
70+
if (($permitsNull && array_key_exists($key, $this->mapperConfig)) || isset($this->mapperConfig[$key])) {
71+
return $this->mapperConfig[$key];
72+
} else {
73+
return $default;
74+
}
5375
}
5476

5577
/**
@@ -82,7 +104,7 @@ public function readString($string, $type, $strict = null)
82104
throw new InvalidArgumentException("Unable to decode JSON: $string");
83105
}
84106
if ($strict === null) {
85-
$strict = $this->strict;
107+
$strict = $this->getConfigOption(Config\Deserialization\Feature::STRICT_TYPES, true);
86108
}
87109
return $this->_decodeValue($decoded, $this->_parseTypeString($type), $strict);
88110
}
@@ -475,7 +497,12 @@ protected function _decodeClass($array, $class, $strict)
475497
$object->$method($key, $value);
476498
} elseif (!$deconfig->ignoreUnknown) {
477499
if (!isset($canIgnoreProperties[$key])) {
478-
trigger_error("Unknown property: $key", E_USER_WARNING);
500+
if ($this->getConfigOption(Config\Deserialization\Feature::WARN_ON_UNKNOWN_PROPERTIES, true)) {
501+
trigger_error("Unknown property $key on $class", E_USER_WARNING);
502+
}
503+
if ($this->getConfigOption(Config\Deserialization\Feature::FAIL_ON_UNKNOWN_PROPERTIES, false)) {
504+
throw new UnknownPropertyException("Unknown property $key on $class");
505+
}
479506
}
480507
}
481508
}

0 commit comments

Comments
 (0)