|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Tarantool\Mapper\Contracts; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
|
|
8
|
|
|
class Type implements Contracts\Type |
|
9
|
|
|
{ |
|
10
|
|
|
protected $properties = []; |
|
11
|
|
|
protected $manager; |
|
12
|
|
|
protected $name; |
|
13
|
|
|
|
|
14
|
4 |
|
public function __construct(Contracts\Manager $manager, $name, array $properties = null) |
|
15
|
|
|
{ |
|
16
|
4 |
|
$this->manager = $manager; |
|
17
|
4 |
|
$this->name = $name; |
|
18
|
|
|
|
|
19
|
4 |
|
if ($name == 'mapping') { |
|
20
|
4 |
|
$properties = ['id', 'space', 'line', 'property']; |
|
21
|
4 |
|
} |
|
22
|
|
|
|
|
23
|
4 |
|
if ($properties) { |
|
24
|
4 |
|
$this->properties = $properties; |
|
25
|
4 |
|
} |
|
26
|
4 |
|
} |
|
27
|
|
|
|
|
28
|
4 |
|
public function getSpace() |
|
29
|
|
|
{ |
|
30
|
4 |
|
return $this->getManager()->getClient()->getSpace($this->name); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
public function getManager() |
|
34
|
|
|
{ |
|
35
|
4 |
|
return $this->manager; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
public function getName() |
|
39
|
|
|
{ |
|
40
|
4 |
|
return $this->name; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
public function getMapping() |
|
44
|
|
|
{ |
|
45
|
4 |
|
return $this->properties; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
public function addIndex($properties, array $arguments = null) |
|
49
|
|
|
{ |
|
50
|
4 |
|
$properties = (array) $properties; |
|
51
|
4 |
View Code Duplication |
foreach ($properties as $property) { |
|
|
|
|
|
|
52
|
4 |
|
if (!$this->hasProperty($property)) { |
|
53
|
|
|
throw new LogicException("Unknown property $property for " . $this->name); |
|
54
|
|
|
} |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
4 |
|
$schema = $this->manager->getSchema(); |
|
58
|
|
|
|
|
59
|
4 |
|
sort($properties); |
|
60
|
4 |
|
$indexName = implode('_', $properties); |
|
61
|
|
|
|
|
62
|
4 |
|
if ($schema->hasIndex($this->getName(), $indexName)) { |
|
63
|
|
|
throw new LogicException("Index $indexName already exists!"); |
|
64
|
|
|
} |
|
65
|
4 |
|
if(!$arguments) { |
|
66
|
4 |
|
$arguments = []; |
|
67
|
4 |
|
} |
|
68
|
|
|
|
|
69
|
4 |
|
if (!array_key_exists('parts', $arguments) || !count($arguments['parts'])) { |
|
70
|
4 |
|
$arguments['parts'] = []; |
|
71
|
4 |
|
foreach ($this->getMapping() as $index => $name) { |
|
72
|
4 |
|
if (in_array($name, $properties)) { |
|
73
|
4 |
|
$arguments['parts'][] = $index + 1; |
|
74
|
4 |
|
$arguments['parts'][] = $name == 'id' ? 'NUM' : 'STR'; |
|
75
|
4 |
|
} |
|
76
|
4 |
|
} |
|
77
|
4 |
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
$schema->createIndex($this->getName(), $indexName, $arguments); |
|
80
|
4 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $property name |
|
84
|
|
|
* @return Type |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function addProperty($first) |
|
87
|
|
|
{ |
|
88
|
4 |
View Code Duplication |
foreach (func_get_args() as $property) { |
|
|
|
|
|
|
89
|
4 |
|
if ($this->hasProperty($property)) { |
|
90
|
|
|
throw new LogicException("Duplicate property $property"); |
|
91
|
|
|
} |
|
92
|
4 |
|
$this->properties[] = $property; |
|
93
|
4 |
|
} |
|
94
|
4 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
public function hasProperty($name) { |
|
98
|
4 |
|
return in_array($name, $this->properties); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
4 |
View Code Duplication |
public function encode($input) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
4 |
|
$output = []; |
|
104
|
4 |
|
foreach ($this->getMapping() as $index => $name) { |
|
105
|
4 |
|
if (array_key_exists($name, $input)) { |
|
106
|
4 |
|
$output[$index] = $input[$name]; |
|
107
|
4 |
|
} |
|
108
|
4 |
|
} |
|
109
|
4 |
|
return $output; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
4 |
View Code Duplication |
public function decode($input) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
4 |
|
$output = []; |
|
115
|
4 |
|
foreach ($this->getMapping() as $index => $name) { |
|
116
|
4 |
|
if (array_key_exists($index, $input)) { |
|
117
|
4 |
|
$output[$name] = $input[$index]; |
|
118
|
4 |
|
} |
|
119
|
4 |
|
} |
|
120
|
4 |
|
return $output; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.