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 ,
3333 public Name |null $ name = null ,
3434 public Result |null $ adjacent = null ,
35- public Path |null $ path = null ,
3635 Result ...$ children ,
3736 ) {
3837 $ this ->children = $ children ;
@@ -46,7 +45,7 @@ public static function of(
4645 array $ parameters = [],
4746 string $ template = Rule::TEMPLATE_STANDARD ,
4847 ): self {
49- return new self ($ hasPassed , $ input , $ rule , Id::fromRule ($ rule ), $ parameters , $ template );
48+ return new self ($ hasPassed , new Subject ( $ input) , $ rule , Id::fromRule ($ rule ), $ parameters , $ template );
5049 }
5150
5251 /** @param array<string, mixed> $parameters */
@@ -74,12 +73,12 @@ public function asAdjacentOf(Result $result, string $prefix): Result
7473 if ($ this ->allowsAdjacent ()) {
7574 return clone ($ result , [
7675 'id ' => $ this ->id ->withPrefix ($ prefix ),
77- 'adjacent ' => $ this ->withInput ($ result ->input ),
76+ 'adjacent ' => $ this ->withSubject ($ result ->subject ),
7877 ]);
7978 }
8079
8180 return clone ($ this , [
82- 'input ' => $ result ->input ,
81+ 'subject ' => $ result ->subject ,
8382 'children ' => array_map (
8483 static fn (Result $ child ) => $ child ->asAdjacentOf ($ result , $ prefix ),
8584 $ this ->children ,
@@ -111,18 +110,8 @@ public function withIdFrom(Rule $rule): self
111110
112111 public function withPath (Path $ path ): self
113112 {
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-
124113 return clone ($ this , [
125- 'path ' => $ path ,
114+ 'subject ' => $ this -> subject -> withPath ( $ path) ,
126115 'adjacent ' => $ this ->adjacent ?->withPath($ path ),
127116 'children ' => array_map (
128117 static fn (Result $ child ) => $ child ->withPath ($ path ),
@@ -133,40 +122,30 @@ public function withPath(Path $path): self
133122
134123 public function withoutName (): self
135124 {
136- if ($ this ->name === null ) {
137- return $ this ;
138- }
139-
140125 return clone ($ this , [
141- 'name ' => null ,
126+ 'subject ' => $ this -> subject -> withoutName () ,
142127 'adjacent ' => $ this ->adjacent ?->withoutName(),
143128 'children ' => array_map (
144- fn (Result $ child ) => $ child ->name === $ this ->name ? $ child ->withoutName () : $ child ,
129+ fn (Result $ child ) => $ child ->subject -> name === $ this -> subject ->name ? $ child ->withoutName () : $ child ,
145130 $ this ->children ,
146131 ),
147132 ]);
148133 }
149134
150135 public function withChildren (Result ...$ children ): self
151136 {
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 )]);
137+ return clone ($ this , [
138+ 'children ' => array_map (fn (Result $ child ) => $ child ->withSubject ($ this ->subject ), $ children )
139+ ]);
157140 }
158141
159142 public function withName (Name $ name ): self
160143 {
161- if ($ this ->path !== null && $ this ->name ?->path !== $ this ->path ) {
162- $ name = $ name ->withPath ($ this ->path );
163- }
164-
165144 return clone ($ this , [
166- 'name ' => $ this ->name ?? $ name ,
145+ 'subject ' => $ this ->subject -> withName ( $ name) ,
167146 'adjacent ' => $ this ->adjacent ?->withName($ name ),
168147 'children ' => array_map (
169- static fn (Result $ child ) => $ child ->path === null ? $ child -> withName ($ child -> name ?? $ name ) : $ child ,
148+ static fn (Result $ child ) => $ child ->withName ($ name) ,
170149 $ this ->children ,
171150 ),
172151 ]);
@@ -176,7 +155,7 @@ public function withNameFrom(Rule $rule): self
176155 {
177156 if ($ rule instanceof Nameable && $ rule ->getName () !== null ) {
178157 return clone ($ this , [
179- 'name ' => $ this ->name ?? $ rule ->getName (),
158+ 'subject ' => $ this ->subject -> withName2 ( $ rule ->getName () ),
180159 'adjacent ' => $ this ->adjacent ?->withNameFrom($ rule ),
181160 'children ' => array_map (
182161 static fn (Result $ child ) => $ child ->withNameFrom ($ rule ),
@@ -188,16 +167,12 @@ public function withNameFrom(Rule $rule): self
188167 return $ this ;
189168 }
190169
191- public function withInput ( mixed $ input ): self
170+ public function withSubject ( Subject $ subject ): self
192171 {
193- $ currentInput = $ this ->input ;
194-
195172 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- ),
173+ 'subject ' => $ this ->subject ->withMergeFrom ($ subject ),
174+ 'adjacent ' => $ this ->adjacent ?->withSubject($ subject ),
175+ 'children ' => array_map (static fn (Result $ child ) => $ child ->withSubject ($ subject ), $ this ->children ),
201176 ]);
202177 }
203178
0 commit comments