77use Symfony \Component \DependencyInjection \ServiceLocator ;
88use Torr \SimpleNormalizer \Exception \ObjectTypeNotSupportedException ;
99use Torr \SimpleNormalizer \Exception \UnsupportedTypeException ;
10+ use Torr \SimpleNormalizer \Normalizer \Validator \ValidJsonVerifier ;
1011
1112/**
1213 * The normalizer to use in your app.
1314 *
1415 * Can't be readonly, as it needs to be mock-able.
1516 *
17+ * The verifier is done on the top-level of every method (instead of at the point where the invalid values could occur
18+ * = the object normalizers), as this way we can provide a full path to the invalid element in the JSON.
19+ *
20+ * @readonly
1621 * @final
1722 */
1823class SimpleNormalizer
@@ -22,11 +27,61 @@ class SimpleNormalizer
2227 */
2328 public function __construct (
2429 private readonly ServiceLocator $ objectNormalizers ,
30+ private readonly bool $ isDebug = false ,
31+ private readonly ?ValidJsonVerifier $ validJsonVerifier = null ,
2532 ) {}
2633
2734 /**
2835 */
2936 public function normalize (mixed $ value , array $ context = []) : mixed
37+ {
38+ $ normalizedValue = $ this ->recursiveNormalize ($ value , $ context );
39+
40+ if ($ this ->isDebug )
41+ {
42+ $ this ->validJsonVerifier ?->ensureValidOnlyJsonTypes($ normalizedValue );
43+ }
44+
45+ return $ normalizedValue ;
46+ }
47+ /**
48+ */
49+ public function normalizeArray (array $ array , array $ context = []) : array
50+ {
51+ $ normalizedValue = $ this ->recursiveNormalizeArray ($ array , $ context );
52+
53+ if ($ this ->isDebug )
54+ {
55+ $ this ->validJsonVerifier ?->ensureValidOnlyJsonTypes($ normalizedValue );
56+ }
57+
58+ return $ normalizedValue ;
59+ }
60+
61+ /**
62+ * Normalizes a map of values.
63+ * Will JSON-encode to `{}` when empty.
64+ */
65+ public function normalizeMap (array $ array , array $ context = []) : array |\stdClass
66+ {
67+ // return stdClass if the array is empty here, as it will be automatically normalized to `{}` in JSON.
68+ $ normalizedValue = $ this ->recursiveNormalizeArray ($ array , $ context ) ?: new \stdClass ();
69+
70+ if ($ this ->isDebug )
71+ {
72+ $ this ->validJsonVerifier ?->ensureValidOnlyJsonTypes($ normalizedValue );
73+ }
74+
75+ return $ normalizedValue ;
76+ }
77+
78+
79+
80+ /**
81+ * The actual normalize logic, that recursively normalizes the value.
82+ * It must never call one of the public methods above and just normalizes the value.
83+ */
84+ private function recursiveNormalize (mixed $ value , array $ context = []) : mixed
3085 {
3186 if (null === $ value || \is_scalar ($ value ))
3287 {
@@ -35,7 +90,7 @@ public function normalize (mixed $value, array $context = []) : mixed
3590
3691 if (\is_array ($ value ))
3792 {
38- return $ this ->normalizeArray ($ value , $ context );
93+ return $ this ->recursiveNormalizeArray ($ value , $ context );
3994 }
4095
4196 if (\is_object ($ value ))
@@ -77,15 +132,17 @@ public function normalize (mixed $value, array $context = []) : mixed
77132 }
78133
79134 /**
135+ * The actual customized normalization logic for arrays, that recursively normalizes the value.
136+ * It must never call one of the public methods above and just normalizes the value.
80137 */
81- public function normalizeArray (array $ array , array $ context = []) : array
138+ private function recursiveNormalizeArray (array $ array , array $ context = []) : array
82139 {
83140 $ result = [];
84141 $ isList = array_is_list ($ array );
85142
86143 foreach ($ array as $ key => $ value )
87144 {
88- $ normalized = $ this ->normalize ($ value , $ context );
145+ $ normalized = $ this ->recursiveNormalize ($ value , $ context );
89146
90147 // if the array was a list and the normalized value is null, just filter it out
91148 if ($ isList && null === $ normalized )
@@ -107,14 +164,4 @@ public function normalizeArray (array $array, array $context = []) : array
107164
108165 return $ result ;
109166 }
110-
111- /**
112- * Normalizes a map of values.
113- * Will JSON-encode to `{}` when empty.
114- */
115- public function normalizeMap (array $ array , array $ context = []) : array |\stdClass
116- {
117- // return stdClass if the array is empty here, as it will be automatically normalized to `{}` in JSON.
118- return $ this ->normalizeArray ($ array , $ context ) ?: new \stdClass ();
119- }
120167}
0 commit comments