Completed
Push — master ( 88814d...52606f )
by Eric
04:51
created

Context::setOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Context;
13
14
use Ivory\Serializer\Exclusion\ExclusionStrategy;
15
use Ivory\Serializer\Exclusion\ExclusionStrategyInterface;
16
use Ivory\Serializer\Mapping\MetadataInterface;
17
use Ivory\Serializer\Naming\IdenticalNamingStrategy;
18
use Ivory\Serializer\Naming\NamingStrategyInterface;
19
use Ivory\Serializer\Navigator\NavigatorInterface;
20
use Ivory\Serializer\Visitor\VisitorInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class Context implements ContextInterface
26
{
27
    /**
28
     * @var NavigatorInterface
29
     */
30
    private $navigator;
31
32
    /**
33
     * @var VisitorInterface
34
     */
35
    private $visitor;
36
37
    /**
38
     * @var int
39
     */
40
    private $direction;
41
42
    /**
43
     * @var string
44
     */
45
    private $format;
46
47
    /**
48
     * @var bool
49
     */
50
    private $ignoreNull = false;
51
52
    /**
53
     * @var ExclusionStrategyInterface
54
     */
55
    private $exclusionStrategy;
56
57
    /**
58
     * @var NamingStrategyInterface
59
     */
60
    private $namingStrategy;
61
62
    /**
63
     * @var mixed[]
64
     */
65
    private $dataStack;
66
67
    /**
68
     * @var MetadataInterface[]
69
     */
70
    private $metadataStack;
71
72
    /**
73
     * @var mixed[]
74
     */
75
    private $options = [];
76
77
    /**
78
     * @param ExclusionStrategyInterface|null $exclusionStrategy
79
     * @param NamingStrategyInterface|null    $namingStrategy
80
     */
81 1184
    public function __construct(
82
        ExclusionStrategyInterface $exclusionStrategy = null,
83
        NamingStrategyInterface $namingStrategy = null
84
    ) {
85 1184
        $this->exclusionStrategy = $exclusionStrategy ?: new ExclusionStrategy();
86 1184
        $this->namingStrategy = $namingStrategy ?: new IdenticalNamingStrategy();
87 1184
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1440
    public function initialize(NavigatorInterface $navigator, VisitorInterface $visitor, $direction, $format)
93
    {
94 720
        $this
95 1440
            ->setNavigator($navigator)
96 1440
            ->setVisitor($visitor)
97 1440
            ->setDirection($direction)
98 1440
            ->setFormat($format)
99 1440
            ->setDataStack([])
100 1440
            ->setMetadataStack([]);
101 1440
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1440
    public function getNavigator()
107
    {
108 1440
        return $this->navigator;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1440
    public function setNavigator(NavigatorInterface $navigator)
115
    {
116 1440
        $this->navigator = $navigator;
117
118 1440
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1440
    public function getVisitor()
125
    {
126 1440
        return $this->visitor;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1440
    public function setVisitor(VisitorInterface $visitor)
133
    {
134 1440
        $this->visitor = $visitor;
135
136 1440
        return $this;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 192
    public function getDirection()
143
    {
144 192
        return $this->direction;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 1440
    public function setDirection($direction)
151
    {
152 1440
        $this->direction = $direction;
153
154 1440
        return $this;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function getFormat()
161
    {
162
        return $this->format;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 1440
    public function setFormat($format)
169
    {
170 1440
        $this->format = $format;
171
172 1440
        return $this;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 352
    public function isNullIgnored()
179
    {
180 352
        return $this->ignoreNull;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function setIgnoreNull($ignoreNull)
187
    {
188
        $this->ignoreNull = $ignoreNull;
189
190
        return $this;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 1192
    public function getExclusionStrategy()
197
    {
198 1192
        return $this->exclusionStrategy;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function setExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy)
205
    {
206
        $this->exclusionStrategy = $exclusionStrategy;
207
208
        return $this;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214 1088
    public function getNamingStrategy()
215
    {
216 1088
        return $this->namingStrategy;
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    public function setNamingStrategy(NamingStrategyInterface $namingStrategy)
223
    {
224
        $this->namingStrategy = $namingStrategy;
225
226
        return $this;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232 32
    public function getDataStack()
233
    {
234 32
        return $this->dataStack;
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240 1440
    public function setDataStack(array $dataStack)
241
    {
242 1440
        $this->dataStack = $dataStack;
243
244 1440
        return $this;
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250 92
    public function getMetadataStack()
251
    {
252 92
        return $this->metadataStack;
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258 1440
    public function setMetadataStack(array $metadataStack)
259
    {
260 1440
        $this->metadataStack = $metadataStack;
261
262 1440
        return $this;
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268 1192
    public function enterScope($data, MetadataInterface $metadata)
269
    {
270 1192
        $this->dataStack[] = $data;
271 1192
        $this->metadataStack[] = $metadata;
272 1192
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277 1192
    public function leaveScope()
278
    {
279 1192
        array_pop($this->dataStack);
280 1192
        array_pop($this->metadataStack);
281 1192
    }
282
283
    /**
284
     * {@inheritdoc}
285
     */
286
    public function hasOptions()
287
    {
288
        return $this->options;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->options; (array) is incompatible with the return type declared by the interface Ivory\Serializer\Context...xtInterface::hasOptions of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
289
    }
290
291
    /**
292
     * {@inheritdoc}
293
     */
294
    public function setOptions(array $options)
295
    {
296
        $this->options = [];
297
        $this->addOptions($options);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303
    public function addOptions(array $options)
304
    {
305
        foreach ($options as $option => $value) {
306
            $this->setOption($option, $value);
307
        }
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313
    public function hasOption($option)
314
    {
315
        return array_key_exists($option, $this->options);
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321
    public function getOption($option)
322
    {
323
        return $this->hasOption($option) ? $this->options[$option] : null;
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    public function setOption($option, $value)
330
    {
331
        $this->options[$option] = $value;
332
    }
333
334
    /**
335
     * {@inheritdoc}
336
     */
337
    public function removeOption($option)
338
    {
339
        unset($this->options[$option]);
340
    }
341
}
342