1 | <?php |
||
9 | class Meta implements Contracts\Meta |
||
10 | { |
||
11 | protected $manager; |
||
12 | protected $property = []; |
||
13 | protected $indexes = []; |
||
14 | protected $types = []; |
||
15 | protected $instances = []; |
||
16 | |||
17 | 64 | public function __construct(Contracts\Manager $manager, array $data = null) |
|
18 | { |
||
19 | 64 | $this->manager = $manager; |
|
20 | |||
21 | 64 | if($data) { |
|
22 | 1 | $this->property = $data['property']; |
|
23 | 1 | $this->indexes = $data['indexes']; |
|
24 | 1 | $this->types = $data['types']; |
|
25 | |||
26 | 1 | } else { |
|
27 | 64 | $this->collectData(); |
|
28 | } |
||
29 | 64 | } |
|
30 | |||
31 | /** |
||
32 | * @return Type |
||
33 | */ |
||
34 | 64 | public function get($type) |
|
35 | { |
||
36 | 64 | if (!array_key_exists($type, $this->instances)) { |
|
37 | 64 | $spaceId = $this->manager->getSchema()->getSpaceId($type); |
|
38 | 64 | if (!$spaceId) { |
|
39 | 1 | throw new LogicException("Type $type not exists"); |
|
40 | } |
||
41 | |||
42 | 64 | $this->instances[$type] = new Type( |
|
43 | 64 | $this->manager, $type, |
|
44 | 64 | $this->property[$spaceId], |
|
45 | 64 | $this->types[$spaceId], |
|
46 | 64 | $this->indexes[$spaceId] |
|
47 | 64 | ); |
|
48 | 64 | } |
|
49 | |||
50 | 64 | return $this->instances[$type]; |
|
51 | } |
||
52 | |||
53 | 4 | public function has($type) |
|
68 | |||
69 | 4 | public function remove($type) |
|
70 | { |
||
71 | 4 | $other = $this->manager->get('property')->find(['type' => $type]); |
|
|
|||
72 | 4 | if (count($other)) { |
|
73 | 1 | $name = $this->manager->getSchema()->getSpaceName($other[0]->space); |
|
74 | 1 | throw new Exception("Space $name references ".$type); |
|
75 | } |
||
76 | 3 | $instance = $this->get($type); |
|
77 | 3 | $rows = $instance->getSpace()->select([])->getData(); |
|
78 | 3 | if (count($rows)) { |
|
79 | 1 | throw new Exception("Can't remove non-empty space $type"); |
|
80 | } |
||
81 | |||
82 | 2 | $indexes = array_reverse(array_keys($instance->getIndexes())); |
|
83 | 2 | foreach ($indexes as $index) { |
|
84 | 2 | $instance->dropIndex($index); |
|
85 | 2 | } |
|
86 | |||
87 | 2 | foreach (array_reverse($instance->getProperties()) as $property) { |
|
88 | 2 | $instance->removeProperty($property); |
|
89 | 2 | } |
|
90 | |||
91 | 2 | $sq = $this->manager->get('sequence')->findOne(['space' => $instance->getSpaceId()]); |
|
92 | 2 | if ($sq) { |
|
93 | 1 | $this->manager->remove($sq); |
|
94 | 1 | $this->manager->get('sequence')->flushCache(); |
|
95 | 1 | } |
|
96 | |||
97 | 2 | $this->manager->getSchema()->dropSpace($type); |
|
98 | 2 | unset($this->instances[$type]); |
|
99 | |||
100 | 2 | $this->manager->forgetRepository($type); |
|
101 | 2 | } |
|
102 | |||
103 | /** |
||
104 | * @return Type |
||
105 | */ |
||
106 | 64 | public function create($type, array $fields = null) |
|
107 | { |
||
108 | 64 | if ($this->manager->getSchema()->hasSpace($type)) { |
|
109 | 1 | throw new LogicException("Type $type exists"); |
|
110 | } |
||
111 | |||
112 | 64 | $this->manager->getSchema()->createSpace($type); |
|
113 | |||
114 | 64 | $instance = new Type($this->manager, $type, [], [], []); |
|
115 | |||
116 | 64 | $instance->addProperty('id'); |
|
117 | 64 | $instance->addIndex('id'); |
|
118 | |||
119 | 64 | if ($fields) { |
|
120 | 64 | foreach ($fields as $index => $field) { |
|
121 | 64 | if ($field instanceof Contracts\Type) { |
|
122 | 5 | if (!is_numeric($index)) { |
|
123 | 1 | $instance->reference($field, $index); |
|
124 | 1 | } else { |
|
125 | 4 | $instance->reference($field); |
|
126 | } |
||
127 | 5 | } else { |
|
128 | 64 | $instance->addProperty($field); |
|
129 | } |
||
130 | 64 | } |
|
131 | 64 | } |
|
132 | 64 | $this->instances[$type] = $instance; |
|
133 | |||
134 | 64 | return $instance; |
|
135 | } |
||
136 | |||
137 | 1 | public function setConvention(Contracts\Convention $convention) |
|
143 | |||
144 | 64 | public function getConvention() |
|
145 | { |
||
146 | 64 | if (!isset($this->convention)) { |
|
147 | 64 | $this->convention = new Convention(); |
|
153 | |||
154 | 64 | private function collectData() |
|
187 | |||
188 | 1 | public function toArray() |
|
198 | } |
||
199 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: