1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix\Plugin; |
14
|
|
|
|
15
|
|
|
use Apix\Entity; |
16
|
|
|
|
17
|
|
|
abstract class PluginAbstractEntity extends PluginAbstract |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The annotation key string for thsi plugin, e.g. "api_NAME". |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $annotation; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Holds this plugin entity. |
27
|
|
|
* @var Entity |
28
|
|
|
*/ |
29
|
|
|
protected $entity; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Holds all the extracted subtags. |
33
|
|
|
* var array|null |
34
|
|
|
*/ |
35
|
|
|
private $subtags_extract = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns the values of the specified subtag. |
39
|
|
|
* |
40
|
|
|
* @param string $key The subtag $key to retrieve |
41
|
|
|
* @param string|null $default The default value |
42
|
|
|
* @return array|null An indexed array of values, or null |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function getSubTagValues($key, array $default=null) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$tags = $this->extractSubTags(); |
47
|
|
|
$k = array_search($key, $tags['keys']); |
48
|
|
|
|
49
|
|
|
// return false === $k |
|
|
|
|
50
|
|
|
// ? $default |
51
|
|
|
// : array_map('trim', explode(',', $tags['values'][$k])); |
|
|
|
|
52
|
|
|
return false === $k ? $default : explode(',', $tags['values'][$k]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the boolean value of the specified subtag. |
57
|
|
|
* |
58
|
|
|
* @param string $key The subtag $key to retrieve |
59
|
|
|
* @param string|null $default The default value |
60
|
|
|
* @return boolean|null |
61
|
|
|
*/ |
62
|
|
|
public function getSubTagBool($key, $default=null) |
63
|
|
|
{ |
64
|
|
|
$tags = $this->extractSubTags(); |
65
|
|
|
$k = array_search($key, $tags['keys']); |
66
|
|
|
|
67
|
|
|
$value = $k === false |
68
|
|
|
? ( $default ? (bool) $default : null) |
69
|
|
|
: $tags['values'][$k]; |
70
|
|
|
|
71
|
|
|
return null === $value |
72
|
|
|
? null |
73
|
|
|
: filter_var($value, FILTER_VALIDATE_BOOLEAN); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the value of the specified subtag. |
78
|
|
|
* |
79
|
|
|
* @param string $key The subtag $key to retrieve |
80
|
|
|
* @param string|null $default The default value |
81
|
|
|
* @return string|null |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function getSubTagString($key, $default=null) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$tags = $this->extractSubTags(); |
86
|
|
|
$k = array_search($key, $tags['keys']); |
87
|
|
|
|
88
|
|
|
return $k === false ? $default : (string) $tags['values'][$k]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Extracts all the subtags. |
93
|
|
|
* |
94
|
|
|
* @return array An associative array |
95
|
|
|
*/ |
96
|
|
|
public function extractSubTags() |
97
|
|
|
{ |
98
|
|
|
// if (null === $this->subtags_extract) { |
|
|
|
|
99
|
|
|
$lines = $this->entity->getAnnotationValue($this->annotation); |
100
|
|
|
|
101
|
|
|
if (!is_array($lines)) { |
102
|
|
|
$lines = array($lines); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($lines as $line) { |
106
|
|
|
preg_match_all( |
107
|
|
|
'/(?P<keys>[^=\s]+)=(?P<values>[^=\s]+)/i', |
108
|
|
|
$line, |
109
|
|
|
$this->subtags_extract |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
// } |
113
|
|
|
return $this->subtags_extract; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets this plugin entity. |
118
|
|
|
* |
119
|
|
|
* @param Entity $entity |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function setEntity(Entity $entity) |
123
|
|
|
{ |
124
|
|
|
$this->entity = $entity; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets this plugin entity. |
129
|
|
|
* |
130
|
|
|
* @return Entity |
131
|
|
|
*/ |
132
|
|
|
public function getEntity() |
133
|
|
|
{ |
134
|
|
|
return $this->entity; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets the annotation string for this plugin. |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function setAnnotation($string) |
143
|
|
|
{ |
144
|
|
|
$this->annotation = $string; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
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.