class-any-of-schema.php
1 year ago
class-array-schema.php
1 year ago
class-boolean-schema.php
1 year ago
class-integer-schema.php
1 year ago
class-null-schema.php
1 year ago
class-number-schema.php
1 year ago
class-object-schema.php
1 year ago
class-one-of-schema.php
1 year ago
class-string-schema.php
1 year ago
class-object-schema.php
93 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\Schema; |
| 10 | |
| 11 | use Automattic\WooCommerce\EmailEditor\Validator\Schema; |
| 12 | |
| 13 | /** |
| 14 | * Represents a schema for an object. |
| 15 | * See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#objects |
| 16 | */ |
| 17 | class Object_Schema extends Schema { |
| 18 | /** |
| 19 | * Schema definition. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | protected $schema = array( |
| 24 | 'type' => 'object', |
| 25 | ); |
| 26 | |
| 27 | /** |
| 28 | * Set the required properties of the object. |
| 29 | * |
| 30 | * @param array<string, Schema> $properties Required properties. |
| 31 | */ |
| 32 | public function properties( array $properties ): self { |
| 33 | return $this->update_schema_property( |
| 34 | 'properties', |
| 35 | array_map( |
| 36 | function ( Schema $property ) { |
| 37 | return $property->to_array(); |
| 38 | }, |
| 39 | $properties |
| 40 | ) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set the required properties of the object. |
| 46 | * |
| 47 | * @param Schema $schema Schema of the additional properties. |
| 48 | */ |
| 49 | public function additionalProperties( Schema $schema ): self { |
| 50 | return $this->update_schema_property( 'additionalProperties', $schema->to_array() ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Disables additional properties. |
| 55 | */ |
| 56 | public function disableAdditionalProperties(): self { |
| 57 | return $this->update_schema_property( 'additionalProperties', false ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Keys of $properties are regular expressions without leading/trailing delimiters. |
| 62 | * See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#patternproperties |
| 63 | * |
| 64 | * @param array<string, Schema> $properties Regular expressions and their schemas. |
| 65 | */ |
| 66 | public function patternProperties( array $properties ): self { |
| 67 | $pattern_properties = array(); |
| 68 | foreach ( $properties as $key => $value ) { |
| 69 | $this->validate_pattern( $key ); |
| 70 | $pattern_properties[ $key ] = $value->to_array(); |
| 71 | } |
| 72 | return $this->update_schema_property( 'patternProperties', $pattern_properties ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sets the minimum number of properties in the object. |
| 77 | * |
| 78 | * @param int $value Minimum number of properties in the object. |
| 79 | */ |
| 80 | public function minProperties( int $value ): self { |
| 81 | return $this->update_schema_property( 'minProperties', $value ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Sets the maximum number of properties in the object. |
| 86 | * |
| 87 | * @param int $value Maximum number of properties in the object. |
| 88 | */ |
| 89 | public function maxProperties( int $value ): self { |
| 90 | return $this->update_schema_property( 'maxProperties', $value ); |
| 91 | } |
| 92 | } |
| 93 |