Completed
Push — master ( 7d4d47...4dc2a1 )
by Peter
08:01
created

ParamsExpander   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 51
ccs 20
cts 22
cp 0.9091
rs 10
c 3
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D expand() 0 26 9
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