|
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
|
|
|
* <ul> |
|
32
|
|
|
* <li>@Annotation(Class\Literal, 2)</li> |
|
33
|
|
|
* <li>@Annotation(class = Class\Literal, items = 2)</li> |
|
34
|
|
|
* </ul> |
|
35
|
|
|
* Example of use: |
|
36
|
|
|
* ParamsExpander::expand($annotation, ['class', 'items']) |
|
37
|
|
|
* This will assign `$annotation->class` and `$annotation->items` with named or anonymous params (based on order of params). |
|
38
|
|
|
* @param AnnotationInterface $annotation |
|
39
|
|
|
* @param string[] $params List of parameters names in order. |
|
40
|
|
|
* @param mixed[] $values Values used to expand params, if not set `$annotation->value` will be used if available. |
|
41
|
|
|
* @return mixed[] Expanded params |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public static function expand(AnnotationInterface $annotation, $params, $values = []) |
|
44
|
|
|
{ |
|
45
|
2 |
|
if (!empty($annotation->value)) |
|
|
|
|
|
|
46
|
2 |
|
{ |
|
47
|
2 |
|
$values = $values? : (array) $annotation->value; |
|
48
|
|
|
} |
|
49
|
2 |
|
$data = []; |
|
50
|
2 |
|
foreach ($params as $key => $name) |
|
51
|
2 |
|
{ |
|
52
|
2 |
|
if (isset($values[$key])) |
|
53
|
2 |
|
{ |
|
54
|
|
|
$data[$name] = $values[$key]; |
|
55
|
2 |
|
unset($values[$key]); |
|
56
|
2 |
|
continue; |
|
57
|
2 |
|
} |
|
58
|
2 |
|
if (isset($values[$name])) |
|
59
|
2 |
|
{ |
|
60
|
2 |
|
$data[$name] = $values[$name]; |
|
61
|
|
|
} |
|
62
|
|
|
if (isset($annotation->$name)) |
|
63
|
2 |
|
{ |
|
64
|
2 |
|
$data[$name] = $annotation->$name; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
return $data; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: