|
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 $property = []; |
|
12
|
|
|
protected $types = []; |
|
13
|
|
|
|
|
14
|
22 |
|
public function __construct(Contracts\Manager $manager) |
|
15
|
|
|
{ |
|
16
|
22 |
|
$this->manager = $manager; |
|
17
|
22 |
|
$this->property = []; |
|
18
|
22 |
|
$this->references = []; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
22 |
|
$client = $manager->getClient(); |
|
21
|
22 |
|
foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) { |
|
22
|
22 |
|
list($id, $spaceId, $line, $name, $type) = $property; |
|
|
|
|
|
|
23
|
22 |
|
if (!array_key_exists($spaceId, $this->property)) { |
|
24
|
22 |
|
$this->property[$spaceId] = []; |
|
25
|
22 |
|
$this->types[$spaceId] = []; |
|
26
|
22 |
|
} |
|
27
|
22 |
|
$this->property[$spaceId][$line] = $name; |
|
28
|
22 |
|
$this->types[$spaceId][$name] = $type; |
|
29
|
22 |
|
} |
|
30
|
22 |
|
foreach ($this->property as $spaceId => $collection) { |
|
31
|
22 |
|
ksort($collection); |
|
32
|
22 |
|
$this->property[$spaceId] = $collection; |
|
33
|
22 |
|
} |
|
34
|
22 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return Type |
|
38
|
|
|
*/ |
|
39
|
22 |
|
public function get($type) |
|
40
|
|
|
{ |
|
41
|
22 |
|
if (!array_key_exists($type, $this->types)) { |
|
42
|
22 |
|
$spaceId = $this->manager->getSchema()->getSpaceId($type); |
|
43
|
22 |
|
if (!$spaceId) { |
|
44
|
1 |
|
throw new LogicException("Type $type not exists"); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
22 |
|
$this->types[$type] = new Type($this->manager, $type, $this->property[$spaceId], $this->types[$spaceId]); |
|
48
|
22 |
|
} |
|
49
|
|
|
|
|
50
|
22 |
|
return $this->types[$type]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return Type |
|
55
|
|
|
*/ |
|
56
|
22 |
|
public function create($type, array $fields = null) |
|
57
|
|
|
{ |
|
58
|
22 |
|
if ($this->manager->getSchema()->hasSpace($type)) { |
|
59
|
1 |
|
throw new LogicException("Type $type exists"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
22 |
|
$this->manager->getSchema()->createSpace($type); |
|
63
|
|
|
|
|
64
|
22 |
|
$instance = new Type($this->manager, $type, [], []); |
|
65
|
|
|
|
|
66
|
22 |
|
$instance->addProperty('id'); |
|
67
|
22 |
|
$instance->addIndex('id'); |
|
68
|
|
|
|
|
69
|
22 |
|
if ($fields) { |
|
70
|
22 |
|
foreach ($fields as $index => $field) { |
|
71
|
22 |
|
if ($field instanceof Contracts\Type) { |
|
72
|
2 |
|
if(!is_numeric($index)) { |
|
73
|
1 |
|
$instance->reference($field, $index); |
|
74
|
1 |
|
} else { |
|
75
|
1 |
|
$instance->reference($field); |
|
76
|
|
|
} |
|
77
|
2 |
|
} else { |
|
78
|
22 |
|
$instance->addProperty($field); |
|
79
|
|
|
} |
|
80
|
22 |
|
} |
|
81
|
22 |
|
} |
|
82
|
|
|
|
|
83
|
22 |
|
return $this->types[$type] = $instance; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function setConvention(Contracts\Convention $convention) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->convention = $convention; |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
22 |
|
public function getConvention() |
|
94
|
|
|
{ |
|
95
|
22 |
|
if (!isset($this->convention)) { |
|
96
|
22 |
|
$this->convention = new Convention(); |
|
97
|
22 |
|
} |
|
98
|
|
|
|
|
99
|
22 |
|
return $this->convention; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: