PluginProbe
Polylang / 3.8.9
Polylang v3.8.9
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / Options / Primitive / Abstract_List.php

Abstract_List.php in Polylang 3.8.9, at src/Options/Primitive/Abstract_List.php

130 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 namespace WP_Syntex\Polylang\Options\Primitive;
7
8 use WP_Syntex\Polylang\Options\Options;
9 use WP_Syntex\Polylang\Options\Abstract_Option;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Class defining single list option, default value type to mixed.
15 *
16 * @since 3.7
17 *
18 * @phpstan-import-type SchemaType from Abstract_Option
19 */
20 abstract class Abstract_List extends Abstract_Option {
21 /**
22 * Option value.
23 *
24 * @var array
25 */
26 protected $value;
27
28 /**
29 * Prepares a value before validation.
30 * Allows to receive a string-keyed array but returns an integer-keyed array.
31 *
32 * @since 3.7
33 *
34 * @param mixed $value Value to format.
35 * @return mixed
36 */
37 protected function prepare( $value ) {
38 if ( is_array( $value ) ) {
39 return array_values( array_unique( $value ) );
40 }
41 return $value;
42 }
43
44 /**
45 * Returns the JSON schema value type for the list items.
46 * Possible values are `'string'`, `'null'`, `'number'` (float), `'integer'`, `'boolean'`,
47 * `'array'` (array with integer keys), and `'object'` (array with string keys).
48 *
49 * @since 3.7
50 * @see https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types
51 *
52 * @return string
53 *
54 * @phpstan-return SchemaType
55 */
56 protected function get_type(): string {
57 return 'string';
58 }
59
60 /**
61 * Returns the default value.
62 *
63 * @since 3.7
64 *
65 * @return array
66 */
67 protected function get_default() {
68 return array();
69 }
70
71 /**
72 * Returns the JSON schema part specific to this option.
73 *
74 * @since 3.7
75 *
76 * @return array Partial schema.
77 *
78 * @phpstan-return array{type: 'array', items: array{type: SchemaType}}
79 */
80 protected function get_data_structure(): array {
81 return array(
82 'type' => 'array',
83 'items' => array(
84 'type' => $this->get_type(),
85 ),
86 );
87 }
88
89 /**
90 * Removes an item from the list.
91 *
92 * @since 3.8
93 *
94 * @param mixed $item The item to remove.
95 * @return bool True if the value has been removed. False otherwise.
96 */
97 public function remove( $item ): bool {
98 if ( ! in_array( $item, $this->value, true ) ) {
99 return false;
100 }
101
102 $this->value = array_diff(
103 $this->value,
104 array( $item )
105 );
106
107 return true;
108 }
109
110 /**
111 * Adds an item to the list.
112 *
113 * @since 3.8
114 *
115 * @param mixed $item The item to add.
116 * @param Options $options The options instance.
117 * @return bool True if the value was added successfully. False otherwise.
118 */
119 public function add( $item, Options $options ): bool {
120 /** @var array $updated_value */
121 $updated_value = $this->get();
122 $updated_value[] = $item;
123
124 return $this->set(
125 $updated_value,
126 $options
127 );
128 }
129 }
130