浏览代码

Added support for string keys in ArrayOf types via acceptStringKeys() method.

phpdevcommunity 7 月之前
父节点
当前提交
c4e8c7ae83
共有 3 个文件被更改,包括 34 次插入2 次删除
  1. 1 1
      src/Schema/AbstractSchema.php
  2. 11 1
      src/Type/ArrayOfType.php
  3. 22 0
      tests/SchemaTest.php

+ 1 - 1
src/Schema/AbstractSchema.php

@@ -50,7 +50,7 @@ abstract class AbstractSchema
 
     final public function processHttpRequest(ServerRequestInterface $request): SchemaAccessor
     {
-        if ($request->getHeader('Content-Type')[0] === 'application/json') {
+        if (in_array('application/json', $request->getHeader('Content-Type'))) {
             return $this->processJsonInput($request->getBody()->getContents());
         }
         return $this->process($request->getParsedBody());

+ 11 - 1
src/Type/ArrayOfType.php

@@ -12,6 +12,7 @@ final class ArrayOfType extends AbstractType
 
     private ?int $min = null;
     private ?int $max = null;
+    private ?bool $acceptStringKeys = false;
 
     public function min(int $min): self
     {
@@ -25,6 +26,12 @@ final class ArrayOfType extends AbstractType
         return $this;
     }
 
+    public function acceptStringKeys(): self
+    {
+        $this->acceptStringKeys = true;
+        return $this;
+    }
+
     public function __construct(AbstractType $type)
     {
         $this->type = $type;
@@ -66,10 +73,13 @@ final class ArrayOfType extends AbstractType
         }
 
         foreach ($values as $key => $value) {
-            if (!is_int($key)) {
+            if ($this->acceptStringKeys === false && !is_int($key)) {
                 $result->setError('All keys must be integers');
                 return;
             }
+            if (is_string($key)) {
+                $key = trim($key);
+            }
             $definitions[$key] = $this->type;
         }
         if (empty($definitions)) {

+ 22 - 0
tests/SchemaTest.php

@@ -41,6 +41,7 @@ class SchemaTest extends TestCase
         $this->testMultipleValidationErrors();
         $this->testNestedData();
         $this->testCollection();
+        $this->testArray();
         $this->testExtend();
         $this->testExampleData();
 
@@ -376,4 +377,25 @@ class SchemaTest extends TestCase
             ]
         ]);
     }
+
+    private function testArray()
+    {
+
+        $schema = Schema::create([
+            'roles' => Type::arrayOf(Type::string()->strict())->required()->example('admin'),
+            'dependencies' => Type::arrayOf(Type::string()->strict())->acceptStringKeys()
+        ]);
+
+        $data = [
+            'roles' => ['admin'],
+            'dependencies' => [
+                'key1' => 'value1',
+                'key2' => 'value2',
+            ],
+        ];
+        $result = $schema->process($data);
+        $this->assertStrictEquals('admin', $result->get('roles.0'));
+        $this->assertStrictEquals('value1', $result->get('dependencies.key1'));
+        $this->assertStrictEquals('value2', $result->get('dependencies.key2'));
+    }
 }