Issues (29)

Maphper/Relation/One.php (3 issues)

1
<?php
2
namespace Maphper\Relation;
3
class One implements \Maphper\Relation {
4
	private $mapper;
5
	private $parentField;
6
	private $localField;
7
	private $parentObject;
8
	private $data;
9
	private $siblings = [];
10
11
	public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $criteria = []) {
12
        if ($criteria) $mapper = $mapper->filter($this->criteira);
0 ignored issues
show
Bug Best Practice introduced by
The expression $criteria of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
The method filter() does not exist on Maphper\Maphper. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

12
        if ($criteria) /** @scrutinizer ignore-call */ $mapper = $mapper->filter($this->criteira);
Loading history...
Bug Best Practice introduced by
The property criteira does not exist on Maphper\Relation\One. Since you implemented __get, consider adding a @property annotation.
Loading history...
13
		$this->mapper = $mapper;
14
		$this->parentField = $parentField;
15
		$this->localField = $localField;
16
	}
17
18
	public function getData($parentObject, &$siblings = null) {
19
		//Don't actually fetch the related data, return an instance of $this that will lazy load data when __get is called
20
		$clone = clone $this;
21
		$clone->parentObject = $parentObject;
22
		$siblings[] = $clone;
23
		$clone->siblings = $siblings;
24
25
	//	var_dump($siblings);
26
		return $clone;
27
	}
28
29
	private function lazyLoad() {
30
		if (!isset($this->data)) {
31
32
			if ($this->parentObject == null) throw new \Exception('Error, no object set');
33
34
			$this->eagerLoad();
35
36
		}
37
		return $this->data;
38
	}
39
40
	private function eagerLoad() {
41
		$recordsToLoad = [];
42
		//Get a list of records by FK to eager load
43
		foreach ($this->siblings as $sibling) {
44
			if ($sibling->parentField === $this->parentField) // Ensure that it is only loading records from the same type of relation
45
				$recordsToLoad[] = $sibling->parentObject->{$sibling->parentField};
46
		}
47
48
		$recordsToLoad = array_unique($recordsToLoad);
49
		//Fetch the results so they're in the cache for the corresponding maphper object
50
		$results = $this->mapper->filter([$this->localField => $recordsToLoad]);
51
52
        $this->loadDataIntoSiblings($results);
53
	}
54
55
    private function loadDataIntoSiblings($results) {
56
        $cache = [];
57
		foreach ($results as $result) {
58
			$cache[$result->{$this->localField}] = $result;
59
		}
60
61
		foreach ($this->siblings as $sibling) {
62
            if ($sibling->parentField === $this->parentField &&
63
                isset($cache[$sibling->parentObject->{$this->parentField}]))$sibling->data = $cache[$sibling->parentObject->{$this->parentField}];
64
		}
65
		/*
66
		foreach ($this->siblings as $sibling) {
67
			if ($sibling->criteria) $sibling->data = $sibling->mapper->filter($sibling->criteria)->filter([$sibling->localField => $sibling->parentObject->{$sibling->parentField}])->item(0);
68
			else $sibling->data = $sibling->mapper->filter([$sibling->localField => $sibling->parentObject->{$this->parentField}])->item(0);
69
		}
70
		*/
71
    }
72
73
	public function __call($func, array $args = []) {
74
		if ($this->lazyLoad() == null) return '';
75
		return call_user_func_array([$this->lazyLoad(), $func], $args);
76
	}
77
78
	public function __get($name) {
79
		if ($this->lazyLoad()) return $this->lazyLoad()->$name;
80
        else return null;
81
	}
82
83
	public function __isset($name) {
84
		return isset($this->lazyLoad()->$name);
85
	}
86
87
	public function overwrite($parentObject, &$data) {
88
        $this->mapper[] = $data;
89
90
        if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) {
91
			$parentObject->{$this->parentField} = $data->{$this->localField};
92
			//Trigger an update of the parent object
93
			return true;
94
		}
95
	}
96
97
	public function getFilter($object) {
98
		return [$this->parentField => $object->{$this->localField}];
99
	}
100
}
101