Passed
Push — master ( 562277...03179d )
by Richard
01:52
created

Entity::addRelationData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 3
nop 4
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
1
<?php
2
// C
3
namespace Maphper\Lib;
4
class Entity {
5
	//Creates an object of type $className and populates it with data
6
7
	private $className;
8
	private $parent;
9
10
	public function __construct(\Maphper\Maphper $parent, $className = null) {
11
        $this->parent = $parent;
12
		$this->className = $className;
13
	}
14
15
	public function create($data = [], $relations = [], $siblings = []) {
16
		$obj = (is_callable($this->className)) ? call_user_func($this->className) : new $this->className;
17
		$writer = new VisibilityOverride($obj);
18
		$writer->write($data);
19
		return $this->wrap($relations, $obj, $siblings);
20
	}
21
22
	public function wrap($relations, $object, $siblings = []) {
23
		//see if any relations need overwriting
24
		foreach ($relations as $name => $relation) $this->addRelationData($object, $name, $relation, $siblings);
25
		return $object;
26
	}
27
28
    private function addRelationData($object, $name, $relation, $siblings) {
29
        if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) {
30
            //After overwriting the relation, does the parent object ($object) need overwriting as well?
31
            if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object;
32
        }
33
34
        $object->$name = $relation->getData($object, $siblings);
35
    }
36
}
37