Skip to content

Commit 144b942

Browse files
ObjectPropertyFinder
1 parent b950fe3 commit 144b942

File tree

2 files changed

+449
-0
lines changed

2 files changed

+449
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @see https://github.com/niceshops/nice-core for the canonical source repository
5+
* @license https://github.com/niceshops/nice-core/blob/master/LICENSE BSD 3-Clause License
6+
*/
7+
8+
namespace NiceshopsDev\NiceCore\Helper\Object;
9+
10+
11+
use ArrayAccess;
12+
use ArrayObject;
13+
use NiceshopsDev\NiceCore\Exception;
14+
use stdClass;
15+
16+
class ObjectPropertyFinder
17+
{
18+
19+
/**
20+
* @var array|object
21+
*/
22+
private $object;
23+
24+
25+
/**
26+
* ObjectPropertyFinder constructor.
27+
*
28+
* @param array|object $object
29+
*
30+
* @throws Exception passed value is not an object or array
31+
*/
32+
public function __construct($object)
33+
{
34+
if (!is_object($object) && !is_array($object)) {
35+
throw new Exception("Passed value is not an object or array!");
36+
}
37+
38+
$this->object = $object;
39+
}
40+
41+
42+
/**
43+
* @return array|object
44+
*/
45+
public function getObject()
46+
{
47+
return $this->object;
48+
}
49+
50+
51+
/**
52+
* @return array
53+
*/
54+
public function getKeys(): array
55+
{
56+
$object = $this->getObject();
57+
if (is_array($object)) {
58+
$arrKey = array_keys($object);
59+
} elseif ($object instanceof stdClass) {
60+
$arrKey = array_keys((array)$object);
61+
} elseif ($object instanceof ArrayObject) {
62+
$arrKey = array_keys($object->getArrayCopy());
63+
} elseif (method_exists($object, "toArray")) {
64+
$arrKey = array_keys($object->toArray());
65+
} else {
66+
$arrKey = array_keys(get_object_vars($object));
67+
}
68+
69+
return $arrKey;
70+
}
71+
72+
73+
/**
74+
* @return array
75+
*/
76+
public function getValues(): array
77+
{
78+
$arrValue = [];
79+
80+
foreach ($this->getKeys() as $key) {
81+
$arrValue[$key] = $this->getValue($key);
82+
}
83+
84+
return $arrValue;
85+
}
86+
87+
88+
/**
89+
* @param int|string $key
90+
*
91+
* @return bool
92+
*/
93+
public function hasKey($key): bool
94+
{
95+
return in_array($key, $this->getKeys(), true);
96+
}
97+
98+
99+
/**
100+
* @param int|string $key
101+
* @param null $defaultValue
102+
*
103+
* @return mixed
104+
*/
105+
public function getValue($key, $defaultValue = null)
106+
{
107+
$object = $this->getObject();
108+
109+
if (!$this->hasKey($key)) {
110+
return $defaultValue;
111+
}
112+
113+
if ((is_array($object) || $object instanceof ArrayObject || $object instanceof stdClass)) {
114+
return ((array)$object)[$key] ?? $defaultValue;
115+
}
116+
117+
if ($object instanceof ArrayAccess) {
118+
return $object->offsetGet($key) ?? $defaultValue;
119+
}
120+
121+
if (method_exists($object, "getData")) {
122+
return $object->getData($key) ?? $defaultValue;
123+
}
124+
125+
return get_object_vars($object)[$key] ?? $defaultValue;
126+
}
127+
}

0 commit comments

Comments
 (0)