1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/helpers package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2018 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Sauls\Component\Helper\Operation\ObjectOperation; |
14
|
|
|
|
15
|
|
|
class ToArray implements ToArrayInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var GetPropertyValueInterface |
19
|
|
|
*/ |
20
|
|
|
private $getPropertyValueOperation; |
21
|
|
|
|
22
|
1 |
|
public function __construct(GetPropertyValueInterface $getPropertyValueOperation) |
23
|
|
|
{ |
24
|
1 |
|
$this->getPropertyValueOperation = $getPropertyValueOperation; |
25
|
1 |
|
} |
26
|
|
|
|
27
|
6 |
|
public function execute($object, array $properties = [], bool $recursive = true): array |
28
|
|
|
{ |
29
|
6 |
|
if (\is_array($object)) { |
30
|
3 |
|
return $this->processArray($object, $properties, $recursive); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
if (\is_object($object)) { |
34
|
3 |
|
return $this->processObject($object, $properties, $recursive); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
return [$object]; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
private function processArray(array $object, array $properties, bool $recursive): array |
41
|
|
|
{ |
42
|
3 |
|
if ($recursive) { |
43
|
2 |
|
return $this->processRecursiveArray($object, $properties, $recursive); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return $object; |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
private function processRecursiveArray($object, array $properties, bool $recursive): array |
50
|
|
|
{ |
51
|
2 |
|
foreach ($object as $key => $value) { |
52
|
2 |
|
if ($this->valueIsArrayOrObject($value)) { |
53
|
2 |
|
$object[$key] = $this->execute($value, $properties, $recursive); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
return $object; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
private function processObject($object, array $properties, bool $recursive): array |
61
|
|
|
{ |
62
|
3 |
|
if (!empty($properties)) { |
63
|
2 |
|
$className = \get_class($object); |
64
|
2 |
|
if (!empty($properties[$className])) { |
65
|
2 |
|
return $this->processProperties($object, $className, $properties, $recursive); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $this->processObjectProperties($object, $properties, $recursive); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
private function processProperties($object, string $className, array $properties, bool $recursive): array |
73
|
|
|
{ |
74
|
2 |
|
$result = []; |
75
|
2 |
|
foreach ($properties[$className] as $key => $name) { |
76
|
2 |
|
if (is_int($key)) { |
77
|
2 |
|
$result[$name] = $object->$name; |
78
|
|
|
} else { |
79
|
2 |
|
$result[$key] = $this->getPropertyValueOperation->execute($object, $name); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
return $recursive ? $this->execute($result, $properties) : $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
private function processObjectProperties($object, array $properties, bool $recursive): array |
87
|
|
|
{ |
88
|
1 |
|
$result = []; |
89
|
1 |
|
foreach ($object as $key => $value) { |
90
|
1 |
|
$result[$key] = $value; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $recursive ? $this->execute($result, $properties) : $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
private function valueIsArrayOrObject($value): bool |
97
|
|
|
{ |
98
|
2 |
|
return \is_array($value) || \is_object($value); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|