1: <?php
2:
3: namespace PHPixie\ORM\Models;
4:
5: abstract class Model
6: {
7: protected $models;
8: protected $configs;
9: protected $wrappers;
10:
11: protected $modelConfigs = array();
12: protected $wrapped = array();
13:
14: public function __construct($models, $configs)
15: {
16: $this->models = $models;
17: $this->configs = $configs;
18: $this->wrappers = $this->models->wrappers();
19: }
20:
21: public function config($modelName)
22: {
23: if(!array_key_exists($modelName, $this->modelConfigs)) {
24: $configSlice = $this->models->modelConfigSlice($modelName);
25:
26: $modelType = $configSlice->get('type', 'database');
27: if($modelType !== $this->type()) {
28: throw new \PHPixie\ORM\Exception\Model("The type of '$modelName' model is '$modelType', expected {$this->type()}");
29: }
30:
31: $this->modelConfigs[$modelName] = $this->buildConfig($modelName, $configSlice);
32: }
33:
34: return $this->modelConfigs[$modelName];
35: }
36:
37: protected function hasWrapper($type, $modelName)
38: {
39: if(!array_key_exists($type, $this->wrapped)) {
40: if($this->wrappers !== null) {
41: $this->wrapped[$type] = array_fill_keys($this->wrappers->$type(), true);
42: }else{
43: $this->wrapped[$type] = array();
44: }
45: }
46:
47: return array_key_exists($modelName, $this->wrapped[$type]);
48: }
49:
50: abstract protected function buildConfig($modelName, $configSlice);
51:
52: abstract public function type();
53: }