|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Basis\Test; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Tarantool\Mapper\Plugin\Spy; |
|
8
|
|
|
|
|
9
|
|
|
class Mapper |
|
10
|
|
|
{ |
|
11
|
|
|
protected $test; |
|
12
|
|
|
public $serviceName; |
|
13
|
|
|
|
|
14
|
2 |
|
public function __construct(Test $test, $service) |
|
15
|
|
|
{ |
|
16
|
2 |
|
$this->test = $test; |
|
17
|
2 |
|
$this->serviceName = $service; |
|
18
|
2 |
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
public function find(string $space, $params = []) |
|
21
|
|
|
{ |
|
22
|
1 |
|
$key = $this->serviceName.'.'.$space; |
|
23
|
1 |
|
if (array_key_exists($key, $this->test->data)) { |
|
24
|
1 |
|
$data = $this->test->data[$key]; |
|
25
|
1 |
|
foreach ($data as $i => $v) { |
|
26
|
1 |
|
if (count($params) && array_intersect_assoc($params, $v) != $params) { |
|
27
|
1 |
|
unset($data[$i]); |
|
28
|
1 |
|
continue; |
|
29
|
|
|
} |
|
30
|
1 |
|
$data[$i] = (object) $v; |
|
31
|
|
|
} |
|
32
|
1 |
|
$data = array_values($data); |
|
33
|
1 |
|
return $data; |
|
34
|
|
|
} |
|
35
|
|
|
return []; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function findOne(string $space, $params = []) |
|
39
|
|
|
{ |
|
40
|
1 |
|
$key = $this->serviceName.'.'.$space; |
|
41
|
1 |
|
if (array_key_exists($key, $this->test->data)) { |
|
42
|
1 |
|
foreach ($this->test->data[$key] as $candidate) { |
|
43
|
1 |
|
if (!count($params) || array_intersect_assoc($params, $candidate) == $params) { |
|
44
|
1 |
|
return (object) $candidate; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function findOrFail(string $space, $params = []) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$result = $this->findOne($space, $params); |
|
53
|
1 |
|
if (!$result) { |
|
54
|
1 |
|
throw new Exception("No ".$space.' found using '.json_encode($params)); |
|
55
|
|
|
} |
|
56
|
1 |
|
return $result; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getPlugin($class) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($class == Spy::class) { |
|
62
|
|
|
return new class { |
|
63
|
|
|
public function hasChanges() |
|
64
|
|
|
{ |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
}; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected $repositores = []; |
|
72
|
|
|
|
|
73
|
1 |
|
public function getRepository($space) |
|
74
|
|
|
{ |
|
75
|
1 |
|
if (!array_key_exists($space, $this->repositores)) { |
|
76
|
1 |
|
$this->repositores[$space] = new Repository($this, $space); |
|
77
|
|
|
} |
|
78
|
1 |
|
return $this->repositores[$space]; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|