ParsersAwareTrait::getParsers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the rafrsr/lib-array2object package.
5
 *
6
 * (c) Rafael SR <https://github.com/rafrsr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Rafrsr\LibArray2Object\Traits;
12
13
use Rafrsr\LibArray2Object\Parser;
14
use Rafrsr\LibArray2Object\Parser\ValueParserInterface;
15
16
trait ParsersAwareTrait
17
{
18
    /**
19
     * @var array|ValueParserInterface[]
20
     */
21
    private $parsers = [];
22
23
    /**
24
     * @return array|Parser\ValueParserInterface[]
25
     */
26
    public function getParsers()
27
    {
28
        return $this->parsers;
29
    }
30
31
    /**
32
     * @param array|Parser\ValueParserInterface[] $parsers
33
     *
34
     * @return $this
35
     */
36
    public function setParsers($parsers)
37
    {
38
        $this->parsers = [];
39
        foreach ($parsers as $parser) {
40
            if ($parser instanceof ValueParserInterface) {
41
                $this->parsers[$parser->getName()] = $parser;
42
            }
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * Append parser to the list of parsers, LOW priority.
50
     *
51
     * @param ValueParserInterface $parser
52
     *
53
     * @return $this
54
     */
55
    public function appendParser(ValueParserInterface $parser)
56
    {
57
        $this->parsers[$parser->getName()] = $parser;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Prepend parser to list of parsers, HIGH priority.
64
     *
65
     * @param ValueParserInterface $parser
66
     *
67
     * @return $this
68
     */
69
    public function prependParser(ValueParserInterface $parser)
70
    {
71
        $parsers = $this->parsers;
72
        if (array_key_exists($parser->getName(), $parsers)) {
73
            unset($parsers[$parser->getName()]);
74
        }
75
76
        $this->parsers = array_merge([$parser->getName() => $parser], $parsers);
77
78
        return $this;
79
    }
80
}
81