Completed
Push — master ( 366fbf...5f1478 )
by Rafael
02:45
created

AbstractBuilder::prepareContext()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.439
cc 6
eloc 17
nc 9
nop 1
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\Matcher\PropertyMatcherInterface;
14
use Rafrsr\LibArray2Object\Parser\BooleanParser;
15
use Rafrsr\LibArray2Object\Parser\DateTimeParser;
16
use Rafrsr\LibArray2Object\Parser\FloatParser;
17
use Rafrsr\LibArray2Object\Parser\IntegerParser;
18
use Rafrsr\LibArray2Object\Parser\ObjectParser;
19
use Rafrsr\LibArray2Object\Parser\StringParser;
20
use Rafrsr\LibArray2Object\Parser\ValueParserInterface;
21
22
abstract class AbstractBuilder
23
{
24
    /**
25
     * @var AbstractContext
26
     */
27
    private $context;
28
29
    /**
30
     * @var array|ValueParserInterface[]
31
     */
32
    private $parsers = [];
33
34
    /**
35
     * @var PropertyMatcherInterface
36
     */
37
    private $matcher;
38
39
    /**
40
     * @var array
41
     */
42
    private $disabledParsers = [];
43
44
    /**
45
     * create builder instance
46
     *
47
     * @return static
48
     */
49
    public static function create()
50
    {
51
        return new static;
52
    }
53
54
    /**
55
     * @return AbstractContext
56
     */
57
    public function getContext()
58
    {
59
        return $this->context;
60
    }
61
62
    /**
63
     * @param AbstractContext $context
64
     *
65
     * @return $this
66
     */
67
    public function setContext($context)
68
    {
69
        $this->context = $context;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return array|Parser\ValueParserInterface[]
76
     */
77
    public function getParsers()
78
    {
79
        return $this->parsers;
80
    }
81
82
    /**
83
     * @param ValueParserInterface $parser
84
     *
85
     * @return $this
86
     */
87
    public function addParser(ValueParserInterface $parser)
88
    {
89
        $this->parsers[$parser->getName()] = $parser;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param string|ValueParserInterface $parser
96
     *
97
     * @return boolean
98
     */
99
    public function hasParser($parser)
100
    {
101
        if ($parser instanceof ValueParserInterface) {
102
            $parser = $parser->getName();
103
        }
104
105
        return array_key_exists($parser, $this->parsers);
106
    }
107
108
    /**
109
     * disableParser
110
     *
111
     * @param string|ValueParserInterface $parser
112
     *
113
     * @return $this
114
     */
115
    public function disableParser($parser)
116
    {
117
        if ($parser instanceof ValueParserInterface) {
118
            $parser = $parser->getName();
119
        }
120
121
        if (is_string($parser)) {
122
            $this->disabledParsers[] = $parser;
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function getDisabledParsers()
132
    {
133
        return $this->disabledParsers;
134
    }
135
136
    /**
137
     * @param string|ValueParserInterface $parser
138
     *
139
     * @return $this
140
     */
141
    public function removeParser($parser)
142
    {
143
        if ($parser instanceof ValueParserInterface) {
144
            $parser = $parser->getName();
145
        }
146
147
        if (is_string($parser) && $this->hasParser($parser)) {
148
            unset($this->parsers[$parser]);
149
        }
150
151
        return $this;
152
    }
153
154
    /**
155
     * @param array|Parser\ValueParserInterface[] $parsers
156
     *
157
     * @return $this
158
     */
159 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...
160
    {
161
        $this->parsers = [];
162
        foreach ($parsers as $parser) {
163
            if ($parser instanceof ValueParserInterface) {
164
                $this->parsers[$parser->getName()] = $parser;
165
            }
166
        }
167
168
        return $this;
169
    }
170
171
    /**
172
     * @return PropertyMatcherInterface
173
     */
174
    public function getMatcher()
175
    {
176
        return $this->matcher;
177
    }
178
179
    /**
180
     * @param PropertyMatcherInterface $matcher
181
     *
182
     * @return $this
183
     */
184
    public function setMatcher(PropertyMatcherInterface $matcher)
185
    {
186
        $this->matcher = $matcher;
187
188
        return $this;
189
    }
190
191
    /**
192
     * @param AbstractContext $context
193
     */
194
    protected function prepareContext(AbstractContext $context)
195
    {
196
        //defaults
197
        $context->setMatcher($this->getMatcher() ?: new CamelizeMatcher());
198
        $context->setParsers(
199
            [
200
                new StringParser(),
201
                new BooleanParser(),
202
                new IntegerParser(),
203
                new FloatParser(),
204
                new DateTimeParser(),
205
                new ObjectParser($context)
206
            ]
207
        );
208
209
        //add custom parsers
210
        foreach ($this->getParsers() as $parser) {
211
            if ($parser instanceof ValueParserInterface) {
212
                $context->prependParser($parser);
213
            }
214
        }
215
216
        //disable parsers
217
        foreach ($this->getDisabledParsers() as $disabledParser) {
218
            $actualParsers = $context->getParsers();
219
            if (array_key_exists($disabledParser, $actualParsers)) {
220
                unset($actualParsers[$disabledParser]);
221
            }
222
            $context->setParsers($actualParsers);
223
        }
224
    }
225
226
    abstract public function build();
227
}