1 | <?php |
||
10 | abstract class DataTransferObject |
||
11 | { |
||
12 | /** @var array */ |
||
13 | protected $allValues = []; |
||
14 | |||
15 | /** @var array */ |
||
16 | protected $exceptKeys = []; |
||
17 | |||
18 | /** @var array */ |
||
19 | protected $onlyKeys = []; |
||
20 | |||
21 | public function __construct(array $parameters) |
||
48 | |||
49 | public function all(): array |
||
53 | |||
54 | /** |
||
55 | * @param string ...$keys |
||
56 | * |
||
57 | * @return static |
||
58 | */ |
||
59 | public function only(string ...$keys): DataTransferObject |
||
67 | |||
68 | /** |
||
69 | * @param string ...$keys |
||
70 | * |
||
71 | * @return static |
||
72 | */ |
||
73 | public function except(string ...$keys): DataTransferObject |
||
81 | |||
82 | public function toArray(): array |
||
83 | { |
||
84 | if (count($this->onlyKeys)) { |
||
85 | $array = Arr::only($this->all(), $this->onlyKeys); |
||
86 | } else { |
||
87 | $array = Arr::except($this->all(), $this->exceptKeys); |
||
88 | } |
||
89 | |||
90 | $array = $this->parseArray($array); |
||
91 | |||
92 | return $array; |
||
93 | } |
||
94 | |||
95 | protected function parseArray(array $array): array |
||
96 | { |
||
97 | foreach ($array as $key => $value) { |
||
98 | if ( |
||
99 | $value instanceof DataTransferObject |
||
100 | || $value instanceof DataTransferObjectCollection |
||
101 | ) { |
||
102 | $array[$key] = $value->toArray(); |
||
103 | |||
104 | continue; |
||
105 | } |
||
106 | |||
107 | if (! is_array($value)) { |
||
108 | continue; |
||
109 | } |
||
110 | |||
111 | $array[$key] = $this->parseArray($value); |
||
112 | } |
||
113 | |||
114 | return $array; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param \ReflectionClass $class |
||
119 | * |
||
120 | * @return array|\Spatie\DataTransferObject\Property[] |
||
121 | */ |
||
122 | protected function getPublicProperties(ReflectionClass $class): array |
||
132 | } |
||
133 |