Schema
11 months ago
class-builder.php
11 months ago
class-schema.php
11 months ago
class-validation-exception.php
11 months ago
class-validator.php
11 months ago
index.php
11 months ago
class-schema.php
72 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Validator; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use function wp_json_encode; |
| 6 | use function rest_get_allowed_schema_keywords; |
| 7 | abstract class Schema { |
| 8 | protected $schema = array(); |
| 9 | public function nullable() { |
| 10 | $type = $this->schema['type'] ?? array( 'null' ); |
| 11 | return $this->update_schema_property( 'type', is_array( $type ) ? $type : array( $type, 'null' ) ); |
| 12 | } |
| 13 | public function non_nullable() { |
| 14 | $type = $this->schema['type'] ?? null; |
| 15 | return null === $type |
| 16 | ? $this->unset_schema_property( 'type' ) |
| 17 | : $this->update_schema_property( 'type', is_array( $type ) ? $type[0] : $type ); |
| 18 | } |
| 19 | public function required() { |
| 20 | return $this->update_schema_property( 'required', true ); |
| 21 | } |
| 22 | public function optional() { |
| 23 | return $this->unset_schema_property( 'required' ); |
| 24 | } |
| 25 | public function title( string $title ) { |
| 26 | return $this->update_schema_property( 'title', $title ); |
| 27 | } |
| 28 | public function description( string $description ) { |
| 29 | return $this->update_schema_property( 'description', $description ); |
| 30 | } |
| 31 | public function default( $default_value ) { |
| 32 | return $this->update_schema_property( 'default', $default_value ); |
| 33 | } |
| 34 | public function field( string $name, $value ) { |
| 35 | if ( in_array( $name, $this->get_reserved_keywords(), true ) ) { |
| 36 | throw new \Exception( \esc_html( "Field name '$name' is reserved" ) ); |
| 37 | } |
| 38 | return $this->update_schema_property( $name, $value ); |
| 39 | } |
| 40 | public function to_array(): array { |
| 41 | return $this->schema; |
| 42 | } |
| 43 | public function to_string(): string { |
| 44 | $json = wp_json_encode( $this->schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION ); |
| 45 | $error = json_last_error(); |
| 46 | if ( $error || false === $json ) { |
| 47 | throw new \Exception( \esc_html( json_last_error_msg() ), 0 ); |
| 48 | } |
| 49 | return $json; |
| 50 | } |
| 51 | protected function update_schema_property( string $name, $value ) { |
| 52 | $clone = clone $this; |
| 53 | $clone->schema[ $name ] = $value; |
| 54 | return $clone; |
| 55 | } |
| 56 | protected function unset_schema_property( string $name ) { |
| 57 | $clone = clone $this; |
| 58 | unset( $clone->schema[ $name ] ); |
| 59 | return $clone; |
| 60 | } |
| 61 | protected function get_reserved_keywords(): array { |
| 62 | return rest_get_allowed_schema_keywords(); |
| 63 | } |
| 64 | protected function validate_pattern( string $pattern ): void { |
| 65 | $escaped = str_replace( '#', '\\#', $pattern ); |
| 66 | $regex = "#$escaped#u"; |
| 67 | if ( @preg_match( $regex, '' ) === false ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 68 | throw new \Exception( \esc_html( "Invalid regular expression '$regex'" ) ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 |