66
77use JetBrains \PhpStorm \Language ;
88use TypeLang \Mapper \Configuration ;
9- use TypeLang \Mapper \Context \Path \Entry \ArrayIndexEntry ;
10- use TypeLang \Mapper \Context \Path \Entry \EntryInterface ;
11- use TypeLang \Mapper \Context \Path \Entry \ObjectEntry ;
12- use TypeLang \Mapper \Context \Path \Entry \ObjectPropertyEntry ;
13- use TypeLang \Mapper \Context \Path \Entry \UnionLeafEntry ;
14- use TypeLang \Mapper \Context \Path \PathInterface ;
15- use TypeLang \Mapper \Type \Coercer \TypeCoercerInterface ;
169use TypeLang \Mapper \Type \Extractor \TypeExtractorInterface ;
1710use TypeLang \Mapper \Type \Parser \TypeParserInterface ;
1811use TypeLang \Mapper \Type \Repository \TypeRepositoryInterface ;
1912use TypeLang \Mapper \Type \TypeInterface ;
2013use TypeLang \Parser \Node \Stmt \TypeStatement ;
2114
22- /**
23- * @template-implements \IteratorAggregate<array-key, Context>
24- */
2515abstract class Context implements
2616 TypeExtractorInterface,
2717 TypeParserInterface,
28- TypeRepositoryInterface,
29- \IteratorAggregate,
30- \Countable
18+ TypeRepositoryInterface
3119{
32- protected function __construct (
33- /**
34- * Gets original (unmodified) value.
35- *
36- * Please note that the value may be changed during type manipulation
37- * (casting), for example, using {@see TypeCoercerInterface}.
38- *
39- * In this case, the `$value` in the {@see Context} remains the original
40- * value, without any mutations from type coercions.
41- */
42- public readonly mixed $ value ,
43- /**
44- * Gets data transformation direction.
45- */
46- public readonly Direction $ direction ,
20+ public function __construct (
4721 /**
4822 * Gets current configuration.
4923 *
50- * Please note that types can modify configuration settings while
51- * processing a value and pass this modified configuration down the
52- * mapping stack.
53- *
54- * In this case, the original configuration settings will be located in
55- * the {@see $original} context's field.
56- *
57- * During further processing, the configuration values will be reset to
58- * their initial configuration values.
59- *
6024 * If you need to retrieve configuration's settings, it is recommended
6125 * to use the following methods:
6226 *
63- * - {@see Context::isObjectAsArray()}
64- * - {@see Context::isStrictTypesEnabled()}
65- *
66- * To modify the configuration's settings, it is recommended to use the
67- * following methods, which guarantees correct operation:
68- *
69- * - {@see Context::withObjectAsArray()}
70- * - {@see Context::withStrictTypes()}
27+ * - {@see MappingContext::isObjectAsArray()}
28+ * - {@see MappingContext::isStrictTypesEnabled()}
7129 */
7230 public readonly Configuration $ config ,
7331 /**
@@ -79,7 +37,7 @@ protected function __construct(
7937 * You can safely use all the methods of this interface, but for ease of
8038 * use, the following methods are available to you:
8139 *
82- * - {@see Context ::getDefinitionByValue()} - returns definition string
40+ * - {@see MappingContext ::getDefinitionByValue()} - returns definition string
8341 * by the passed value.
8442 */
8543 public readonly TypeExtractorInterface $ extractor ,
@@ -93,9 +51,9 @@ protected function __construct(
9351 * You can safely use all the methods of this interface, but for ease of
9452 * use, the following methods are available to you:
9553 *
96- * - {@see Context ::getStatementByValue()} - returns statement node by
54+ * - {@see MappingContext ::getStatementByValue()} - returns statement node by
9755 * the value.
98- * - {@see Context ::getStatementByDefinition()} - returns statement node
56+ * - {@see MappingContext ::getStatementByDefinition()} - returns statement node
9957 * by the definition string.
10058 */
10159 public readonly TypeParserInterface $ parser ,
@@ -109,94 +67,16 @@ protected function __construct(
10967 * You can safely use all the methods of this interface, but for ease of
11068 * use, the following methods are available to you:
11169 *
112- * - {@see Context ::getTypeByValue()} - returns type instance by the
70+ * - {@see MappingContext ::getTypeByValue()} - returns type instance by the
11371 * passed value.
114- * - {@see Context ::getTypeByDefinition()} - returns type instance by
72+ * - {@see MappingContext ::getTypeByDefinition()} - returns type instance by
11573 * the type definition string.
116- * - {@see Context ::getTypeByStatement()} - returns type instance by
74+ * - {@see MappingContext ::getTypeByStatement()} - returns type instance by
11775 * the type statement.
11876 */
11977 public readonly TypeRepositoryInterface $ types ,
120- /**
121- * Contains a reference to the original config created during this
122- * context initialization.
123- *
124- * If there is no reference ({@see null}), then the current config in
125- * the {@see $config} context's field is the original.
126- */
127- public readonly ?Configuration $ original = null ,
12878 ) {}
12979
130- /**
131- * Creates new child context.
132- */
133- public function enter (mixed $ value , EntryInterface $ entry , ?Configuration $ config = null ): self
134- {
135- // Original configuration
136- $ original = $ this ->original ?? $ this ->config ;
137-
138- // Configuration of the current context
139- $ current = $ config ?? $ original ;
140-
141- // Do not set "previous" config in case of
142- // "current" config is original
143- if ($ current === $ original ) {
144- $ original = null ;
145- }
146-
147- return new ChildContext (
148- parent: $ this ,
149- entry: $ entry ,
150- value: $ value ,
151- direction: $ this ->direction ,
152- config: $ current ,
153- extractor: $ this ->extractor ,
154- parser: $ this ->parser ,
155- types: $ this ->types ,
156- original: $ current === $ original ? null : $ original ,
157- );
158- }
159-
160- /**
161- * Creates an "array index" child context
162- *
163- * @param array-key $index
164- */
165- public function enterIntoArrayIndex (mixed $ value , int |string $ index , ?Configuration $ config = null ): self
166- {
167- return $ this ->enter ($ value , new ArrayIndexEntry ($ index ), $ config );
168- }
169-
170- /**
171- * Creates an "object" child context
172- *
173- * @param class-string $class
174- */
175- public function enterIntoObject (mixed $ value , string $ class , ?Configuration $ config = null ): self
176- {
177- return $ this ->enter ($ value , new ObjectEntry ($ class ), $ config );
178- }
179-
180- /**
181- * Creates an "object's property" child context
182- *
183- * @param non-empty-string $name
184- */
185- public function enterIntoObjectProperty (mixed $ value , string $ name , ?Configuration $ override = null ): self
186- {
187- return $ this ->enter ($ value , new ObjectPropertyEntry ($ name ), $ override );
188- }
189-
190- /**
191- * Creates an "union leaf" child context
192- *
193- * @param int<0, max> $index
194- */
195- public function enterIntoUnionLeaf (mixed $ value , int $ index , ?Configuration $ override = null ): self
196- {
197- return $ this ->enter ($ value , new UnionLeafEntry ($ index ), $ override );
198- }
199-
20080 /**
20181 * A more convenient and correct way to get current "object as array"
20282 * configuration value.
@@ -210,25 +90,6 @@ public function isObjectAsArray(): bool
21090 return $ this ->config ->isObjectAsArray ();
21191 }
21292
213- /**
214- * Sets the value of the "object as array" configuration settings using
215- * the original configuration rules.
216- *
217- * Note that the {@see $config} property contains the **current** context
218- * configuration settings, which may differ from the original ones.
219- * Therefore, method {@see Context::withObjectAsArray()} is not equivalent
220- * to calling {@see Configuration::withObjectAsArray()}.
221- */
222- public function withObjectAsArray (?bool $ enabled ): Configuration
223- {
224- if ($ enabled === null ) {
225- return $ this ->original ?? $ this ->config ;
226- }
227-
228- return ($ this ->original ?? $ this ->config )
229- ->withObjectAsArray ($ enabled );
230- }
231-
23293 /**
23394 * A more convenient and correct way to get current "strict types"
23495 * configuration value.
@@ -242,25 +103,6 @@ public function isStrictTypesEnabled(): bool
242103 return $ this ->config ->isStrictTypesEnabled ();
243104 }
244105
245- /**
246- * Sets the value of the "strict types" configuration settings using
247- * the original configuration rules.
248- *
249- * Note that the {@see $config} property contains the **current** context
250- * configuration settings, which may differ from the original ones.
251- * Therefore, method {@see Context::withStrictTypes()} is not equivalent
252- * to calling {@see Configuration::withStrictTypes()}.
253- */
254- public function withStrictTypes (?bool $ enabled ): Configuration
255- {
256- if ($ enabled === null ) {
257- return $ this ->original ?? $ this ->config ;
258- }
259-
260- return ($ this ->original ?? $ this ->config )
261- ->withStrictTypes ($ enabled );
262- }
263-
264106 public function getDefinitionByValue (mixed $ value ): string
265107 {
266108 return $ this ->extractor ->getDefinitionByValue ($ value );
@@ -343,20 +185,4 @@ public function getTypeByStatement(TypeStatement $statement): TypeInterface
343185 statement: $ statement ,
344186 );
345187 }
346-
347- /**
348- * Returns current context path.
349- *
350- * The {@see PathInterface} contains all occurrences used in "parent"
351- * (composite) types when calling {@see enter()} method.
352- */
353- abstract public function getPath (): PathInterface ;
354-
355- /**
356- * @return int<1, max>
357- */
358- public function count (): int
359- {
360- return \max (1 , \iterator_count ($ this ));
361- }
362188}
0 commit comments