1: <?php
2:
3: namespace PHPixie\ORM\Relationships\Relationship\Implementation\Preloader;
4:
5: abstract class Result extends \PHPixie\ORM\Relationships\Relationship\Implementation\Preloader
6: {
7: protected $side;
8: protected $modelConfig;
9: protected $result;
10: protected $loader;
11:
12: protected $idOffsets;
13: protected $mapped = false;
14:
15:
16: public function __construct($side, $modelConfig, $result, $loader)
17: {
18: $this->side = $side;
19: $this->modelConfig = $modelConfig;
20: $this->result = $result;
21: $this->loader = $loader;
22: }
23:
24: public function getEntity($id)
25: {
26: $this->ensureMapped();
27: return $this->loader->getByOffset($this->idOffsets[$id]);
28: }
29:
30: public function loadProperty($property)
31: {
32: $this->ensureMapped();
33: $entity = $property->entity();
34: $property->setValue($this->getMappedFor($entity));
35: }
36:
37: protected function ensureMapped()
38: {
39: if ($this->mapped)
40: return;
41:
42: $this->mapItems();
43: $this->mapped = true;
44:
45: }
46:
47: protected function mapIdOffsets()
48: {
49: $idField = $this->modelConfig->idField;
50: $this->idOffsets = array_flip($this->result->getField($idField));
51: }
52:
53: abstract protected function mapItems();
54: abstract protected function getMappedFor($entity);
55: }
56: