Entity   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 5 2
A __construct() 0 3 1
A addRelationData() 0 7 4
A wrap() 0 4 2
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