Test Setup Failed
Push — master ( 4c62c0...259020 )
by Bruce Pinheiro de
02:12
created

PropertyHandler::fillProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
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
	function fillProperties(array $data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
	{
19
		foreach ($data as $p => $v)
20
		{
21
			$this->fillProperty($p, $v);
22
		}
23
	}
24
25
	/**
26
	 * @param string $property
27
	 * @param mixed $value
28
	 */
29
	function fillProperty(string $property, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
	{
31
		$property = lcfirst(str_replace('_', '', ucwords($property, '_')));
32
		if(property_exists(__CLASS__, $property))
33
		{
34
			$method = sprintf('set%s', ucfirst($property));
35
			$this->{$method}($value);
36
		}
37
	}
38
39
	/**
40
	 * @return array
41
	 */
42
	function getPropertyArray(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
	{
44
		$reflection = new \ReflectionClass(__CLASS__);
45
		$properties = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED);
46
		$data = [];
47
		foreach($properties as $property){
48
			$prop_name = $property->getName();
49
			$method = sprintf('get%s', ucfirst($prop_name));
50
			$form_name = strtolower(preg_replace('/[A-Z]/', '_$0', $prop_name));
51
			if($reflection->hasMethod($method)){
52
				$data[$form_name] = $this->{$method}();
53
			}
54
		}
55
		return $data;
56
	}
57
58
}
59