| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Options\Primitive; |
| 7 |
|
| 8 |
use WP_Syntex\Polylang\Options\Abstract_Option; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class defining single list option, default value type to mixed. |
| 14 |
* |
| 15 |
* @since 3.7 |
| 16 |
* |
| 17 |
* @phpstan-import-type SchemaType from Abstract_Option |
| 18 |
*/ |
| 19 |
abstract class Abstract_List extends Abstract_Option { |
| 20 |
/** |
| 21 |
* Prepares a value before validation. |
| 22 |
* Allows to receive a string-keyed array but returns an integer-keyed array. |
| 23 |
* |
| 24 |
* @since 3.7 |
| 25 |
* |
| 26 |
* @param mixed $value Value to format. |
| 27 |
* @return mixed |
| 28 |
*/ |
| 29 |
protected function prepare( $value ) { |
| 30 |
if ( is_array( $value ) ) { |
| 31 |
return array_values( array_unique( $value ) ); |
| 32 |
} |
| 33 |
return $value; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns the JSON schema value type for the list items. |
| 38 |
* Possible values are `'string'`, `'null'`, `'number'` (float), `'integer'`, `'boolean'`, |
| 39 |
* `'array'` (array with integer keys), and `'object'` (array with string keys). |
| 40 |
* |
| 41 |
* @since 3.7 |
| 42 |
* @see https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types |
| 43 |
* |
| 44 |
* @return string |
| 45 |
* |
| 46 |
* @phpstan-return SchemaType |
| 47 |
*/ |
| 48 |
protected function get_type(): string { |
| 49 |
return 'string'; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns the default value. |
| 54 |
* |
| 55 |
* @since 3.7 |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
protected function get_default() { |
| 60 |
return array(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the JSON schema part specific to this option. |
| 65 |
* |
| 66 |
* @since 3.7 |
| 67 |
* |
| 68 |
* @return array Partial schema. |
| 69 |
* |
| 70 |
* @phpstan-return array{type: 'array', items: array{type: SchemaType}} |
| 71 |
*/ |
| 72 |
protected function get_data_structure(): array { |
| 73 |
return array( |
| 74 |
'type' => 'array', |
| 75 |
'items' => array( |
| 76 |
'type' => $this->get_type(), |
| 77 |
), |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|