1616use Weasel \JsonMarshaller \Exception \BadConfigurationException ;
1717use Weasel \JsonMarshaller \Exception \InvalidTypeException ;
1818use InvalidArgumentException ;
19+ use Weasel \JsonMarshaller \Exception \UnknownPropertyException ;
1920use Weasel \JsonMarshaller \Types ;
2021use Weasel \JsonMarshaller \Types \JsonType ;
2122use 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