1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the rafrsr/lib-array2object package. |
5
|
|
|
* |
6
|
|
|
* (c) Rafael SR <https://github.com/rafrsr> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Rafrsr\LibArray2Object; |
12
|
|
|
|
13
|
|
|
use Rafrsr\LibArray2Object\Naming\CallableNamingStrategy; |
14
|
|
|
use Rafrsr\LibArray2Object\Naming\CamelCaseNamingStrategy; |
15
|
|
|
use Rafrsr\LibArray2Object\Naming\IdenticalNamingStrategy; |
16
|
|
|
use Rafrsr\LibArray2Object\Naming\UnderscoreNamingStrategy; |
17
|
|
|
use Rafrsr\LibArray2Object\Reader\AccessorReader; |
18
|
|
|
use Rafrsr\LibArray2Object\Reader\PropertyReaderInterface; |
19
|
|
|
use Rafrsr\LibArray2Object\Reader\ReflectionReader; |
20
|
|
|
use Rafrsr\LibArray2Object\Traits\IgnoreNullsTrait; |
21
|
|
|
use Rafrsr\LibArray2Object\Traits\NamingStrategyAwareTrait; |
22
|
|
|
|
23
|
|
|
class Object2ArrayBuilder extends AbstractBuilder |
24
|
|
|
{ |
25
|
|
|
use NamingStrategyAwareTrait; |
26
|
|
|
use IgnoreNullsTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var PropertyReaderInterface |
30
|
|
|
*/ |
31
|
|
|
private $reader; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return PropertyReaderInterface |
35
|
|
|
*/ |
36
|
|
|
public function getReader() |
37
|
|
|
{ |
38
|
|
|
return $this->reader; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param PropertyReaderInterface $reader |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function setReader($reader) |
47
|
|
|
{ |
48
|
|
|
$this->reader = $reader; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Name array keys as "PropertyName". |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function useUcFirstCamelCaseNamingStrategy() |
59
|
|
|
{ |
60
|
|
|
$this->setNamingStrategy(new CamelCaseNamingStrategy(true)); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Name array keys as "propertyName". |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function useCamelCaseNamingStrategy() |
71
|
|
|
{ |
72
|
|
|
$this->setNamingStrategy(new CamelCaseNamingStrategy()); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Name array keys as "property_name". |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function useUnderScoreNamingStrategy() |
83
|
|
|
{ |
84
|
|
|
$this->setNamingStrategy(new UnderscoreNamingStrategy()); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The array keys is identical to property name. |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function useIdenticalNamingStrategy() |
95
|
|
|
{ |
96
|
|
|
$this->setNamingStrategy(new IdenticalNamingStrategy()); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Use given callback to transform the array key. |
103
|
|
|
* |
104
|
|
|
* @param callable $callback |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function useCallbackNamingStrategy(callable $callback) |
109
|
|
|
{ |
110
|
|
|
$this->setNamingStrategy(new CallableNamingStrategy($callback)); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Read the property directly without use getters. |
117
|
|
|
* |
118
|
|
|
* @param bool $onlyPublicProperties only public properties should be exported |
119
|
|
|
* |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function disableGetters($onlyPublicProperties = false) |
123
|
|
|
{ |
124
|
|
|
$this->setReader(new ReflectionReader($onlyPublicProperties)); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Build custom Array2Object instance. |
131
|
|
|
*/ |
132
|
|
|
public function build() |
133
|
|
|
{ |
134
|
|
|
if ($this->getContext()) { |
135
|
|
|
$context = $this->getContext(); |
136
|
|
|
} else { |
137
|
|
|
$context = new Object2ArrayContext(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->prepareContext($context); |
141
|
|
|
|
142
|
|
|
return new Object2Array($context); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
protected function prepareContext(AbstractContext $context) |
149
|
|
|
{ |
150
|
|
|
parent::prepareContext($context); |
151
|
|
|
|
152
|
|
|
if ($context instanceof Object2ArrayContext) { |
153
|
|
|
$context->setIgnoreNulls($this->isIgnoreNulls()); |
154
|
|
|
$context->setReader($this->getReader() ?: new AccessorReader()); |
155
|
|
|
$context->setNamingStrategy($this->getNamingStrategy() ?: new CamelCaseNamingStrategy()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.