PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / chillerlan / php-settings-container / src / SettingsContainerAbstract.php

SettingsContainerAbstract.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/chillerlan/php-settings-container/src/SettingsContainerAbstract.php

155 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class SettingsContainerAbstract
5 *
6 * @created 28.08.2018
7 * @author Smiley <[email protected]>
8 * @copyright 2018 Smiley
9 * @license MIT
10 */
11 declare (strict_types=1);
12 namespace WCPOS\Vendor\chillerlan\Settings;
13
14 use JsonException, ReflectionClass, ReflectionProperty;
15 use function array_keys, get_object_vars, json_decode, json_encode, json_last_error_msg, method_exists, property_exists;
16 use const JSON_THROW_ON_ERROR;
17 abstract class SettingsContainerAbstract implements SettingsContainerInterface
18 {
19 /**
20 * SettingsContainerAbstract constructor.
21 *
22 * @phpstan-param array<string, mixed> $properties
23 */
24 public function __construct(?iterable $properties = null)
25 {
26 if (!empty($properties)) {
27 $this->fromIterable($properties);
28 }
29 $this->construct();
30 }
31 /**
32 * calls a method with trait name as replacement constructor for each used trait
33 * (remember pre-php5 classname constructors? yeah, basically this.)
34 */
35 protected function construct() : void
36 {
37 $traits = (new ReflectionClass($this))->getTraits();
38 foreach ($traits as $trait) {
39 $method = $trait->getShortName();
40 if (method_exists($this, $method)) {
41 $this->{$method}();
42 }
43 }
44 }
45 /**
46 * @inheritdoc
47 */
48 public function __get(string $property)
49 {
50 if (!property_exists($this, $property) || $this->isPrivate($property)) {
51 return null;
52 }
53 $method = 'get_' . $property;
54 if (method_exists($this, $method)) {
55 return $this->{$method}();
56 }
57 return $this->{$property};
58 }
59 /**
60 * @inheritdoc
61 */
62 public function __set(string $property, $value) : void
63 {
64 if (!property_exists($this, $property) || $this->isPrivate($property)) {
65 return;
66 }
67 $method = 'set_' . $property;
68 if (method_exists($this, $method)) {
69 $this->{$method}($value);
70 return;
71 }
72 $this->{$property} = $value;
73 }
74 /**
75 * @inheritdoc
76 */
77 public function __isset(string $property) : bool
78 {
79 return isset($this->{$property}) && !$this->isPrivate($property);
80 }
81 /**
82 * @internal Checks if a property is private
83 */
84 protected function isPrivate(string $property) : bool
85 {
86 return (new ReflectionProperty($this, $property))->isPrivate();
87 }
88 /**
89 * @inheritdoc
90 */
91 public function __unset(string $property) : void
92 {
93 if ($this->__isset($property)) {
94 unset($this->{$property});
95 }
96 }
97 /**
98 * @inheritdoc
99 */
100 public function __toString() : string
101 {
102 return $this->toJSON();
103 }
104 /**
105 * @inheritdoc
106 */
107 public function toArray() : array
108 {
109 $properties = [];
110 foreach (array_keys(get_object_vars($this)) as $key) {
111 $properties[$key] = $this->__get($key);
112 }
113 return $properties;
114 }
115 /**
116 * @inheritdoc
117 */
118 public function fromIterable(iterable $properties) : SettingsContainerInterface
119 {
120 foreach ($properties as $key => $value) {
121 $this->__set($key, $value);
122 }
123 return $this;
124 }
125 /**
126 * @inheritdoc
127 */
128 public function toJSON(?int $jsonOptions = null) : string
129 {
130 $json = json_encode($this, $jsonOptions ?? 0);
131 if ($json === \false) {
132 throw new JsonException(json_last_error_msg());
133 }
134 return $json;
135 }
136 /**
137 * @inheritdoc
138 */
139 public function fromJSON(string $json) : SettingsContainerInterface
140 {
141 /** @phpstan-var array<string, mixed> $data */
142 $data = json_decode($json, \true, 512, JSON_THROW_ON_ERROR);
143 return $this->fromIterable($data);
144 }
145 /**
146 * @inheritdoc
147 * @return array<string, mixed>
148 */
149 #[\ReturnTypeWillChange]
150 public function jsonSerialize() : array
151 {
152 return $this->toArray();
153 }
154 }
155