1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bludata\Doctrine\ORM\Traits; |
4
|
|
|
|
5
|
|
|
use Bludata\Doctrine\Common\Annotations\Fillable; |
6
|
|
|
use DateTime; |
7
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\ORM\PersistentCollection; |
10
|
|
|
use ReflectionProperty; |
11
|
|
|
|
12
|
|
|
trait ToArrayTrait |
13
|
|
|
{ |
14
|
|
|
private function isOnly(ReflectionProperty $property, array $optionsToArray): bool |
15
|
|
|
{ |
16
|
|
|
if (!isset($optionsToArray['only']) && !isset($optionsToArray['except'])) { |
17
|
|
|
return true; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (isset($optionsToArray['only'])) { |
21
|
|
|
if (in_array($property->getName(), $optionsToArray['only'])) { |
22
|
|
|
return true; |
23
|
|
|
} |
24
|
|
|
} elseif (isset($optionsToArray['except']) && !in_array($property->getName(), $optionsToArray['except'])) { |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function getPropertiesFillable(): array |
32
|
|
|
{ |
33
|
|
|
$allProperties = $this->getRepository() |
|
|
|
|
34
|
|
|
->getClassMetadata() |
35
|
|
|
->getReflectionClass() |
36
|
|
|
->getProperties(); |
37
|
|
|
|
38
|
|
|
return array_filter($allProperties, function ($property) { |
39
|
|
|
$existAnnotationToArray = array_filter((new AnnotationReader())->getPropertyAnnotations($property), function ($annotation) { |
40
|
|
|
return $annotation instanceof Fillable; |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
return count($existAnnotationToArray); |
44
|
|
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function toArray(array $options = []): array |
48
|
|
|
{ |
49
|
|
|
$classMetadata = $this->getRepository() |
50
|
|
|
->getClassMetadata(); |
51
|
|
|
|
52
|
|
|
$propertiesFillable = $this->getPropertiesFillable(); |
53
|
|
|
|
54
|
|
|
$array = []; |
55
|
|
|
|
56
|
|
|
foreach ($propertiesFillable as $property) { |
57
|
|
|
if ($this->isOnly($property, $options)) { |
58
|
|
|
$key = $property->getName(); |
59
|
|
|
$metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null; |
60
|
|
|
|
61
|
|
|
if (is_object($this->$key)) { |
62
|
|
|
if ($this->$key instanceof DateTime) { |
63
|
|
|
if ($this->$key) { |
64
|
|
|
$dateFormat = 'Y-m-d'; |
65
|
|
|
|
66
|
|
|
if ($metaDataKey) { |
67
|
|
|
switch ($metaDataKey['type']) { |
68
|
|
|
case 'datetime': |
69
|
|
|
$dateFormat = 'Y-m-d H:i:s'; |
70
|
|
|
break; |
71
|
|
|
case 'time': |
72
|
|
|
$dateFormat = 'H:i:s'; |
73
|
|
|
break; |
74
|
|
|
default: |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$array[$key] = $this->$key->format($dateFormat); |
80
|
|
|
} |
81
|
|
|
} elseif ($this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) { |
82
|
|
|
$array[$key] = array_map(function ($item) { |
83
|
|
|
return $item->getId(); |
84
|
|
|
}, $this->$key->getValues()); |
85
|
|
|
} else { |
86
|
|
|
if (method_exists($this->$key, 'getId')) { |
87
|
|
|
$array[$key] = $this->$key->getId(); |
88
|
|
|
} else { |
89
|
|
|
$array[$key] = $this->$key; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
if (in_array($metaDataKey['type'], ['decimal', 'float'])) { |
94
|
|
|
$array[$key] = (float) $this->$key; |
95
|
|
|
} else { |
96
|
|
|
$array[$key] = $this->$key; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $array; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|