Skip to content

Commit 45846de

Browse files
committed
fluent schema interface
1 parent e704a71 commit 45846de

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/Space.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Space
88
{
99
private $mapper;
10-
10+
1111
private $id;
1212
private $name;
1313
private $format;
@@ -38,6 +38,8 @@ public function addProperty($name, $type)
3838
$this->mapper->getClient()->evaluate("box.space[$this->id]:format(...)", [$format]);
3939

4040
$this->parseFormat();
41+
42+
return $this;
4143
}
4244

4345
public function removeProperty($name)
@@ -51,13 +53,17 @@ public function removeProperty($name)
5153
$this->format = $format;
5254

5355
$this->parseFormat();
56+
57+
return $this;
5458
}
5559

5660
public function removeIndex($name)
5761
{
5862
$this->mapper->getClient()->evaluate("box.space[$this->id].index.$name:drop()");
5963
$this->indexes = [];
6064
$this->mapper->getRepository('_index')->flushCache();
65+
66+
return $this;
6167
}
6268

6369
public function createIndex($config)
@@ -66,7 +72,7 @@ public function createIndex($config)
6672
if(!is_array($config)) {
6773
$config = ['fields' => $config];
6874
}
69-
75+
7076

7177
if(!array_key_exists('fields', $config)) {
7278
if(array_values($config) != $config) {
@@ -104,6 +110,7 @@ public function createIndex($config)
104110
$this->mapper->getClient()->evaluate("box.space[$this->id]:create_index('$name', ...)", [$options]);
105111
$this->indexes = [];
106112

113+
return $this;
107114
}
108115

109116
public function isSpecial()
@@ -153,6 +160,7 @@ private function parseFormat()
153160
$this->formatTypesHash[$row['name']] = $row['type'];
154161
$this->formatNamesHash[$row['name']] = $key;
155162
}
163+
return $this;
156164
}
157165

158166
public function hasProperty($name)
@@ -222,7 +230,7 @@ public function castIndex($params)
222230
$equals = $equals && in_array($part[0], $keys);
223231
}
224232
}
225-
233+
226234
if($equals) {
227235
return $index->iid;
228236
}
@@ -313,4 +321,4 @@ public function getRepository()
313321
}
314322
return $this->repository ?: $this->repository = new $class($this);
315323
}
316-
}
324+
}

tests/MapperTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@
66

77
class MapperTest extends TestCase
88
{
9+
public function testFluentInterface()
10+
{
11+
$mapper = $this->createMapper();
12+
$this->clean($mapper);
13+
14+
$mapper->getSchema()->createSpace('session')
15+
16+
->addProperty('uuid', 'str')
17+
->addProperty('activity_at', 'unsigned')
18+
->addProperty('login', 'unsigned')
19+
->addProperty('ip', 'unsigned')
20+
21+
->createIndex('uuid')
22+
23+
->createIndex([
24+
'fields' => 'login',
25+
'unique' => false,
26+
])
27+
28+
->createIndex([
29+
'fields' => 'ip',
30+
'unique' => false,
31+
]);
32+
33+
$entity = $mapper->create('session', [
34+
'uuid' => '81b3edc8-0dd0-43b6-80b4-39f1f8045f3e',
35+
'login' => 1,
36+
'ip' => 2130706433
37+
]);
38+
39+
$this->assertNotNull($entity);
40+
}
41+
942
public function testBasics()
1043
{
1144
$mapper = $this->createMapper();
@@ -107,4 +140,4 @@ public function testQueryCaching()
107140

108141
$this->assertCount(0, $client->getLog());
109142
}
110-
}
143+
}

0 commit comments

Comments
 (0)