| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Schema\Elements; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Nette\Schema\Context; |
| 12 |
/** |
| 13 |
* @internal |
| 14 |
*/ |
| 15 |
trait Base |
| 16 |
{ |
| 17 |
/** @var bool */ |
| 18 |
private $required = \false; |
| 19 |
/** @var mixed */ |
| 20 |
private $default; |
| 21 |
/** @var callable|null */ |
| 22 |
private $before; |
| 23 |
/** @var array[] */ |
| 24 |
private $asserts = []; |
| 25 |
/** @var string|null */ |
| 26 |
private $castTo; |
| 27 |
/** @var string|null */ |
| 28 |
private $deprecated; |
| 29 |
public function default($value) : self |
| 30 |
{ |
| 31 |
$this->default = $value; |
| 32 |
return $this; |
| 33 |
} |
| 34 |
public function required(bool $state = \true) : self |
| 35 |
{ |
| 36 |
$this->required = $state; |
| 37 |
return $this; |
| 38 |
} |
| 39 |
public function before(callable $handler) : self |
| 40 |
{ |
| 41 |
$this->before = $handler; |
| 42 |
return $this; |
| 43 |
} |
| 44 |
public function castTo(string $type) : self |
| 45 |
{ |
| 46 |
$this->castTo = $type; |
| 47 |
return $this; |
| 48 |
} |
| 49 |
public function assert(callable $handler, ?string $description = null) : self |
| 50 |
{ |
| 51 |
$this->asserts[] = [$handler, $description]; |
| 52 |
return $this; |
| 53 |
} |
| 54 |
/** Marks as deprecated */ |
| 55 |
public function deprecated(string $message = 'The item %path% is deprecated.') : self |
| 56 |
{ |
| 57 |
$this->deprecated = $message; |
| 58 |
return $this; |
| 59 |
} |
| 60 |
public function completeDefault(Context $context) |
| 61 |
{ |
| 62 |
if ($this->required) { |
| 63 |
$context->addError('The mandatory item %path% is missing.', \Packetery\Nette\Schema\Message::MISSING_ITEM); |
| 64 |
return null; |
| 65 |
} |
| 66 |
return $this->default; |
| 67 |
} |
| 68 |
public function doNormalize($value, Context $context) |
| 69 |
{ |
| 70 |
if ($this->before) { |
| 71 |
$value = ($this->before)($value); |
| 72 |
} |
| 73 |
return $value; |
| 74 |
} |
| 75 |
private function doDeprecation(Context $context) : void |
| 76 |
{ |
| 77 |
if ($this->deprecated !== null) { |
| 78 |
$context->addWarning($this->deprecated, \Packetery\Nette\Schema\Message::DEPRECATED); |
| 79 |
} |
| 80 |
} |
| 81 |
private function doValidate($value, string $expected, Context $context) : bool |
| 82 |
{ |
| 83 |
if (!Nette\Utils\Validators::is($value, $expected)) { |
| 84 |
$expected = \str_replace(['|', ':'], [' or ', ' in range '], $expected); |
| 85 |
$context->addError('The %label% %path% expects to be %expected%, %value% given.', \Packetery\Nette\Schema\Message::TYPE_MISMATCH, ['value' => $value, 'expected' => $expected]); |
| 86 |
return \false; |
| 87 |
} |
| 88 |
return \true; |
| 89 |
} |
| 90 |
private function doValidateRange($value, array $range, Context $context, string $types = '') : bool |
| 91 |
{ |
| 92 |
if (\is_array($value) || \is_string($value)) { |
| 93 |
[$length, $label] = \is_array($value) ? [\count($value), 'items'] : (\in_array('unicode', \explode('|', $types), \true) ? [\Packetery\Nette\Utils\Strings::length($value), 'characters'] : [\strlen($value), 'bytes']); |
| 94 |
if (!self::isInRange($length, $range)) { |
| 95 |
$context->addError("The length of %label% %path% expects to be in range %expected%, %length% {$label} given.", \Packetery\Nette\Schema\Message::LENGTH_OUT_OF_RANGE, ['value' => $value, 'length' => $length, 'expected' => \implode('..', $range)]); |
| 96 |
return \false; |
| 97 |
} |
| 98 |
} elseif ((\is_int($value) || \is_float($value)) && !self::isInRange($value, $range)) { |
| 99 |
$context->addError('The %label% %path% expects to be in range %expected%, %value% given.', \Packetery\Nette\Schema\Message::VALUE_OUT_OF_RANGE, ['value' => $value, 'expected' => \implode('..', $range)]); |
| 100 |
return \false; |
| 101 |
} |
| 102 |
return \true; |
| 103 |
} |
| 104 |
private function isInRange($value, array $range) : bool |
| 105 |
{ |
| 106 |
return ($range[0] === null || $value >= $range[0]) && ($range[1] === null || $value <= $range[1]); |
| 107 |
} |
| 108 |
private function doFinalize($value, Context $context) |
| 109 |
{ |
| 110 |
if ($this->castTo) { |
| 111 |
if (\Packetery\Nette\Utils\Reflection::isBuiltinType($this->castTo)) { |
| 112 |
\settype($value, $this->castTo); |
| 113 |
} else { |
| 114 |
$object = new $this->castTo(); |
| 115 |
foreach ($value as $k => $v) { |
| 116 |
$object->{$k} = $v; |
| 117 |
} |
| 118 |
$value = $object; |
| 119 |
} |
| 120 |
} |
| 121 |
foreach ($this->asserts as $i => [$handler, $description]) { |
| 122 |
if (!$handler($value)) { |
| 123 |
$expected = $description ?: (\is_string($handler) ? "{$handler}()" : "#{$i}"); |
| 124 |
$context->addError('Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.', \Packetery\Nette\Schema\Message::FAILED_ASSERTION, ['value' => $value, 'assertion' => $expected]); |
| 125 |
return; |
| 126 |
} |
| 127 |
} |
| 128 |
return $value; |
| 129 |
} |
| 130 |
} |
| 131 |
|