Test Failed
Pull Request — master (#12)
by Vincent
12:16
created

setDefaultDenormalizationOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Bdf\Serializer;
4
5
use Bdf\Serializer\Context\DenormalizationContext;
6
use Bdf\Serializer\Context\NormalizationContext;
7
use Bdf\Serializer\Metadata\Driver\AnnotationsDriver;
8
use Bdf\Serializer\Metadata\Driver\DriverInterface;
9
use Bdf\Serializer\Metadata\Driver\StaticMethodDriver;
10
use Bdf\Serializer\Metadata\MetadataFactory;
11
use Bdf\Serializer\Normalizer\DateTimeNormalizer;
12
use Bdf\Serializer\Normalizer\NormalizerInterface;
13
use Bdf\Serializer\Normalizer\NormalizerLoader;
14
use Bdf\Serializer\Normalizer\PropertyNormalizer;
15
use Bdf\Serializer\Normalizer\TraversableNormalizer;
16
use Psr\SimpleCache\CacheInterface;
17
18
/**
19
 * Builder for serializer.
20
 *
21
 * @author seb
22
 *
23
 * @psalm-consistent-constructor
24
 */
25
class SerializerBuilder
26
{
27
    /**
28
     * The cache for normalizer
29
     *
30
     * @var CacheInterface|null
31
     */
32
    private $cache;
33
34
    /**
35
     * The normalizers
36
     *
37
     * @var NormalizerInterface[]
38
     */
39
    private $normalizers = [];
40
41
    /**
42
     * The metadata drivers
43
     *
44
     * @var DriverInterface[]
45
     */
46
    private $drivers = [];
47
48
    /**
49
     * Default options to use when denormalizing (i.e. convert serialized data to PHP data).
50
     *
51 190
     * @var array<string, mixed>|null
52
     */
53 190
    private $defaultDenormalizationOptions;
54
55
    /**
56
     * Default options to use when normalizing (i.e. convert PHP data to serialized data).
57
     *
58
     * @var array<string, mixed>|null
59
     */
60
    private $defaultNormalizationOptions;
61
62
    /**
63
     * Create a new builder
64
     *
65
     * @return self
66
     */
67
    public static function create()
68
    {
69
        return new static();
70
    }
71
72
    /**
73
     * Set the cache
74
     *
75
     * @param CacheInterface $cache  The cache driver.
76
     *
77 26
     * @return $this
78
     */
79 26
    public function setCache(CacheInterface $cache)
80
    {
81 26
        $this->cache = $cache;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Set the normalizers
88
     *
89
     * @param NormalizerInterface[] $normalizers
90
     *
91
     * @return $this
92
     */
93
    public function setNormalizers(array $normalizers)
94
    {
95
        $this->normalizers = $normalizers;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Set the metadata drivers
102
     *
103 190
     * @param DriverInterface[] $drivers
104
     *
105 190
     * @return $this
106 164
     */
107 164
    public function setDrivers(array $drivers)
108 164
    {
109 164
        $this->drivers = $drivers;
110 164
111
        return $this;
112
    }
113 164
114 164
    /**
115 164
     * Configure default option to use when denormalizing (i.e. convert serialized data to PHP data).
116 164
     *
117 164
     * @param array<string, mixed>|null $defaultDenormalizationOptions
118
     * @return $this
119
     *
120 190
     * @see DenormalizationContext
121
     */
122
    public function setDefaultDenormalizationOptions(?array $defaultDenormalizationOptions)
123
    {
124
        $this->defaultDenormalizationOptions = $defaultDenormalizationOptions;
125
        return $this;
126
    }
127
128
    /**
129
     * Configure default option to use when normalizing (i.e. convert PHP data to serialized data).
130
     *
131
     * @param array<string, mixed>|null $defaultNormalizationOptions
132
     * @return $this
133
     *
134
     * @see NormalizationContext
135
     */
136
    public function setDefaultNormalizationOptions(?array $defaultNormalizationOptions)
137
    {
138
        $this->defaultNormalizationOptions = $defaultNormalizationOptions;
139
        return $this;
140
    }
141
142
    /**
143
     * Build the serializer
144
     *
145
     * @return Serializer
146
     */
147
    public function build()
148
    {
149
        if (!$this->normalizers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->normalizers of type Bdf\Serializer\Normalizer\NormalizerInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
150
            if (!$this->drivers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->drivers of type Bdf\Serializer\Metadata\Driver\DriverInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
151
                $this->drivers = [
152
                    new StaticMethodDriver(),
153
                    new AnnotationsDriver(),
154
                ];
155
            }
156
157
            $this->normalizers = [
158
                new DateTimeNormalizer(),
159
                new TraversableNormalizer(),
160
                new PropertyNormalizer(new MetadataFactory($this->drivers, $this->cache))
161
            ];
162
        }
163
164
        return new Serializer(
165
            new NormalizerLoader($this->normalizers),
166
            $this->defaultDenormalizationOptions,
167
            $this->defaultNormalizationOptions
168
        );
169
    }
170
}
171