|
4 | 4 |
|
5 | 5 | abstract class BaseHandler |
6 | 6 | { |
7 | | - /** |
8 | | - * Returns a single value from the handler, if stored. |
9 | | - * |
10 | | - * @param string $class |
11 | | - * @param string $key |
12 | | - * |
13 | | - * @return mixed |
14 | | - */ |
15 | | - abstract public function get(string $class, string $key); |
| 7 | + /** |
| 8 | + * Returns a single value from the handler, if stored. |
| 9 | + * |
| 10 | + * @param string $class |
| 11 | + * @param string $key |
| 12 | + * |
| 13 | + * @return mixed |
| 14 | + */ |
| 15 | + abstract public function get(string $class, string $key); |
16 | 16 |
|
17 | | - /** |
18 | | - * If the Handler supports saving values, it |
19 | | - * MUST override this method to provide that functionality. |
20 | | - * Not all Handlers will support writing values. |
21 | | - * |
22 | | - * @param string $key |
23 | | - * @param mixed $value |
24 | | - * |
25 | | - * @return mixed |
26 | | - */ |
27 | | - public function set(string $class, string $key, $value = null) |
28 | | - { |
29 | | - throw new \RuntimeException('Set method not implemented for current Settings handler.'); |
30 | | - } |
| 17 | + /** |
| 18 | + * If the Handler supports saving values, it |
| 19 | + * MUST override this method to provide that functionality. |
| 20 | + * Not all Handlers will support writing values. |
| 21 | + * |
| 22 | + * @param string $key |
| 23 | + * @param mixed $value |
| 24 | + * |
| 25 | + * @return mixed |
| 26 | + */ |
| 27 | + public function set(string $class, string $key, $value = null) |
| 28 | + { |
| 29 | + throw new \RuntimeException('Set method not implemented for current Settings handler.'); |
| 30 | + } |
31 | 31 |
|
32 | | - /** |
33 | | - * Takes care of converting some item types so they can be safely |
34 | | - * stored and re-hydrated into the config files. |
35 | | - * |
36 | | - * @param mixed $value |
37 | | - * |
38 | | - * @return string|mixed |
39 | | - */ |
40 | | - protected function prepareValue($value) |
41 | | - { |
42 | | - if (is_bool($value)) |
43 | | - { |
44 | | - return (int)$value; |
45 | | - } |
| 32 | + /** |
| 33 | + * Takes care of converting some item types so they can be safely |
| 34 | + * stored and re-hydrated into the config files. |
| 35 | + * |
| 36 | + * @param mixed $value |
| 37 | + * |
| 38 | + * @return string|mixed |
| 39 | + */ |
| 40 | + protected function prepareValue($value) |
| 41 | + { |
| 42 | + if (is_bool($value)) { |
| 43 | + return (int)$value; |
| 44 | + } |
46 | 45 |
|
47 | | - if (is_array($value) || is_object($value)) |
48 | | - { |
49 | | - return serialize($value); |
50 | | - } |
| 46 | + if (is_array($value) || is_object($value)) { |
| 47 | + return serialize($value); |
| 48 | + } |
51 | 49 |
|
52 | | - return $value; |
53 | | - } |
| 50 | + return $value; |
| 51 | + } |
54 | 52 |
|
55 | | - /** |
56 | | - * Handles some special case conversions that |
57 | | - * data might have been saved as, such as booleans |
58 | | - * and serialized data. |
59 | | - * |
60 | | - * @param mixed $value |
61 | | - * |
62 | | - * @return boolean|mixed |
63 | | - */ |
64 | | - protected function parseValue($value, string $type) |
65 | | - { |
66 | | - // Serialized? |
67 | | - if ($this->isSerialized($value)) |
68 | | - { |
69 | | - $value = unserialize($value); |
70 | | - } |
| 53 | + /** |
| 54 | + * Handles some special case conversions that |
| 55 | + * data might have been saved as, such as booleans |
| 56 | + * and serialized data. |
| 57 | + * |
| 58 | + * @param mixed $value |
| 59 | + * |
| 60 | + * @return boolean|mixed |
| 61 | + */ |
| 62 | + protected function parseValue($value, string $type) |
| 63 | + { |
| 64 | + // Serialized? |
| 65 | + if ($this->isSerialized($value)) { |
| 66 | + $value = unserialize($value); |
| 67 | + } |
71 | 68 |
|
72 | | - settype($value, $type); |
| 69 | + settype($value, $type); |
73 | 70 |
|
74 | | - return $value; |
75 | | - } |
| 71 | + return $value; |
| 72 | + } |
76 | 73 |
|
77 | | - /** |
78 | | - * Checks to see if an object is serialized and correctly formatted. |
79 | | - * |
80 | | - * Taken from Wordpress core functions. |
81 | | - * |
82 | | - * @param mixed $data |
83 | | - * @param boolean $strict Whether to be strict about the end of the string. |
84 | | - * |
85 | | - * @return boolean |
86 | | - */ |
87 | | - protected function isSerialized($data, $strict = true): bool |
88 | | - { |
89 | | - // If it isn't a string, it isn't serialized. |
90 | | - if (! is_string($data)) |
91 | | - { |
92 | | - return false; |
93 | | - } |
94 | | - $data = trim($data); |
95 | | - if ('N;' === $data) |
96 | | - { |
97 | | - return true; |
98 | | - } |
99 | | - if (strlen($data) < 4) |
100 | | - { |
101 | | - return false; |
102 | | - } |
103 | | - if (':' !== $data[1]) |
104 | | - { |
105 | | - return false; |
106 | | - } |
107 | | - if ($strict) |
108 | | - { |
109 | | - $lastc = substr($data, -1); |
110 | | - if (';' !== $lastc && '}' !== $lastc) |
111 | | - { |
112 | | - return false; |
113 | | - } |
114 | | - } |
115 | | - else |
116 | | - { |
117 | | - $semicolon = strpos($data, ';'); |
118 | | - $brace = strpos($data, '}'); |
119 | | - // Either ; or } must exist. |
120 | | - if (false === $semicolon && false === $brace) |
121 | | - { |
122 | | - return false; |
123 | | - } |
124 | | - // But neither must be in the first X characters. |
125 | | - if (false !== $semicolon && $semicolon < 3) |
126 | | - { |
127 | | - return false; |
128 | | - } |
129 | | - if (false !== $brace && $brace < 4) |
130 | | - { |
131 | | - return false; |
132 | | - } |
133 | | - } |
134 | | - $token = $data[0]; |
135 | | - switch ($token) { |
136 | | - case 's': |
137 | | - if ($strict) |
138 | | - { |
139 | | - if ('"' !== substr($data, -2, 1)) |
140 | | - { |
141 | | - return false; |
142 | | - } |
143 | | - } |
144 | | - elseif (false === strpos($data, '"')) |
145 | | - { |
146 | | - return false; |
147 | | - } |
148 | | - // Or else fall through. |
149 | | - case 'a': |
150 | | - case 'O': |
151 | | - return (bool) preg_match("/^{$token}:[0-9]+:/s", $data); |
152 | | - case 'b': |
153 | | - case 'i': |
154 | | - case 'd': |
155 | | - $end = $strict ? '$' : ''; |
156 | | - return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data); |
157 | | - } |
158 | | - return false; |
159 | | - } |
| 74 | + /** |
| 75 | + * Checks to see if an object is serialized and correctly formatted. |
| 76 | + * |
| 77 | + * Taken from Wordpress core functions. |
| 78 | + * |
| 79 | + * @param mixed $data |
| 80 | + * @param boolean $strict Whether to be strict about the end of the string. |
| 81 | + * |
| 82 | + * @return boolean |
| 83 | + */ |
| 84 | + protected function isSerialized($data, $strict = true): bool |
| 85 | + { |
| 86 | + // If it isn't a string, it isn't serialized. |
| 87 | + if (! is_string($data)) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + $data = trim($data); |
| 91 | + if ('N;' === $data) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + if (strlen($data) < 4) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + if (':' !== $data[1]) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + if ($strict) { |
| 101 | + $lastc = substr($data, -1); |
| 102 | + if (';' !== $lastc && '}' !== $lastc) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } else { |
| 106 | + $semicolon = strpos($data, ';'); |
| 107 | + $brace = strpos($data, '}'); |
| 108 | + // Either ; or } must exist. |
| 109 | + if (false === $semicolon && false === $brace) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + // But neither must be in the first X characters. |
| 113 | + if (false !== $semicolon && $semicolon < 3) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + if (false !== $brace && $brace < 4) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + } |
| 120 | + $token = $data[0]; |
| 121 | + switch ($token) { |
| 122 | + case 's': |
| 123 | + if ($strict) { |
| 124 | + if ('"' !== substr($data, -2, 1)) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + } elseif (false === strpos($data, '"')) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + // Or else fall through. |
| 131 | + case 'a': |
| 132 | + case 'O': |
| 133 | + return (bool) preg_match("/^{$token}:[0-9]+:/s", $data); |
| 134 | + case 'b': |
| 135 | + case 'i': |
| 136 | + case 'd': |
| 137 | + $end = $strict ? '$' : ''; |
| 138 | + return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data); |
| 139 | + } |
| 140 | + return false; |
| 141 | + } |
160 | 142 | } |
0 commit comments