Completed
Push — master ( 282925...bb1873 )
by Rafael
03:01
created

AbstractBuilder::getContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Parser\BooleanParser;
13
use Rafrsr\LibArray2Object\Parser\DateTimeParser;
14
use Rafrsr\LibArray2Object\Parser\FloatParser;
15
use Rafrsr\LibArray2Object\Parser\IntegerParser;
16
use Rafrsr\LibArray2Object\Parser\ObjectParser;
17
use Rafrsr\LibArray2Object\Parser\StringParser;
18
use Rafrsr\LibArray2Object\Parser\ValueParserInterface;
19
20
abstract class AbstractBuilder
21
{
22
    use ParsersAwareTrait;
23
24
    /**
25
     * @var AbstractContext
26
     */
27
    private $context;
28
29
    /**
30
     * @var array
31
     */
32
    private $disabledParsers = [];
33
34
    /**
35
     * create builder instance
36
     *
37
     * @return static
38
     */
39
    public static function create()
40
    {
41
        return new static;
42
    }
43
44
    /**
45
     * @return AbstractContext
46
     */
47
    public function getContext()
48
    {
49
        return $this->context;
50
    }
51
52
    /**
53
     * @param AbstractContext $context
54
     *
55
     * @return $this
56
     */
57
    public function setContext($context)
58
    {
59
        $this->context = $context;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param ValueParserInterface $parser
66
     *
67
     * @return $this
68
     */
69
    public function addParser(ValueParserInterface $parser)
70
    {
71
        $this->parsers[$parser->getName()] = $parser;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param string|ValueParserInterface $parser
78
     *
79
     * @return boolean
80
     */
81
    public function hasParser($parser)
82
    {
83
        if ($parser instanceof ValueParserInterface) {
84
            $parser = $parser->getName();
85
        }
86
87
        return array_key_exists($parser, $this->parsers);
88
    }
89
90
    /**
91
     * disableParser
92
     *
93
     * @param string|ValueParserInterface $parser
94
     *
95
     * @return $this
96
     */
97
    public function disableParser($parser)
98
    {
99
        if ($parser instanceof ValueParserInterface) {
100
            $parser = $parser->getName();
101
        }
102
103
        if (is_string($parser)) {
104
            $this->disabledParsers[] = $parser;
105
        }
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getDisabledParsers()
114
    {
115
        return $this->disabledParsers;
116
    }
117
118
    /**
119
     * @param string|ValueParserInterface $parser
120
     *
121
     * @return $this
122
     */
123
    public function removeParser($parser)
124
    {
125
        if ($parser instanceof ValueParserInterface) {
126
            $parser = $parser->getName();
127
        }
128
129
        if (is_string($parser) && $this->hasParser($parser)) {
130
            unset($this->parsers[$parser]);
131
        }
132
133
        return $this;
134
    }
135
136
    /**
137
     * @param AbstractContext $context
138
     */
139
    protected function prepareContext(AbstractContext $context)
140
    {
141
        //defaults
142
        $context->setParsers(
143
            [
144
                new StringParser(),
145
                new BooleanParser(),
146
                new IntegerParser(),
147
                new FloatParser(),
148
                new DateTimeParser(),
149
                new ObjectParser($context)
150
            ]
151
        );
152
153
        //add custom parsers
154
        foreach ($this->getParsers() as $parser) {
155
            if ($parser instanceof ValueParserInterface) {
156
                $context->prependParser($parser);
157
            }
158
        }
159
160
        //disable parsers
161
        foreach ($this->getDisabledParsers() as $disabledParser) {
162
            $actualParsers = $context->getParsers();
163
            if (array_key_exists($disabledParser, $actualParsers)) {
164
                unset($actualParsers[$disabledParser]);
165
            }
166
            $context->setParsers($actualParsers);
167
        }
168
    }
169
170
    abstract public function build();
171
}