This commit is contained in:
2025-05-12 14:25:25 +02:00
parent ab2db755ef
commit 9e378ca2b7
2719 changed files with 46505 additions and 60181 deletions

View File

@@ -15,11 +15,11 @@
}
],
"require": {
"php": "8.1 - 8.3",
"php": "8.1 - 8.4",
"nette/utils": "^4.0"
},
"require-dev": {
"nette/tester": "^2.4",
"nette/tester": "^2.5.2",
"tracy/tracy": "^2.8",
"phpstan/phpstan-nette": "^1.0"
},

View File

@@ -21,7 +21,7 @@ Installation:
composer require nette/schema
```
It requires PHP version 8.1 and supports PHP up to 8.3.
It requires PHP version 8.1 and supports PHP up to 8.4.
[Support Me](https://github.com/sponsors/dg)

View File

@@ -31,12 +31,12 @@ final class Structure implements Schema
/**
* @param Schema[] $items
* @param Schema[] $shape
*/
public function __construct(array $items)
public function __construct(array $shape)
{
(function (Schema ...$items) {})(...array_values($items));
$this->items = $items;
(function (Schema ...$items) {})(...array_values($shape));
$this->items = $shape;
$this->castTo('object');
$this->required = true;
}
@@ -76,6 +76,19 @@ final class Structure implements Schema
}
public function extend(array|self $shape): self
{
$shape = $shape instanceof self ? $shape->items : $shape;
return new self(array_merge($this->items, $shape));
}
public function getShape(): array
{
return $this->items;
}
/********************* processing ****************d*g**/
@@ -117,25 +130,22 @@ final class Structure implements Schema
}
if (is_array($value) && is_array($base)) {
$index = 0;
$index = $this->otherItems === null ? null : 0;
foreach ($value as $key => $val) {
if ($key === $index) {
$base[] = $val;
$index++;
} elseif (array_key_exists($key, $base)) {
$itemSchema = $this->items[$key] ?? $this->otherItems;
$base[$key] = $itemSchema
? $itemSchema->merge($val, $base[$key])
: Helpers::merge($val, $base[$key]);
} else {
$base[$key] = $val;
$base[$key] = array_key_exists($key, $base) && ($itemSchema = $this->items[$key] ?? $this->otherItems)
? $itemSchema->merge($val, $base[$key])
: $val;
}
}
return $base;
}
return Helpers::merge($value, $base);
return $value ?? $base;
}

View File

@@ -75,7 +75,7 @@ final class Type implements Schema
/**
* @internal use arrayOf() or listOf()
*/
public function items(string|Schema $valueType = 'mixed', string|Schema $keyType = null): self
public function items(string|Schema $valueType = 'mixed', string|Schema|null $keyType = null): self
{
$this->itemsValue = $valueType instanceof Schema
? $valueType

View File

@@ -24,7 +24,6 @@ use Nette\Schema\Elements\Type;
* @method static Type float($default = null)
* @method static Type bool($default = null)
* @method static Type null()
* @method static Type array($default = [])
* @method static Type list($default = [])
* @method static Type mixed($default = null)
* @method static Type email($default = null)
@@ -56,11 +55,11 @@ final class Expect
/**
* @param Schema[] $items
* @param Schema[] $shape
*/
public static function structure(array $items): Structure
public static function structure(array $shape): Structure
{
return new Structure($items);
return new Structure($shape);
}
@@ -95,7 +94,18 @@ final class Expect
}
public static function arrayOf(string|Schema $valueType, string|Schema $keyType = null): Type
/**
* @param mixed[] $shape
*/
public static function array(?array $shape = []): Structure|Type
{
return Nette\Utils\Arrays::first($shape ?? []) instanceof Schema
? (new Structure($shape))->castTo('array')
: (new Type('array'))->default($shape);
}
public static function arrayOf(string|Schema $valueType, string|Schema|null $keyType = null): Type
{
return (new Type('array'))->items($valueType, $keyType);
}