Completed
Push — master ( fef923...c0df34 )
by Rafael
03:03
created

Array2ObjectBuilder::build()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
rs 5.3846
cc 8
eloc 23
nc 18
nop 0
1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object;
11
12
use Rafrsr\LibArray2Object\Matcher\CamelizeMatcher;
13
use Rafrsr\LibArray2Object\Parser\BooleanParser;
14
use Rafrsr\LibArray2Object\Parser\DateTimeParser;
15
use Rafrsr\LibArray2Object\Parser\FloatParser;
16
use Rafrsr\LibArray2Object\Parser\IntegerParser;
17
use Rafrsr\LibArray2Object\Parser\ObjectParser;
18
use Rafrsr\LibArray2Object\Parser\StringParser;
19
use Rafrsr\LibArray2Object\Parser\ValueParserInterface;
20
use Rafrsr\LibArray2Object\Matcher\PropertyMatcherInterface;
21
use Rafrsr\LibArray2Object\Writer\AccessorWriter;
22
use Rafrsr\LibArray2Object\Writer\PropertyWriterInterface;
23
24
/**
25
 * Class Array2ObjectBuilder
26
 */
27
class Array2ObjectBuilder
28
{
29
30
    /**
31
     * @var Array2ObjectContext
32
     */
33
    private $context;
34
35
    /**
36
     * @var array|ValueParserInterface[]
37
     */
38
    private $parsers = [];
39
40
    /**
41
     * @var PropertyMatcherInterface
42
     */
43
    private $propertyMatcher;
44
45
    /**
46
     * @var PropertyWriterInterface
47
     */
48
    private $propertyWriter;
49
50
    /**
51
     * @var array
52
     */
53
    private $disabledParsers = [];
54
55
    /**
56
     * create builder instance
57
     *
58
     * @return static
59
     */
60
    public static function create()
61
    {
62
        return new static;
63
    }
64
65
    /**
66
     * @return Array2ObjectContext
67
     */
68
    public function getContext()
69
    {
70
        return $this->context;
71
    }
72
73
    /**
74
     * @param Array2ObjectContext $context
75
     *
76
     * @return $this
77
     */
78
    public function setContext($context)
79
    {
80
        $this->context = $context;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return array|Parser\ValueParserInterface[]
87
     */
88
    public function getParsers()
89
    {
90
        return $this->parsers;
91
    }
92
93
    /**
94
     * @param ValueParserInterface $parser
95
     *
96
     * @return $this
97
     */
98
    public function addParser(ValueParserInterface $parser)
99
    {
100
        $this->parsers[$parser->getName()] = $parser;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param string|ValueParserInterface $parser
107
     *
108
     * @return boolean
109
     */
110
    public function hasParser($parser)
111
    {
112
        if ($parser instanceof ValueParserInterface) {
113
            $parser = $parser->getName();
114
        }
115
116
        return array_key_exists($parser, $this->parsers);
117
    }
118
119
    /**
120
     * disableParser
121
     *
122
     * @param string|ValueParserInterface $parser
123
     *
124
     * @return $this
125
     */
126
    public function disableParser($parser)
127
    {
128
        if ($parser instanceof ValueParserInterface) {
129
            $parser = $parser->getName();
130
        }
131
132
        if (is_string($parser)) {
133
            $this->disabledParsers[] = $parser;
134
        }
135
136
        return $this;
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function getDisabledParsers()
143
    {
144
        return $this->disabledParsers;
145
    }
146
147
    /**
148
     * @param string|ValueParserInterface $parser
149
     *
150
     * @return $this
151
     */
152
    public function removeParser($parser)
153
    {
154
        if ($parser instanceof ValueParserInterface) {
155
            $parser = $parser->getName();
156
        }
157
158
        if (is_string($parser) && $this->hasParser($parser)) {
159
            unset($this->parsers[$parser]);
160
        }
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param array|Parser\ValueParserInterface[] $parsers
167
     *
168
     * @return $this
169
     */
170 View Code Duplication
    public function setParsers($parsers)
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...
171
    {
172
        $this->parsers = [];
173
        foreach ($parsers as $parser) {
174
            if ($parser instanceof ValueParserInterface) {
175
                $this->parsers[$parser->getName()] = $parser;
176
            }
177
        }
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return PropertyMatcherInterface
184
     */
185
    public function getPropertyMatcher()
186
    {
187
        return $this->propertyMatcher;
188
    }
189
190
    /**
191
     * @param PropertyMatcherInterface $propertyMatcher
192
     *
193
     * @return $this
194
     */
195
    public function setPropertyMatcher(PropertyMatcherInterface $propertyMatcher)
196
    {
197
        $this->propertyMatcher = $propertyMatcher;
198
199
        return $this;
200
    }
201
202
    /**
203
     * @return PropertyWriterInterface
204
     */
205
    public function getPropertyWriter()
206
    {
207
        return $this->propertyWriter;
208
    }
209
210
    /**
211
     * @param PropertyWriterInterface $propertyWriter
212
     *
213
     * @return $this
214
     */
215
    public function setPropertyWriter($propertyWriter)
216
    {
217
        $this->propertyWriter = $propertyWriter;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Build custom Array2Object instance
224
     */
225
    public function build()
226
    {
227
        if ($this->getContext()) {
228
            $context = $this->getContext();
229
        } else {
230
            $context = new Array2ObjectContext();
231
        }
232
233
        //defaults
234
        $context->setMatcher($this->getPropertyMatcher() ?: new CamelizeMatcher());
235
        $context->setWriter($this->getPropertyWriter() ?: new AccessorWriter());
236
        $context->setParsers(
237
            [
238
                new StringParser(),
239
                new BooleanParser(),
240
                new IntegerParser(),
241
                new FloatParser(),
242
                new DateTimeParser(),
243
                new ObjectParser($context)
244
            ]
245
        );
246
247
        //add custom parsers
248
        foreach ($this->parsers as $parser) {
249
            if ($parser instanceof ValueParserInterface) {
250
                $context->prependParser($parser);
251
            }
252
        }
253
254
        //disable parsers
255
        foreach ($this->disabledParsers as $disabledParser) {
256
            $actualParsers = $context->getParsers();
257
            if (array_key_exists($disabledParser, $actualParsers)) {
258
                unset($actualParsers[$disabledParser]);
259
            }
260
            $context->setParsers($actualParsers);
261
        }
262
263
        return new Array2Object($context);
264
    }
265
}