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\Traits; |
11
|
|
|
|
12
|
|
|
use Rafrsr\LibArray2Object\Parser; |
13
|
|
|
use Rafrsr\LibArray2Object\Parser\ValueParserInterface; |
14
|
|
|
|
15
|
|
|
Trait ParsersAwareTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array|ValueParserInterface[] |
19
|
|
|
*/ |
20
|
|
|
private $parsers = []; |
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
|
|
|
} |