Entity::wrap()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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