Schema
1 year ago
class-builder.php
1 year ago
class-schema.php
1 year ago
class-validation-exception.php
1 year ago
class-validator.php
1 year ago
class-schema.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the WooCommerce Email Editor package. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types = 1 ); |
| 9 | namespace Automattic\WooCommerce\EmailEditor\Validator; |
| 10 | |
| 11 | use function wp_json_encode; |
| 12 | use function rest_get_allowed_schema_keywords; |
| 13 | |
| 14 | /** |
| 15 | * Represents abastract schema. |
| 16 | */ |
| 17 | abstract class Schema { |
| 18 | /** |
| 19 | * Schema definition. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | protected $schema = array(); |
| 24 | |
| 25 | /** |
| 26 | * Sets the schema as nullable. |
| 27 | * |
| 28 | * @return static |
| 29 | */ |
| 30 | public function nullable() { |
| 31 | $type = $this->schema['type'] ?? array( 'null' ); |
| 32 | return $this->update_schema_property( 'type', is_array( $type ) ? $type : array( $type, 'null' ) ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Sets the schema as non-nullable. |
| 37 | * |
| 38 | * @return static |
| 39 | */ |
| 40 | public function non_nullable() { |
| 41 | $type = $this->schema['type'] ?? null; |
| 42 | return null === $type |
| 43 | ? $this->unset_schema_property( 'type' ) |
| 44 | : $this->update_schema_property( 'type', is_array( $type ) ? $type[0] : $type ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Sets the schema as required. |
| 49 | * |
| 50 | * @return static |
| 51 | */ |
| 52 | public function required() { |
| 53 | return $this->update_schema_property( 'required', true ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Unsets the required property. |
| 58 | * |
| 59 | * @return static |
| 60 | */ |
| 61 | public function optional() { |
| 62 | return $this->unset_schema_property( 'required' ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set the title of the schema. |
| 67 | * |
| 68 | * @param string $title Title. |
| 69 | * @return static |
| 70 | */ |
| 71 | public function title( string $title ) { |
| 72 | return $this->update_schema_property( 'title', $title ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Set the description of the schema. |
| 77 | * |
| 78 | * @param string $description Description. |
| 79 | * @return static |
| 80 | */ |
| 81 | public function description( string $description ) { |
| 82 | return $this->update_schema_property( 'description', $description ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Set the default value. |
| 87 | * |
| 88 | * @param mixed $default_value Default value. |
| 89 | * @return static |
| 90 | */ |
| 91 | public function default( $default_value ) { |
| 92 | return $this->update_schema_property( 'default', $default_value ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Set the field name and value. |
| 97 | * |
| 98 | * @param string $name Name of the field. |
| 99 | * @param mixed $value Value of the field. |
| 100 | * @return static |
| 101 | * @throws \Exception When the field name is reserved. |
| 102 | */ |
| 103 | public function field( string $name, $value ) { |
| 104 | if ( in_array( $name, $this->get_reserved_keywords(), true ) ) { |
| 105 | throw new \Exception( \esc_html( "Field name '$name' is reserved" ) ); |
| 106 | } |
| 107 | return $this->update_schema_property( $name, $value ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the schema as an array. |
| 112 | */ |
| 113 | public function to_array(): array { |
| 114 | return $this->schema; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the schema as a JSON string. |
| 119 | * |
| 120 | * @throws \Exception When the schema cannot be converted to JSON. |
| 121 | */ |
| 122 | public function to_string(): string { |
| 123 | $json = wp_json_encode( $this->schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION ); |
| 124 | $error = json_last_error(); |
| 125 | if ( $error || false === $json ) { |
| 126 | throw new \Exception( \esc_html( json_last_error_msg() ), 0 ); |
| 127 | } |
| 128 | return $json; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Updates the schema property. |
| 133 | * |
| 134 | * @param string $name Property name. |
| 135 | * @param mixed $value Property value. |
| 136 | * @return static |
| 137 | */ |
| 138 | protected function update_schema_property( string $name, $value ) { |
| 139 | $clone = clone $this; |
| 140 | $clone->schema[ $name ] = $value; |
| 141 | return $clone; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Unsets the schema property. |
| 146 | * |
| 147 | * @param string $name Property name. |
| 148 | * @return static |
| 149 | */ |
| 150 | protected function unset_schema_property( string $name ) { |
| 151 | $clone = clone $this; |
| 152 | unset( $clone->schema[ $name ] ); |
| 153 | return $clone; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns reserved keywords. |
| 158 | * |
| 159 | * @return string[] |
| 160 | */ |
| 161 | protected function get_reserved_keywords(): array { |
| 162 | return rest_get_allowed_schema_keywords(); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Validates the regular expression pattern. |
| 167 | * |
| 168 | * @param string $pattern Regular expression pattern. |
| 169 | * @throws \Exception When the pattern is invalid. |
| 170 | */ |
| 171 | protected function validate_pattern( string $pattern ): void { |
| 172 | $escaped = str_replace( '#', '\\#', $pattern ); |
| 173 | $regex = "#$escaped#u"; |
| 174 | if ( @preg_match( $regex, '' ) === false ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 175 | throw new \Exception( \esc_html( "Invalid regular expression '$regex'" ) ); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 |