1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL, Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/addendum |
7
|
|
|
* @licence AGPL, Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes) |
9
|
|
|
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes) |
10
|
|
|
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser) |
11
|
|
|
* @link http://maslosoft.com/addendum/ - maslosoft addendum |
12
|
|
|
* @link https://code.google.com/p/addendum/ - original addendum project |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Maslosoft\Addendum\Helpers; |
16
|
|
|
|
17
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotationInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Helper class for easier annotation params setting |
21
|
|
|
* |
22
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
23
|
|
|
*/ |
24
|
|
|
class ParamsExpander |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Expand params to annotation values. |
29
|
|
|
* When annotation is used with multi top values, this will help assign this values to annotation. |
30
|
|
|
* Examples of multi valued params include: |
31
|
|
|
* |
32
|
|
|
* ``` |
33
|
|
|
* @Annotation(Class\Literal, 2) |
34
|
|
|
* @Annotation(class = Class\Literal, items = 2) |
35
|
|
|
* ``` |
36
|
|
|
* |
37
|
|
|
* Example of use: |
38
|
|
|
* ``` |
39
|
|
|
* $data = ParamsExpander::expand($annotation, ['class', 'items']) |
40
|
|
|
* ``` |
41
|
|
|
* This will assign `$annotation->class` and `$annotation->items` with named or anonymous params (based on order of params). |
42
|
|
|
* @param AnnotationInterface $annotation |
43
|
|
|
* @param string[] $params List of parameters names in order. |
44
|
|
|
* @param mixed[] $values Values used to expand params, if not set `$annotation->value` will be used if available. |
45
|
|
|
* @return mixed[] Expanded params |
46
|
|
|
*/ |
47
|
1 |
|
public static function expand(AnnotationInterface $annotation, $params, $values = []) |
48
|
|
|
{ |
49
|
1 |
|
if (!empty($annotation->value)) |
50
|
1 |
|
{ |
51
|
1 |
|
$values = $values ?: (array) $annotation->value; |
52
|
1 |
|
} |
53
|
1 |
|
$data = []; |
54
|
1 |
|
foreach ($params as $key => $name) |
55
|
|
|
{ |
56
|
1 |
|
if (is_array($values) && array_key_exists($key, $values)) |
57
|
1 |
|
{ |
58
|
1 |
|
$data[$name] = $values[$key]; |
59
|
1 |
|
unset($values[$key]); |
60
|
1 |
|
continue; |
61
|
|
|
} |
62
|
1 |
|
if (is_array($values) && array_key_exists($name, $values)) |
63
|
1 |
|
{ |
64
|
1 |
|
$data[$name] = $values[$name]; |
65
|
1 |
|
} |
66
|
1 |
|
if (isset($annotation->$name)) |
67
|
1 |
|
{ |
68
|
|
|
$data[$name] = $annotation->$name; |
69
|
|
|
} |
70
|
1 |
|
} |
71
|
1 |
|
return $data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|