Skip to content

Commit 4af2483

Browse files
committed
optional array result type
1 parent 4ec0d08 commit 4af2483

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ $helloWorld = $posts->update($helloWorld, [
142142

143143
// if you use instance classes, instance would be updated
144144
$policy = $mapper->findOrFail('policy', ['id' => 3]);
145+
$policy = $mapper->get('policy', 3); // getter shortcut
145146
$mapper->update('policy', $policy, [
146147
'title' => 'updated title',
147148
]);

src/Mapper.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626
Client $client,
2727
public ?CacheItemPoolInterface $cache = null,
2828
public bool $spy = false,
29+
public bool $arrays = false,
2930
) {
3031
$this->middleware = new Middleware($this);
3132
$this->client = $client->withMiddleware($this->middleware);

src/Space.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,19 @@ public function getInstance(array $tuple)
244244
}
245245

246246
try {
247-
return array_combine($this->fields, $tuple);
247+
$instance = array_combine($this->fields, $tuple);
248248
} catch (ValueError) {
249249
$instance = [];
250250
foreach ($this->fields as $n => $field) {
251251
$instance[$field] = array_key_exists($n, $tuple) ? $tuple[$n] : null;
252252
}
253+
}
254+
255+
if ($this->mapper->arrays) {
253256
return $instance;
254257
}
258+
259+
return (object) $instance;
255260
}
256261

257262
public function getKey($query, ?array $index = null): array

tests/MapperTest.php

+21-14
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public function testDifferentIndexPartConfiguration()
5555
{
5656
$mapper = $this->createMapper();
5757
foreach ($mapper->find('_vspace') as $space) {
58-
if ($space['id'] >= 512) {
59-
$mapper->getSpace($space['id'])->drop();
58+
if ($space->id >= 512) {
59+
$mapper->getSpace($space->id)->drop();
6060
}
6161
}
6262

@@ -97,8 +97,8 @@ public function testCreateRow()
9797
{
9898
$mapper = $this->createMapper();
9999
foreach ($mapper->find('_vspace') as $space) {
100-
if ($space['id'] >= 512) {
101-
$mapper->getSpace($space['id'])->drop();
100+
if ($space->id >= 512) {
101+
$mapper->getSpace($space->id)->drop();
102102
}
103103
}
104104

@@ -156,8 +156,8 @@ public function testFindOrCreateRow()
156156
{
157157
$mapper = $this->createMapper();
158158
foreach ($mapper->find('_vspace') as $space) {
159-
if ($space['id'] >= 512) {
160-
$mapper->getSpace($space['id'])->drop();
159+
if ($space->id >= 512) {
160+
$mapper->getSpace($space->id)->drop();
161161
}
162162
}
163163

@@ -184,8 +184,8 @@ public function testFindOrCreateRow()
184184
$findRow = $tester->findOrCreate(['nick' => 'Billy']);
185185
$result = $mapper->client->evaluate("return box.space.tester.index.nick:select('Jimmy')")[0];
186186
$this->assertTrue($result[0][1] == 0);
187-
$this->assertSame($secondRow['id'], $result[0][1]);
188-
$this->assertSame($firstRow, $findRow);
187+
$this->assertSame($secondRow->id, $result[0][1]);
188+
$this->assertEquals($firstRow, $findRow);
189189
$tester->drop();
190190

191191
//id is first field
@@ -199,17 +199,17 @@ public function testFindOrCreateRow()
199199
$findRow = $tester->findOrCreate(['nick' => 'Jimmy']);
200200
$result = $mapper->client->evaluate("return box.space.tester.index.nick:select('Jimmy')")[0];
201201
$this->assertTrue($result[0][0] == 2);
202-
$this->assertSame($secondRow['id'], $result[0][0]);
203-
$this->assertSame($secondRow, $findRow);
202+
$this->assertSame($secondRow->id, $result[0][0]);
203+
$this->assertEquals($secondRow, $findRow);
204204
$tester->drop();
205205
}
206206

207207
public function testLua()
208208
{
209209
$mapper = $this->createMapper();
210210
foreach ($mapper->find('_vfunc') as $func) {
211-
if (strpos($func['name'], 'evaluate_') === 0) {
212-
$mapper->client->call('box.schema.func.drop', $func['name']);
211+
if (strpos($func->name, 'evaluate_') === 0) {
212+
$mapper->client->call('box.schema.func.drop', $func->name);
213213
}
214214
}
215215

@@ -237,8 +237,8 @@ public function testSpaces()
237237
$mapper = $this->createMapper();
238238

239239
foreach ($mapper->find('_vspace') as $space) {
240-
if ($space['id'] >= 512) {
241-
$mapper->getSpace($space['id'])->drop();
240+
if ($space->id >= 512) {
241+
$mapper->getSpace($space->id)->drop();
242242
}
243243
}
244244

@@ -263,10 +263,17 @@ public function testSpaces()
263263
$space->addProperty('name', 'string');
264264
$space->addProperty('nick', 'string', ['default' => 'nick']);
265265

266+
$space = $mapper->createSpace('object');
267+
$space->addProperty('id', 'unsigned');
268+
$space->addProperty('name', 'string');
269+
$space->addProperty('nick', 'string', ['default' => 'nick']);
270+
266271
$todo = array_keys($userTypes);
267272
$todo[] = 'array';
273+
$todo[] = 'object';
268274

269275
foreach ($todo as $nick) {
276+
$mapper->arrays = $nick == 'array';
270277
$space = $mapper->getSpace($nick);
271278
$this->assertSame($space->getFields(), ['id', 'name', 'nick']);
272279
$this->assertEquals($space->getFieldFormat('id'), [

0 commit comments

Comments
 (0)