44
55namespace Attributes \Validation ;
66
7+ use ArrayAccess ;
78use Attributes \Options ;
89use Attributes \Options \Exceptions \InvalidOptionException ;
910use Attributes \Validation \Exceptions \ContextPropertyException ;
@@ -51,7 +52,7 @@ public function __construct(?PropertyValidator $validator = null, bool $stopFirs
5152 /**
5253 * Validates a given data according to a given model
5354 *
54- * @param array $data - Data to validate
55+ * @param array|ArrayAccess $data - Data to validate
5556 * @param string|object $model - Model to validate against
5657 * @return object - Model populated with the validated data
5758 *
@@ -60,7 +61,7 @@ public function __construct(?PropertyValidator $validator = null, bool $stopFirs
6061 * @throws ReflectionException
6162 * @throws InvalidOptionException
6263 */
63- public function validate (array $ data , string |object $ model ): object
64+ public function validate (array | ArrayAccess $ data , string |object $ model ): object
6465 {
6566 $ currentLevel = $ this ->context ->getOptional ('internal.recursionLevel ' , 0 );
6667 $ maxRecursionLevel = $ this ->context ->getOptional ('internal.maxRecursionLevel ' , 30 );
@@ -127,7 +128,7 @@ public function validate(array $data, string|object $model): object
127128 /**
128129 * Validates a given data according to a given model
129130 *
130- * @param array $data - Data to validate
131+ * @param array|ArrayAccess $data - Data to validate
131132 * @param callable $call - Callable to validate data against
132133 * @return array - Returns an array with the necessary arguments for the callable
133134 *
@@ -136,14 +137,14 @@ public function validate(array $data, string|object $model): object
136137 * @throws ReflectionException
137138 * @throws InvalidOptionException
138139 */
139- public function validateCallable (array $ data , callable $ call ): array
140+ public function validateCallable (array | ArrayAccess $ data , callable $ call ): array
140141 {
141142 $ arguments = [];
142143 $ reflectionFunction = new ReflectionFunction ($ call );
143144 $ errorInfo = $ this ->context ->getOptional (ErrorHolder::class) ?: new ErrorHolder ($ this ->context );
144145 $ this ->context ->set (ErrorHolder::class, $ errorInfo , override: true );
145146 $ defaultAliasGenerator = $ this ->getDefaultAliasGenerator ($ reflectionFunction );
146- foreach ($ reflectionFunction ->getParameters () as $ parameter ) {
147+ foreach ($ reflectionFunction ->getParameters () as $ index => $ parameter ) {
147148 if (! $ this ->isToValidate ($ parameter )) {
148149 continue ;
149150 }
@@ -152,7 +153,7 @@ public function validateCallable(array $data, callable $call): array
152153 $ aliasName = $ this ->getAliasName ($ parameter , $ defaultAliasGenerator );
153154 $ this ->context ->push ('internal.currentProperty ' , $ propertyName );
154155
155- if (! array_key_exists ($ aliasName , $ data )) {
156+ if (! array_key_exists ($ aliasName , $ data ) && ! array_key_exists ( $ index , $ data ) ) {
156157 if (! $ parameter ->isDefaultValueAvailable ()) {
157158 try {
158159 $ errorInfo ->addError ("Missing required argument ' $ aliasName' " );
@@ -166,7 +167,7 @@ public function validateCallable(array $data, callable $call): array
166167 continue ;
167168 }
168169
169- $ propertyValue = $ data [$ aliasName ];
170+ $ propertyValue = $ data [$ index ] ?? $ data [ $ aliasName ];
170171 $ property = new Property ($ parameter , $ propertyValue );
171172 $ this ->context ->set (Property::class, $ property , override: true );
172173
0 commit comments