-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringObjects.php
More file actions
216 lines (186 loc) · 4.92 KB
/
StringObjects.php
File metadata and controls
216 lines (186 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* This file is part of the StrObj package.
*
* (c) Uğur Biçer <contact@codeplus.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package StrObj
* @version GIT: <git_id>
* @link https://github.com/uuur86/strobj
*/
declare(strict_types=1);
namespace StrObj;
use Exception;
use StrObj\Data\DataFilters;
use StrObj\Data\DataObject;
use StrObj\Data\Validation;
use function is_string;
/**
* StringObjects class
*/
class StringObjects
{
use Helpers\Adapters;
/**
* The main data object
*
* @var DataObject
*/
private DataObject $obj;
/**
* Validation object
*
* @var Validation
*/
private Validation $validation;
/**
* Middleware object
*
* @var Middleware
*/
private Middleware $middleware;
/**
* Filters object
*
* @var DataFilters
*/
private DataFilters $filters;
/**
* Constructor
*
* @param object $obj The object to use
* @param array $options Options
*/
final public function __construct(object $obj, array $options = [])
{
$this->obj = new DataObject($obj);
if (isset($options['middleware'])) {
$this->middleware = new Middleware($options['middleware']);
$this->middleware->memoryLeakProtection();
}
if (isset($options['validation'])) {
$this->validation = new Validation($this->obj, $options['validation']);
$this->validation->validate();
}
if (isset($options['filters'])) {
$this->filters = new DataFilters($options['filters']);
}
}
/**
* You can provide an array or any traversable object
*
* @param mixed $data The mixed type of object data to use
* @param array $options Options
*
* @return static
* @throws Exception
*/
public static function instance($data, array $options = [])
{
if (is_string($data)) {
$decodedData = json_decode($data);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("JSON decoding error: " . json_last_error_msg(), 22);
}
$data = $decodedData;
} elseif (! is_array($data) && ! is_object($data)) {
throw new Exception("Input data is neither an object nor an array.", 24);
}
if (is_array($data)) {
$data = (object) $data;
}
if (!is_object($data)) {
throw new Exception("Input data is not a valid object!\r\n" . print_r($data, true), 23);
}
return new static($data, $options);
}
/**
* Gets the value from the inside of the loaded object
* or returns the default value
*
* @param string $path requested object path like
* data/child_data instead of data->child_data
* @param mixed $default default value will return if value not exists
*
* @return mixed returns the value or default value (false)
*/
public function get(?string $path = '', $default = false)
{
$result = $this->obj->get($path);
if ($result === false) {
return $default;
}
if (isset($this->filters)) {
$result = $this->filters->filter($path, $result);
}
return $result;
}
/**
* Sets the value to the inside of the loaded object
*
* @param string $path requested object path like
* data/child_data instead of data->child_data
* @param mixed $value value to be set
*
* @return void
*/
public function set(string $path, $value): void
{
$this->obj->set($path, $value);
}
/**
* Checks if the value exists in the loaded object
*
* @param string $path requested object path like
* data/child_data instead of data->child_data
*
* @return bool
*/
public function has(string $path): bool
{
return $this->obj->has($path);
}
/**
* Returns the object as a JSON string
*
* @return string
*/
public function toJson(): string
{
return json_encode($this->obj);
}
/**
* Returns the object as an array
*
* @return array
*/
public function toArray(): array
{
return $this->obj->toArray();
}
/**
* The object is valid or not
*
* @param string $path data path ( /data/0/text )
*
* @return bool
*/
public function isValid(string $path = ''): bool
{
return $this->validation->isValid($path);
}
/**
* Set a memory limit
*
* @param int $memory memory limit
*
* @return void
*/
public function setMemoryLimit(int $memory): void
{
$this->middleware->setMemoryLimit($memory);
}
}