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\PropertyMatcherInterface; |
13
|
|
|
use Rafrsr\LibArray2Object\Parser\ValueParserInterface; |
14
|
|
|
|
15
|
|
|
abstract class AbstractContext |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array|ValueParserInterface[] |
19
|
|
|
*/ |
20
|
|
|
private $parsers = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PropertyMatcherInterface |
24
|
|
|
*/ |
25
|
|
|
private $matcher; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array|Parser\ValueParserInterface[] |
29
|
|
|
*/ |
30
|
|
|
public function getParsers() |
31
|
|
|
{ |
32
|
|
|
return $this->parsers; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array|Parser\ValueParserInterface[] $parsers |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function setParsers($parsers) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$this->parsers = []; |
43
|
|
|
foreach ($parsers as $parser) { |
44
|
|
|
if ($parser instanceof ValueParserInterface) { |
45
|
|
|
$this->parsers[$parser->getName()] = $parser; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Append parser to the list of parsers, LOW priority |
54
|
|
|
* |
55
|
|
|
* @param ValueParserInterface $parser |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function appendParser(ValueParserInterface $parser) |
60
|
|
|
{ |
61
|
|
|
$this->parsers[$parser->getName()] = $parser; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Prepend parser to list of parsers, HIGH priority |
68
|
|
|
* |
69
|
|
|
* @param ValueParserInterface $parser |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function prependParser(ValueParserInterface $parser) |
74
|
|
|
{ |
75
|
|
|
$parsers = $this->parsers; |
76
|
|
|
if (array_key_exists($parser->getName(), $parsers)) { |
77
|
|
|
unset($parsers[$parser->getName()]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->parsers = array_merge([$parser->getName() => $parser], $parsers); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return PropertyMatcherInterface |
87
|
|
|
*/ |
88
|
|
|
public function getMatcher() |
89
|
|
|
{ |
90
|
|
|
return $this->matcher; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param PropertyMatcherInterface $matcher |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function setMatcher($matcher) |
99
|
|
|
{ |
100
|
|
|
$this->matcher = $matcher; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
} |
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.