1 | <?php |
||
17 | abstract class JsonConfig |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var boolean $has_changes |
||
22 | */ |
||
23 | private $has_changes = false; |
||
24 | |||
25 | /** |
||
26 | * @var string $option_name |
||
27 | */ |
||
28 | private $option_name; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * SettingsConfig constructor. |
||
33 | * |
||
34 | * @param array $defaults |
||
35 | */ |
||
36 | public function __construct(array $defaults) |
||
42 | |||
43 | |||
44 | /** |
||
45 | * @return array |
||
46 | */ |
||
47 | abstract protected function getProperties(); |
||
48 | |||
49 | |||
50 | /** |
||
51 | * converts property name to: |
||
52 | * camelCase for getters ex: show_expired => showExpired |
||
53 | * PascalCase for setters ex: show_expired => ShowExpired |
||
54 | * |
||
55 | * @param string $string |
||
56 | * @param false $camelCase |
||
57 | * @return string|string[] |
||
58 | * @since $VID:$ |
||
59 | */ |
||
60 | private function convertCase($string, $camelCase = false) |
||
68 | |||
69 | |||
70 | /** |
||
71 | * @param string $property |
||
72 | * @param bool $getter |
||
73 | * @return string |
||
74 | */ |
||
75 | private function createGetterSetter($property, $getter = true) |
||
82 | |||
83 | |||
84 | /** |
||
85 | * @param string $method |
||
86 | * @return bool |
||
87 | * @throws DomainException |
||
88 | */ |
||
89 | private function isValidMethod($method) |
||
102 | |||
103 | |||
104 | /** |
||
105 | * converts class name to option name by changing backslashes to dashes |
||
106 | */ |
||
107 | private function setOptionName() |
||
111 | |||
112 | |||
113 | /** |
||
114 | * retrieves WP option for class, decodes the data, and resigns values to properties |
||
115 | * |
||
116 | * @param array $defaults |
||
117 | */ |
||
118 | protected function load(array $defaults) |
||
132 | |||
133 | |||
134 | /** |
||
135 | * updates property value and marks changes if property value has changed |
||
136 | * |
||
137 | * @param string $property |
||
138 | * @param mixed $value |
||
139 | */ |
||
140 | protected function setProperty($property, $value) |
||
145 | |||
146 | |||
147 | /** |
||
148 | * will only toggle has_changes to true otherwise keeps existing value (ie: will never toggle to false) |
||
149 | * why? this allows this method to be fed with the result of a conditional |
||
150 | * that compares an incoming value in a setter with it's previously set value. |
||
151 | * ie: if $x = 1 and you call setX(1) then the value has not really changed. |
||
152 | * |
||
153 | * @param bool $changes |
||
154 | * @since $VID:$ |
||
155 | */ |
||
156 | protected function markChanges($changes = true) |
||
160 | |||
161 | |||
162 | /** |
||
163 | * resets $has_changes flag to false but does NOT actually reset any data |
||
164 | */ |
||
165 | public function clearChanges() |
||
169 | |||
170 | |||
171 | /** |
||
172 | * flag for marking that changes have been made to property data |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function hasChanges() |
||
180 | |||
181 | |||
182 | /** |
||
183 | * encodes all property data to JSON and saves it to a WP option |
||
184 | */ |
||
185 | public function update() |
||
207 | } |
||
208 |