Entity::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace SpareParts\Pillar\Entity;
3
4
/**
5
 * Simple implementation of IEntity interface
6
 * Can be used as an example for your own, specific implementation
7
 * This is a bit reliant on entity factory used (factory calls contructor, so the format of the constructor must be the same as the factory uses)
8
 */
9
class Entity implements IEntity
10
{
11
	/**
12
	 * @var mixed[]
13
	 */
14
	private $_originalValues;
15
16
	/**
17
	 * @param array $data
18
	 */
19 3
	public function __construct(array $data = []) {
20 3
		$this->_originalValues = $data;
21 3
		foreach ($data as $propertyName => $propertyValue) {
22 2
			$this->{$propertyName} = $propertyValue;
23
		}
24 3
	}
25
26
	/**
27
	 * @param string[] $properties List of concerned properties
28
	 * @return mixed[]
29
	 */
30 2
	public function getChangedProperties($properties)
31
	{
32 2
		$changed = [];
33 2
		foreach ($properties as $property) {
34 2
			$originalValue = isset($this->_originalValues[$property]) ? $this->_originalValues[$property] : null;
35 2
			if ($originalValue !== $this->{$property}) {
36 2
				$changed[] = $property;
37
			}
38
		}
39 2
		return $changed;
40
	}
41
}
42