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

ParsersAwareTrait::appendParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
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\Parser\ValueParserInterface;
13
14
Trait ParsersAwareTrait
15
{
16
    /**
17
     * @var array|ValueParserInterface[]
18
     */
19
    private $parsers = [];
20
21
22
    /**
23
     * @return array|Parser\ValueParserInterface[]
24
     */
25
    public function getParsers()
26
    {
27
        return $this->parsers;
28
    }
29
30
    /**
31
     * @param array|Parser\ValueParserInterface[] $parsers
32
     *
33
     * @return $this
34
     */
35
    public function setParsers($parsers)
36
    {
37
        $this->parsers = [];
38
        foreach ($parsers as $parser) {
39
            if ($parser instanceof ValueParserInterface) {
40
                $this->parsers[$parser->getName()] = $parser;
41
            }
42
        }
43
44
        return $this;
45
    }
46
47
    /**
48
     * Append parser to the list of parsers, LOW priority
49
     *
50
     * @param ValueParserInterface $parser
51
     *
52
     * @return $this
53
     */
54
    public function appendParser(ValueParserInterface $parser)
55
    {
56
        $this->parsers[$parser->getName()] = $parser;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Prepend parser to list of parsers, HIGH priority
63
     *
64
     * @param ValueParserInterface $parser
65
     *
66
     * @return $this
67
     */
68
    public function prependParser(ValueParserInterface $parser)
69
    {
70
        $parsers = $this->parsers;
71
        if (array_key_exists($parser->getName(), $parsers)) {
72
            unset($parsers[$parser->getName()]);
73
        }
74
75
        $this->parsers = array_merge([$parser->getName() => $parser], $parsers);
76
77
        return $this;
78
    }
79
}