1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper; |
4
|
|
|
|
5
|
|
|
use Tarantool\Client\Client as TarantoolClient; |
6
|
|
|
use Tarantool\Mapper\Schema\Schema; |
7
|
|
|
use Tarantool\Mapper\Schema\Meta; |
8
|
|
|
use LogicException; |
9
|
|
|
|
10
|
|
|
class Manager implements Contracts\Manager |
11
|
|
|
{ |
12
|
|
|
protected $meta; |
13
|
|
|
protected $schema; |
14
|
|
|
protected $client; |
15
|
|
|
protected $repositores = []; |
16
|
|
|
|
17
|
64 |
|
public function __construct(TarantoolClient $client) |
18
|
|
|
{ |
19
|
64 |
|
$this->client = $client; |
20
|
64 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return Contracts\Repository|Contracts\Entity |
24
|
|
|
*/ |
25
|
63 |
|
public function get($type, $id = null) |
26
|
|
|
{ |
27
|
63 |
|
if (!array_key_exists($type, $this->repositores)) { |
28
|
63 |
|
$this->repositores[$type] = new Repository($this->getMeta()->get($type)); |
29
|
|
|
} |
30
|
|
|
|
31
|
63 |
|
if ($id) { |
32
|
12 |
|
return $this->repositores[$type]->find($id); |
33
|
|
|
} |
34
|
|
|
|
35
|
63 |
|
return $this->repositores[$type]; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
public function forgetRepository($type) |
39
|
|
|
{ |
40
|
2 |
|
if (array_key_exists($type, $this->repositores)) { |
41
|
1 |
|
unset($this->repositores[$type]); |
42
|
|
|
} |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Contracts\Entity |
47
|
|
|
*/ |
48
|
63 |
|
public function save(Contracts\Entity $entity) |
49
|
|
|
{ |
50
|
63 |
|
return $this->findRepository($entity)->save($entity); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Contracts\Entity |
55
|
|
|
*/ |
56
|
6 |
|
public function remove(Contracts\Entity $entity) |
57
|
|
|
{ |
58
|
6 |
|
return $this->findRepository($entity)->remove($entity); |
59
|
|
|
} |
60
|
|
|
|
61
|
63 |
|
public function findRepository(Contracts\Entity $entity) |
62
|
|
|
{ |
63
|
63 |
|
foreach ($this->repositores as $repository) { |
64
|
63 |
|
if ($repository->knows($entity)) { |
65
|
63 |
|
return $repository; |
66
|
|
|
} |
67
|
|
|
} |
68
|
1 |
|
throw new LogicException('Entity should be related with repository'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Contracts\Entity |
73
|
|
|
*/ |
74
|
63 |
|
public function create($type, $data = null) |
75
|
|
|
{ |
76
|
63 |
|
return $this->save($this->get($type)->create($data)); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return Client |
81
|
|
|
*/ |
82
|
64 |
|
public function getClient() |
83
|
|
|
{ |
84
|
64 |
|
return $this->client; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Schema |
89
|
|
|
*/ |
90
|
63 |
|
public function getSchema() |
91
|
|
|
{ |
92
|
63 |
|
if (!isset($this->schema)) { |
93
|
63 |
|
$this->schema = new Schema($this->getClient()); |
94
|
|
|
} |
95
|
|
|
|
96
|
63 |
|
return $this->schema; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Schema |
101
|
|
|
*/ |
102
|
1 |
|
public function setSchema(Contracts\Schema $schema) |
103
|
|
|
{ |
104
|
1 |
|
if (isset($this->schema)) { |
105
|
|
|
throw new Exception("Schema is defined"); |
106
|
|
|
} |
107
|
1 |
|
$this->schema = $schema; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Meta |
112
|
|
|
*/ |
113
|
63 |
|
public function getMeta() |
114
|
|
|
{ |
115
|
63 |
|
if (!isset($this->meta)) { |
116
|
63 |
|
$this->meta = new Meta($this); |
117
|
|
|
} |
118
|
|
|
|
119
|
63 |
|
return $this->meta; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Meta |
124
|
|
|
*/ |
125
|
1 |
|
public function setMeta(Contracts\Meta $meta) |
126
|
|
|
{ |
127
|
1 |
|
if (isset($this->meta)) { |
128
|
|
|
throw new Exception("Meta is defined"); |
129
|
|
|
} |
130
|
1 |
|
$this->meta = $meta; |
131
|
1 |
|
} |
132
|
|
|
} |
133
|
|
|
|
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: