1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Schema; |
4
|
|
|
|
5
|
|
|
use Tarantool\Mapper\Contracts; |
6
|
|
|
use LogicException; |
7
|
|
|
|
8
|
|
|
class Meta implements Contracts\Meta |
9
|
|
|
{ |
10
|
|
|
protected $manager; |
11
|
|
|
protected $types = []; |
12
|
|
|
|
13
|
4 |
|
public function __construct(Contracts\Manager $manager) |
14
|
|
|
{ |
15
|
4 |
|
$this->manager = $manager; |
16
|
4 |
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return Type |
20
|
|
|
*/ |
21
|
4 |
|
public function get($type) |
22
|
|
|
{ |
23
|
4 |
|
if (!array_key_exists($type, $this->types)) { |
24
|
4 |
|
if (!$this->manager->getSchema()->hasSpace($type)) { |
25
|
|
|
throw new LogicException("Type $type not exists"); |
26
|
|
|
} |
27
|
|
|
|
28
|
4 |
|
$fields = []; |
29
|
4 |
|
if ($type != 'mapping') { |
30
|
4 |
|
$mapping = $this->manager->get('mapping')->find(['space' => $type]); |
31
|
4 |
|
foreach ($mapping as $row) { |
32
|
4 |
|
$fields[$row->line] = $row->property; |
|
|
|
|
33
|
4 |
|
} |
34
|
4 |
|
ksort($fields); |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
4 |
|
$this->types[$type] = new Type($this->manager, $type, array_values($fields)); |
39
|
4 |
|
} |
40
|
4 |
|
return $this->types[$type]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Type |
45
|
|
|
*/ |
46
|
4 |
|
public function create($type, array $fields = null) |
47
|
|
|
{ |
48
|
4 |
|
if ($this->manager->getSchema()->hasSpace($type)) { |
49
|
|
|
throw new LogicException("Type $type exists"); |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
$this->manager->getSchema()->createSpace($type); |
53
|
|
|
|
54
|
4 |
|
$instance = new Type($this->manager, $type); |
55
|
|
|
|
56
|
4 |
|
$instance->addProperty('id'); |
57
|
4 |
|
$instance->addIndex('id'); |
58
|
|
|
|
59
|
4 |
|
if ($fields) { |
60
|
4 |
|
call_user_func_array([$instance, 'addProperty'], (array) $fields); |
61
|
4 |
|
} |
62
|
|
|
|
63
|
4 |
|
return $this->types[$type] = $instance; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: