Completed
Push — master ( 0a636a...80871c )
by Peter
15:13
created

ParamsExpander   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 0
cbo 0
dl 0
loc 47
ccs 20
cts 22
cp 0.9091
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C expand() 0 26 7
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>&commat;Annotation(Class\Literal, 2)</li>
33
	 * <li>&commat;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 1
	public static function expand(AnnotationInterface $annotation, $params, $values = [])
44
	{
45 1
		if (!empty($annotation->value))
46 1
		{
47 1
			$values = $values? : (array) $annotation->value;
48 1
		}
49 1
		$data = [];
50 1
		foreach ($params as $key => $name)
51
		{
52 1
			if (isset($values[$key]))
53 1
			{
54 1
				$data[$name] = $values[$key];
55 1
				unset($values[$key]);
56 1
				continue;
57
			}
58 1
			if (isset($values[$name]))
59 1
			{
60 1
				$data[$name] = $values[$name];
61 1
			}
62 1
			if (isset($annotation->$name))
63 1
			{
64
				$data[$name] = $annotation->$name;
65
			}
66 1
		}
67 1
		return $data;
68
	}
69
70
}
71