Schema
3 years ago
Builder.php
1 year ago
Schema.php
8 months ago
ValidationException.php
4 years ago
Validator.php
3 years ago
index.php
3 years ago
Schema.php
102 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Validator; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\InvalidStateException; |
| 9 | |
| 10 | use function json_encode; |
| 11 | use function rest_get_allowed_schema_keywords; |
| 12 | |
| 13 | abstract class Schema { |
| 14 | protected $schema = []; |
| 15 | |
| 16 | /** @return static */ |
| 17 | public function nullable() { |
| 18 | $type = $this->schema['type'] ?? ['null']; |
| 19 | return $this->updateSchemaProperty('type', is_array($type) ? $type : [$type, 'null']); |
| 20 | } |
| 21 | |
| 22 | /** @return static */ |
| 23 | public function nonNullable() { |
| 24 | $type = $this->schema['type'] ?? null; |
| 25 | return $type === null |
| 26 | ? $this->unsetSchemaProperty('type') |
| 27 | : $this->updateSchemaProperty('type', is_array($type) ? $type[0] : $type); |
| 28 | } |
| 29 | |
| 30 | /** @return static */ |
| 31 | public function required() { |
| 32 | return $this->updateSchemaProperty('required', true); |
| 33 | } |
| 34 | |
| 35 | /** @return static */ |
| 36 | public function optional() { |
| 37 | return $this->unsetSchemaProperty('required'); |
| 38 | } |
| 39 | |
| 40 | /** @return static */ |
| 41 | public function title(string $title) { |
| 42 | return $this->updateSchemaProperty('title', $title); |
| 43 | } |
| 44 | |
| 45 | /** @return static */ |
| 46 | public function description(string $description) { |
| 47 | return $this->updateSchemaProperty('description', $description); |
| 48 | } |
| 49 | |
| 50 | /** @return static */ |
| 51 | public function default($default) { |
| 52 | return $this->updateSchemaProperty('default', $default); |
| 53 | } |
| 54 | |
| 55 | /** @return static */ |
| 56 | public function field(string $name, $value) { |
| 57 | if (in_array($name, $this->getReservedKeywords(), true)) { |
| 58 | throw new InvalidStateException("Field name '$name' is reserved"); |
| 59 | } |
| 60 | return $this->updateSchemaProperty($name, $value); |
| 61 | } |
| 62 | |
| 63 | public function toArray(): array { |
| 64 | return $this->schema; |
| 65 | } |
| 66 | |
| 67 | public function toString(): string { |
| 68 | $json = json_encode($this->schema, JSON_HEX_TAG | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION); |
| 69 | $error = json_last_error(); |
| 70 | if ($error || $json === false) { |
| 71 | throw new InvalidStateException(json_last_error_msg(), (string)$error); |
| 72 | } |
| 73 | return $json; |
| 74 | } |
| 75 | |
| 76 | /** @return static */ |
| 77 | protected function updateSchemaProperty(string $name, $value) { |
| 78 | $clone = clone $this; |
| 79 | $clone->schema[$name] = $value; |
| 80 | return $clone; |
| 81 | } |
| 82 | |
| 83 | /** @return static */ |
| 84 | protected function unsetSchemaProperty(string $name) { |
| 85 | $clone = clone $this; |
| 86 | unset($clone->schema[$name]); |
| 87 | return $clone; |
| 88 | } |
| 89 | |
| 90 | protected function getReservedKeywords(): array { |
| 91 | return rest_get_allowed_schema_keywords(); |
| 92 | } |
| 93 | |
| 94 | protected function validatePattern(string $pattern): void { |
| 95 | $escaped = str_replace('#', '\\#', $pattern); |
| 96 | $regex = "#$escaped#u"; |
| 97 | if (@preg_match($regex, '') === false) { |
| 98 | throw new InvalidStateException("Invalid regular expression '$regex'"); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 |