Passed
Push — master ( e4da81...49dc91 )
by Vincent
08:33
created

SerializerBuilder::setNormalizers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bdf\Serializer;
4
5
use Bdf\Serializer\Metadata\Driver\AnnotationsDriver;
6
use Bdf\Serializer\Metadata\Driver\DriverInterface;
7
use Bdf\Serializer\Metadata\Driver\StaticMethodDriver;
8
use Bdf\Serializer\Metadata\MetadataFactory;
9
use Bdf\Serializer\Normalizer\DateTimeNormalizer;
10
use Bdf\Serializer\Normalizer\NormalizerInterface;
11
use Bdf\Serializer\Normalizer\NormalizerLoader;
12
use Bdf\Serializer\Normalizer\PropertyNormalizer;
13
use Bdf\Serializer\Normalizer\TraversableNormalizer;
14
use Psr\SimpleCache\CacheInterface;
15
16
/**
17
 * Builder for serializer.
18
 *
19
 * @author seb
20
 *
21
 * @psalm-consistent-constructor
22
 */
23
class SerializerBuilder
24
{
25
    /**
26
     * The cache for normalizer
27
     *
28
     * @var CacheInterface|null
29
     */
30
    private $cache;
31
32
    /**
33
     * The normalizers
34
     *
35
     * @var NormalizerInterface[]
36
     */
37
    private $normalizers = [];
38
39
    /**
40
     * The metadata drivers
41
     *
42
     * @var DriverInterface[]
43
     */
44
    private $drivers = [];
45
46
    /**
47
     * Create a new builder
48
     *
49
     * @return self
50
     */
51 190
    public static function create()
52
    {
53 190
        return new static();
54
    }
55
56
    /**
57
     * Set the cache
58
     *
59
     * @param CacheInterface $cache  The cache driver.
60
     *
61
     * @return $this
62
     */
63
    public function setCache(CacheInterface $cache)
64
    {
65
        $this->cache = $cache;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Set the normalizers
72
     *
73
     * @param NormalizerInterface[] $normalizers
74
     *
75
     * @return $this
76
     */
77 26
    public function setNormalizers(array $normalizers)
78
    {
79 26
        $this->normalizers = $normalizers;
80
81 26
        return $this;
82
    }
83
84
    /**
85
     * Set the metadata drivers
86
     *
87
     * @param DriverInterface[] $drivers
88
     *
89
     * @return $this
90
     */
91
    public function setDrivers(array $drivers)
92
    {
93
        $this->drivers = $drivers;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Build the serializer
100
     *
101
     * @return Serializer
102
     */
103 190
    public function build()
104
    {
105 190
        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...
106 164
            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...
107 164
                $this->drivers = [
108 164
                    new StaticMethodDriver(),
109 164
                    new AnnotationsDriver(),
110 164
                ];
111
            }
112
113 164
            $this->normalizers = [
114 164
                new DateTimeNormalizer(),
115 164
                new TraversableNormalizer(),
116 164
                new PropertyNormalizer(new MetadataFactory($this->drivers, $this->cache))
117 164
            ];
118
        }
119
120 190
        return new Serializer(new NormalizerLoader($this->normalizers));
121
    }
122
}
123