1: <?php
2:
3: namespace PHPixie\ORM;
4:
5: class Maps
6: {
7: 8: 9:
10: protected $ormBuilder;
11: protected $configSlice;
12:
13: protected $maps = array(
14: 'relationship',
15: 'queryProperty',
16: 'entityProperty',
17: 'preload',
18: 'cascadeDelete'
19: );
20:
21: protected $mapInstances = null;
22:
23: protected $relationshipMap;
24: protected $entityPropertyMap;
25: protected $queryPropertyMap;
26:
27:
28: public function __construct($ormBuilder, $configSlice)
29: {
30: $this->ormBuilder = $ormBuilder;
31: $this->configSlice = $configSlice;
32: }
33:
34: 35: 36:
37: public function relationship()
38: {
39: return $this->getMap('relationship');
40: }
41:
42: 43: 44:
45: public function entityProperty()
46: {
47: return $this->getMap('entityProperty');
48: }
49:
50: 51: 52:
53: public function queryProperty()
54: {
55: return $this->getMap('queryProperty');
56: }
57:
58: 59: 60:
61: public function preload()
62: {
63: return $this->getMap('preload');
64: }
65:
66: 67: 68:
69: public function cascadeDelete()
70: {
71: return $this->getMap('cascadeDelete');
72: }
73:
74: protected function buildMaps()
75: {
76: $this->mapInstances = array();
77:
78: foreach($this->maps as $name)
79: {
80: $method = 'build'.ucfirst($name).'Map';
81: $this->mapInstances[$name] = $this->$method();
82: }
83: $this->addSidesFromConfig($this->configSlice);
84: }
85:
86: protected function addSidesFromConfig($configSlice)
87: {
88: $relationships = $this->ormBuilder->relationships();
89: foreach ($configSlice->keys() as $key) {
90: $relationshipConfig = $configSlice->slice($key);
91: $type = $relationshipConfig->getRequired('type');
92: $relationship = $relationships->get($type);
93: $sides = $relationship->getSides($relationshipConfig);
94:
95: foreach($sides as $side) {
96: $this->addSide($side);
97: }
98: }
99: }
100:
101: protected function addSide($side)
102: {
103: if($side instanceof \PHPixie\ORM\Relationships\Relationship\Side\Relationship) {
104: $this->mapInstances['relationship']->add($side);
105: }
106:
107: if($side instanceof \PHPixie\ORM\Relationships\Relationship\Side\Property\Entity) {
108: $this->mapInstances['entityProperty']->add($side);
109: }
110:
111: if($side instanceof \PHPixie\ORM\Relationships\Relationship\Side\Property\Query) {
112: $this->mapInstances['queryProperty']->add($side);
113: }
114:
115: if($side instanceof \PHPixie\ORM\Relationships\Relationship\Side\Preload) {
116: $this->mapInstances['preload']->add($side);
117: }
118:
119: if($side instanceof \PHPixie\ORM\Relationships\Relationship\Side\Cascade\Delete && $side->isDeleteHandled()) {
120: $this->mapInstances['cascadeDelete']->add($side);
121: }
122: }
123:
124: protected function getMap($name)
125: {
126: if($this->mapInstances === null) {
127: $this->buildMaps();
128: }
129:
130: return $this->mapInstances[$name];
131: }
132:
133: protected function buildRelationshipMap()
134: {
135: return new Maps\Map\Relationship();
136: }
137:
138: protected function buildEntityPropertyMap()
139: {
140: return new Maps\Map\Property\Entity(
141: $this->ormBuilder->relationships()
142: );
143: }
144:
145: protected function buildQueryPropertyMap()
146: {
147: return new Maps\Map\Property\Query(
148: $this->ormBuilder->relationships()
149: );
150: }
151:
152: protected function buildPreloadMap()
153: {
154: return new Maps\Map\Preload();
155: }
156:
157: protected function buildCascadeDeleteMap()
158: {
159: return new Maps\Map\Cascade\Delete();
160: }
161: }
162: