Completed
Push — master ( 674f63...1322e1 )
by Nazar
04:59
created

Properties_getter::get_property_items()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
nc 8
nop 2
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 5
rs 8.8571
c 1
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
10
trait Properties_getter {
11
	/**
12
	 * Get items for specified property (can get single property or array of properties)
13
	 *
14
	 * @param string              $property
15
	 * @param string[]|string[][] $items Typically obtained as `...$items`
16
	 *
17
	 * @return mixed|mixed[]|null Property items (or associative array of items) if exists or `null` otherwise (in case if `$item` is an array even one
18
	 *                            missing key will cause the whole thing to fail)
19
	 */
20 36
	protected function get_property_items ($property, $items) {
21 36
		if (count($items) === 1) {
22 32
			$items = $items[0];
23
		}
24 36
		$property = $this->$property;
25
		/**
26
		 * @var string|string[] $items
27
		 */
28 36
		if (is_array($items)) {
29 10
			$result = [];
30 10
			foreach ($items as &$i) {
31 10
				if (!array_key_exists($i, $property)) {
32 6
					return null;
33
				}
34 10
				$result[$i] = $property[$i];
35
			}
36 10
			return $result;
37
		}
38 32
		return @$property[$items];
39
	}
40
}
41