1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @copyright (c) 2019 Mendel <[email protected]> |
4
|
|
|
* @license see license.txt |
5
|
|
|
*/ |
6
|
|
|
namespace drycart\data; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Wrapper for pretty access to field and check flexible logic conditions |
10
|
|
|
* Used for deep access to some data at some unknown data |
11
|
|
|
*/ |
12
|
|
|
class DataWrapper implements \Countable, \JsonSerializable, \IteratorAggregate |
13
|
|
|
{ |
14
|
|
|
use CheckTrait; |
15
|
|
|
|
16
|
|
|
protected $data; |
17
|
|
|
protected $safe = true; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param mixed $data Data for we access. Array, object etc... |
21
|
|
|
* @param bool $safe if true - Exception for not exist fields |
22
|
|
|
*/ |
23
|
|
|
public function __construct($data, bool $safe = true) |
24
|
|
|
{ |
25
|
|
|
$this->data = $data; |
26
|
|
|
$this->safe = $safe; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get some data by pretty name |
31
|
|
|
* |
32
|
|
|
* @param string $name name for access |
33
|
|
|
* @param mixed $default used for non safe request, if we dont find answer |
34
|
|
|
* @return mixed |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
|
|
public function get(string $name, $default = null) |
38
|
|
|
{ |
39
|
|
|
return GetterHelper::get($this->data, $name, $this->safe, $default); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Magic proxy call to data |
44
|
|
|
* @param string $name |
45
|
|
|
* @param array $arguments |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function __call($name, $arguments) |
49
|
|
|
{ |
50
|
|
|
// @2DO: check if method exist |
51
|
|
|
return call_user_func_array([$this->data, $name], $arguments); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Magic isset |
56
|
|
|
* @param string $name |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function __isset($name) |
60
|
|
|
{ |
61
|
|
|
return !is_null($this->get($name)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Magic getter, sugar for get() |
66
|
|
|
* @param string $name |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function __get($name) |
70
|
|
|
{ |
71
|
|
|
return $this->get($name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get count of data fields, just sugar for data methods |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
public function count(): int |
80
|
|
|
{ |
81
|
|
|
return count($this->data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Json serialise data - here just data object/array |
86
|
|
|
* @return type |
87
|
|
|
*/ |
88
|
|
|
public function jsonSerialize() |
89
|
|
|
{ |
90
|
|
|
return $this->data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getIterator(): \Traversable |
94
|
|
|
{ |
95
|
|
|
if(is_a($this->data, \Traversable::class)) { |
96
|
|
|
return $this->data; |
97
|
|
|
} elseif(is_array($this->data)) { |
98
|
|
|
return new \ArrayIterator($this->data); |
99
|
|
|
} else { |
100
|
|
|
return new \ArrayIterator((array) $this->data); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |