PluginProbe
Polylang / 3.8.7
Polylang v3.8.7
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_Map.php

Abstract_Map.php in Polylang 3.8.7, at src/Options/Primitive/Abstract_Map.php

106 lines 2.1 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 a map option.
15 *
16 * @since 3.8
17 */
18 abstract class Abstract_Map extends Abstract_Option {
19 /**
20 * Option value.
21 *
22 * @var array
23 */
24 protected $value;
25
26 /**
27 * Returns the JSON schema part specific to this option.
28 *
29 * @since 3.8
30 *
31 * @return array Partial schema.
32 */
33 protected function get_data_structure(): array {
34 return array_merge(
35 $this->get_inner_structure(),
36 array(
37 'type' => 'object', // Correspond to associative array in PHP, @see{https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#primitive-types}.
38 )
39 );
40 }
41
42 /**
43 * Removes a key from the map.
44 *
45 * @since 3.8
46 *
47 * @param string $key The key to remove.
48 * @return bool True if the key has been removed. False otherwise.
49 */
50 public function remove( string $key ): bool {
51 if ( ! array_key_exists( $key, $this->value ) ) {
52 return false;
53 }
54
55 $this->value[ $key ] = $this->reset_value( $key );
56
57 return true;
58 }
59
60 /**
61 * Adds an item to the map.
62 *
63 * @since 3.8
64 *
65 * @param array<string, mixed> $item The item(s) to add. Must be a key-value pair.
66 * @param Options $options The options instance.
67 * @return bool True if the value was added successfully. False otherwise.
68 */
69 public function add( $item, Options $options ): bool {
70 if ( ! is_array( $item ) ) {
71 return false;
72 }
73
74 /** @var array<string, mixed> $old_value */
75 $old_value = $this->get();
76 $updated_value = array_merge(
77 $old_value,
78 $item
79 );
80
81 return $this->set(
82 $updated_value,
83 $options
84 );
85 }
86
87 /**
88 * Returns the JSON schema part specific to the inner structure of this option.
89 *
90 * @since 3.8
91 *
92 * @return array Partial schema.
93 */
94 abstract protected function get_inner_structure(): array;
95
96 /**
97 * Returns the reset value for a key.
98 *
99 * @since 3.8
100 *
101 * @param string $key The key to reset.
102 * @return mixed The reset value.
103 */
104 abstract protected function reset_value( string $key );
105 }
106