Completed
Pull Request — master (#15)
by
unknown
03:34
created

createEmbedMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Mapping;
6
7
use Chubbyphp\Serialization\Accessor\PropertyAccessor;
8
use Chubbyphp\Serialization\Normalizer\CallbackFieldNormalizer;
9
use Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer;
10
use Chubbyphp\Serialization\Normalizer\FieldNormalizer;
11
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
12
use Chubbyphp\Serialization\Normalizer\Relation\EmbedManyFieldNormalizer;
13
use Chubbyphp\Serialization\Normalizer\Relation\EmbedOneFieldNormalizer;
14
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceManyFieldNormalizer;
15
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceOneFieldNormalizer;
16
use Chubbyphp\Serialization\Policy\NullPolicy;
17
use Chubbyphp\Serialization\Policy\PolicyInterface;
18
19
final class NormalizationFieldMappingBuilder implements NormalizationFieldMappingBuilderInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $name;
25
26
    /**
27
     * @var array
28
     */
29
    private $groups = [];
30
31
    /**
32
     * @var FieldNormalizerInterface|null
33
     */
34
    private $fieldNormalizer;
35
36
    /**
37
     * @var PolicyInterface|null
38
     */
39
    private $policy;
40
41
    /**
42
     * @param string $name
43
     */
44 9
    private function __construct(string $name)
45
    {
46 9
        $this->name = $name;
47 9
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return NormalizationFieldMappingBuilderInterface
53
     */
54 2
    public static function create(string $name): NormalizationFieldMappingBuilderInterface
55
    {
56 2
        return new self($name);
57
    }
58
59
    /**
60
     * @param string   $name
61
     * @param callable $callback
62
     *
63
     * @return NormalizationFieldMappingBuilderInterface
64
     */
65 1
    public static function createCallback(string $name, callable $callback): NormalizationFieldMappingBuilderInterface
66
    {
67 1
        $self = new self($name);
68 1
        $self->fieldNormalizer = new CallbackFieldNormalizer($callback);
69
70 1
        return $self;
71
    }
72
73
    /**
74
     * @param string $name
75
     * @param string $format
76
     *
77
     * @return NormalizationFieldMappingBuilderInterface
78
     */
79 2
    public static function createDateTime(string $name, string $format = 'c'): NormalizationFieldMappingBuilderInterface
80
    {
81 2
        $self = new self($name);
82 2
        $self->fieldNormalizer = new DateTimeFieldNormalizer(new PropertyAccessor($name), $format);
83
84 2
        return $self;
85
    }
86
87
    /**
88
     * @param string $name
89
     *
90
     * @return NormalizationFieldMappingBuilderInterface
91
     */
92 1
    public static function createEmbedMany(string $name): NormalizationFieldMappingBuilderInterface
93
    {
94 1
        $self = new self($name);
95 1
        $self->fieldNormalizer = new EmbedManyFieldNormalizer(new PropertyAccessor($name));
96
97 1
        return $self;
98
    }
99
100
    /**
101
     * @param string $name
102
     *
103
     * @return NormalizationFieldMappingBuilderInterface
104
     */
105 1
    public static function createEmbedOne(string $name): NormalizationFieldMappingBuilderInterface
106
    {
107 1
        $self = new self($name);
108 1
        $self->fieldNormalizer = new EmbedOneFieldNormalizer(new PropertyAccessor($name));
109
110 1
        return $self;
111
    }
112
113
    /**
114
     * @param string $name
115
     * @param string $idName
116
     *
117
     * @return NormalizationFieldMappingBuilderInterface
118
     */
119 1 View Code Duplication
    public static function createReferenceMany(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
        string $name,
121
        string $idName = 'id'
122
    ): NormalizationFieldMappingBuilderInterface {
123 1
        $self = new self($name);
124 1
        $self->fieldNormalizer = new ReferenceManyFieldNormalizer(
125 1
            new PropertyAccessor($idName),
126 1
            new PropertyAccessor($name)
127
        );
128
129 1
        return $self;
130
    }
131
132
    /**
133
     * @param string $name
134
     * @param string $idName
135
     *
136
     * @return NormalizationFieldMappingBuilderInterface
137
     */
138 1 View Code Duplication
    public static function createReferenceOne(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
        string $name,
140
        string $idName = 'id'
141
    ): NormalizationFieldMappingBuilderInterface {
142 1
        $self = new self($name);
143 1
        $self->fieldNormalizer = new ReferenceOneFieldNormalizer(
144 1
            new PropertyAccessor($idName),
145 1
            new PropertyAccessor($name)
146
        );
147
148 1
        return $self;
149
    }
150
151
    /**
152
     * @deprecated
153
     *
154
     * @param array $groups
155
     *
156
     * @return NormalizationFieldMappingBuilderInterface
157
     */
158 1
    public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface
159
    {
160 1
        $this->groups = $groups;
161
162 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...tionFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...derInterface::setGroups of type self.

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...
163
    }
164
165
    /**
166
     * @param FieldNormalizerInterface $fieldNormalizer
167
     *
168
     * @return NormalizationFieldMappingBuilderInterface
169
     */
170 1
    public function setFieldNormalizer(
171
        FieldNormalizerInterface $fieldNormalizer
172
    ): NormalizationFieldMappingBuilderInterface {
173 1
        $this->fieldNormalizer = $fieldNormalizer;
174
175 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...tionFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...ace::setFieldNormalizer of type self.

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...
176
    }
177
178
    /**
179
     * @param PolicyInterface $policy
180
     *
181
     * @return NormalizationFieldMappingBuilderInterface
182
     */
183
    public function setPolicy(PolicyInterface $policy): NormalizationFieldMappingBuilderInterface
184
    {
185
        $this->policy = $policy;
186
187
        return $this;
188
    }
189
190
    /**
191
     * @return NormalizationFieldMappingInterface
192
     */
193 9
    public function getMapping(): NormalizationFieldMappingInterface
194
    {
195 9
        return new NormalizationFieldMapping(
196 9
            $this->name,
197 9
            $this->groups,
198 9
            $this->fieldNormalizer ?? new FieldNormalizer(new PropertyAccessor($this->name)),
199 9
            $this->policy ?? new NullPolicy()
200
        );
201
    }
202
}
203