2424 /** @param array<string, mixed> $parameters */
2525 public function __construct (
2626 public bool $ hasPassed ,
27- public mixed $ input ,
27+ public Subject $ subject ,
2828 public Rule $ rule ,
2929 public Id $ id ,
3030 public array $ parameters = [],
3131 public string $ template = Rule::TEMPLATE_STANDARD ,
3232 public bool $ hasInvertedMode = false ,
33- public Name |null $ name = null ,
3433 public Result |null $ adjacent = null ,
35- public Path |null $ path = null ,
3634 Result ...$ children ,
3735 ) {
3836 $ this ->children = $ children ;
@@ -46,7 +44,7 @@ public static function of(
4644 array $ parameters = [],
4745 string $ template = Rule::TEMPLATE_STANDARD ,
4846 ): self {
49- return new self ($ hasPassed , $ input , $ rule , Id::fromRule ($ rule ), $ parameters , $ template );
47+ return new self ($ hasPassed , new Subject ( $ input) , $ rule , Id::fromRule ($ rule ), $ parameters , $ template );
5048 }
5149
5250 /** @param array<string, mixed> $parameters */
@@ -74,12 +72,12 @@ public function asAdjacentOf(Result $result, string $prefix): Result
7472 if ($ this ->allowsAdjacent ()) {
7573 return clone ($ result , [
7674 'id ' => $ this ->id ->withPrefix ($ prefix ),
77- 'adjacent ' => $ this ->withInput ($ result ->input ),
75+ 'adjacent ' => $ this ->withSubject ($ result ->subject ),
7876 ]);
7977 }
8078
8179 return clone ($ this , [
82- 'input ' => $ result ->input ,
80+ 'subject ' => $ result ->subject ,
8381 'children ' => array_map (
8482 static fn (Result $ child ) => $ child ->asAdjacentOf ($ result , $ prefix ),
8583 $ this ->children ,
@@ -111,18 +109,8 @@ public function withIdFrom(Rule $rule): self
111109
112110 public function withPath (Path $ path ): self
113111 {
114- if ($ this ->path === $ path ) {
115- return $ this ;
116- }
117-
118- if ($ this ->path !== null ) {
119- $ this ->path ->parent = $ path ;
120-
121- return $ this ;
122- }
123-
124112 return clone ($ this , [
125- 'path ' => $ path ,
113+ 'subject ' => $ this -> subject -> withPath ( $ path) ,
126114 'adjacent ' => $ this ->adjacent ?->withPath($ path ),
127115 'children ' => array_map (
128116 static fn (Result $ child ) => $ child ->withPath ($ path ),
@@ -133,40 +121,30 @@ public function withPath(Path $path): self
133121
134122 public function withoutName (): self
135123 {
136- if ($ this ->name === null ) {
137- return $ this ;
138- }
139-
140124 return clone ($ this , [
141- 'name ' => null ,
125+ 'subject ' => $ this -> subject -> withoutName () ,
142126 'adjacent ' => $ this ->adjacent ?->withoutName(),
143127 'children ' => array_map (
144- fn (Result $ child ) => $ child ->name === $ this ->name ? $ child ->withoutName () : $ child ,
128+ fn (Result $ child ) => $ child ->subject -> name === $ this -> subject ->name ? $ child ->withoutName () : $ child ,
145129 $ this ->children ,
146130 ),
147131 ]);
148132 }
149133
150134 public function withChildren (Result ...$ children ): self
151135 {
152- if ($ this ->path === null ) {
153- return clone ($ this , ['children ' => $ children ]);
154- }
155-
156- return clone ($ this , ['children ' => array_map (fn (Result $ child ) => $ child ->withPath ($ this ->path ), $ children )]);
136+ return clone ($ this , [
137+ 'children ' => array_map (fn (Result $ child ) => $ child ->withSubject ($ this ->subject ), $ children ),
138+ ]);
157139 }
158140
159141 public function withName (Name $ name ): self
160142 {
161- if ($ this ->path !== null && $ this ->name ?->path !== $ this ->path ) {
162- $ name = $ name ->withPath ($ this ->path );
163- }
164-
165143 return clone ($ this , [
166- 'name ' => $ this ->name ?? $ name ,
144+ 'subject ' => $ this ->subject -> withName ( $ name) ,
167145 'adjacent ' => $ this ->adjacent ?->withName($ name ),
168146 'children ' => array_map (
169- static fn (Result $ child ) => $ child ->path === null ? $ child -> withName ($ child -> name ?? $ name ) : $ child ,
147+ static fn (Result $ child ) => $ child ->withName ($ name) ,
170148 $ this ->children ,
171149 ),
172150 ]);
@@ -176,7 +154,7 @@ public function withNameFrom(Rule $rule): self
176154 {
177155 if ($ rule instanceof Nameable && $ rule ->getName () !== null ) {
178156 return clone ($ this , [
179- 'name ' => $ this ->name ?? $ rule ->getName (),
157+ 'subject ' => $ this ->subject -> withName2 ( $ rule ->getName () ),
180158 'adjacent ' => $ this ->adjacent ?->withNameFrom($ rule ),
181159 'children ' => array_map (
182160 static fn (Result $ child ) => $ child ->withNameFrom ($ rule ),
@@ -188,16 +166,12 @@ public function withNameFrom(Rule $rule): self
188166 return $ this ;
189167 }
190168
191- public function withInput ( mixed $ input ): self
169+ public function withSubject ( Subject $ subject ): self
192170 {
193- $ currentInput = $ this ->input ;
194-
195171 return clone ($ this , [
196- 'input ' => $ input ,
197- 'children ' => array_map (
198- static fn (Result $ child ) => $ child ->input === $ currentInput ? $ child ->withInput ($ input ) : $ child ,
199- $ this ->children ,
200- ),
172+ 'subject ' => $ this ->subject ->withMergeFrom ($ subject ),
173+ 'adjacent ' => $ this ->adjacent ?->withSubject($ subject ),
174+ 'children ' => array_map (static fn (Result $ child ) => $ child ->withSubject ($ subject ), $ this ->children ),
201175 ]);
202176 }
203177
0 commit comments