1: <?php
2:
3: namespace PHPixie\ORM\Values;
4:
5: class Preload
6: {
7: protected $values;
8: protected $properties = array();
9:
10: public function __construct($values)
11: {
12: $this->values = $values;
13: }
14:
15: public function properties()
16: {
17: return array_values($this->properties);
18: }
19:
20: public function addProperty($property)
21: {
22: $this->properties[$property->propertyName()] = $property;
23: return $this;
24: }
25:
26: public function getProperty($name)
27: {
28: if(!array_key_exists($name, $this->properties))
29: return null;
30:
31: return $this->properties[$name];
32: }
33:
34: public function addPath($path)
35: {
36: $path = explode('.', $path);
37: return $this->addExplodedPath($path);
38: }
39:
40: public function addExplodedPath($explodedPath)
41: {
42: $propertyName = array_shift($explodedPath);
43: $property = $this->getProperty($propertyName);
44: if($property === null) {
45: $property = $this->values->cascadingPreloadProperty($propertyName);
46: $this->addProperty($property);
47: }
48:
49: if(!empty($explodedPath))
50: $property->preload()->addExplodedPath($explodedPath);
51:
52: return $this;
53: }
54:
55: public function add($item)
56: {
57: if(is_string($item))
58: return $this->addPath($item);
59:
60: if($item instanceof \PHPixie\ORM\Values\Preload\Property)
61: return $this->addProperty($item);
62:
63: throw new \PHPixie\ORM\Exception\Query("Only string and Property instances can be used.");
64: }
65:
66: }