PropertyHandler::fillProperties()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: brucepc
5
 * Date: 24/02/18
6
 * Time: 12:33
7
 */
8
9
namespace BPCI\SumUp\Traits;
10
11
12
trait PropertyHandler
13
{
14
	/**
15
	 * @param array $data
16
	 */
17 4
    public function fillProperties(array $data): void
18
	{
19 4
		foreach ($data as $p => $v)
20
		{
21 4
			$this->fillProperty($p, $v);
22
		}
23 4
	}
24
25
	/**
26
	 * @param string $property
27
	 * @param mixed $value
28
	 */
29 4
    public function fillProperty(string $property, $value): void
30
	{
31 4
		$property = lcfirst(str_replace('_', '', ucwords($property, '_')));
32 4
		if(property_exists(__CLASS__, $property))
33
		{
34 4
			$method = sprintf('set%s', ucfirst($property));
35 4
			$this->{$method}($value);
36
		}
37 4
	}
38
39
	/**
40
	 * @return array
41
	 */
42 1
    public function getPropertyArray(): array
43
	{
44 1
		$reflection = new \ReflectionClass(__CLASS__);
45 1
		$properties = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED);
46 1
		$data = [];
47 1
		foreach($properties as $property){
48 1
			$prop_name = $property->getName();
49 1
			$method = sprintf('get%s', ucfirst($prop_name));
50 1
			$form_name = strtolower(preg_replace('/[A-Z]/', '_$0', $prop_name));
51 1
			if($reflection->hasMethod($method)){
52 1
				$data[$form_name] = $this->{$method}();
53
			}
54
		}
55 1
		return $data;
56
	}
57
58
}
59