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-string-schema.php
98 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 a string. |
| 15 | * See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#strings |
| 16 | */ |
| 17 | class String_Schema extends Schema { |
| 18 | /** |
| 19 | * Schema definition. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | protected $schema = array( |
| 24 | 'type' => 'string', |
| 25 | ); |
| 26 | |
| 27 | /** |
| 28 | * Set minimum length of the string. |
| 29 | * |
| 30 | * @param int $value Minimum length. |
| 31 | */ |
| 32 | public function minLength( int $value ): self { |
| 33 | return $this->update_schema_property( 'minLength', $value ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Set maximum length of the string. |
| 38 | * |
| 39 | * @param int $value Maximum length. |
| 40 | */ |
| 41 | public function maxLength( int $value ): self { |
| 42 | return $this->update_schema_property( 'maxLength', $value ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Parameter $pattern is a regular expression without leading/trailing delimiters. |
| 47 | * See: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#pattern |
| 48 | * |
| 49 | * @param string $pattern Regular expression pattern. |
| 50 | */ |
| 51 | public function pattern( string $pattern ): self { |
| 52 | $this->validate_pattern( $pattern ); |
| 53 | return $this->update_schema_property( 'pattern', $pattern ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Set the format of the string according to DateTime. |
| 58 | */ |
| 59 | public function formatDateTime(): self { |
| 60 | return $this->update_schema_property( 'format', 'date-time' ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Set the format of the string according to email. |
| 65 | */ |
| 66 | public function formatEmail(): self { |
| 67 | return $this->update_schema_property( 'format', 'email' ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set the format of the string according to Hex color. |
| 72 | */ |
| 73 | public function formatHexColor(): self { |
| 74 | return $this->update_schema_property( 'format', 'hex-color' ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Set the format of the string according to IP address. |
| 79 | */ |
| 80 | public function formatIp(): self { |
| 81 | return $this->update_schema_property( 'format', 'ip' ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set the format of the string according to uri. |
| 86 | */ |
| 87 | public function formatUri(): self { |
| 88 | return $this->update_schema_property( 'format', 'uri' ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Set the format of the string according to uuid. |
| 93 | */ |
| 94 | public function formatUuid(): self { |
| 95 | return $this->update_schema_property( 'format', 'uuid' ); |
| 96 | } |
| 97 | } |
| 98 |