From 4af2483348f9d7f1894595a3c30464dd9d9db9f1 Mon Sep 17 00:00:00 2001 From: dmitry krokhin Date: Tue, 4 Jun 2024 17:02:42 +0300 Subject: [PATCH] optional array result type --- README.md | 1 + src/Mapper.php | 1 + src/Space.php | 7 ++++++- tests/MapperTest.php | 35 +++++++++++++++++++++-------------- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3f04d76..69f89bc 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,7 @@ $helloWorld = $posts->update($helloWorld, [ // if you use instance classes, instance would be updated $policy = $mapper->findOrFail('policy', ['id' => 3]); +$policy = $mapper->get('policy', 3); // getter shortcut $mapper->update('policy', $policy, [ 'title' => 'updated title', ]); diff --git a/src/Mapper.php b/src/Mapper.php index ac96cfd..bca730d 100644 --- a/src/Mapper.php +++ b/src/Mapper.php @@ -26,6 +26,7 @@ public function __construct( Client $client, public ?CacheItemPoolInterface $cache = null, public bool $spy = false, + public bool $arrays = false, ) { $this->middleware = new Middleware($this); $this->client = $client->withMiddleware($this->middleware); diff --git a/src/Space.php b/src/Space.php index 5591d9c..bec075c 100644 --- a/src/Space.php +++ b/src/Space.php @@ -244,14 +244,19 @@ public function getInstance(array $tuple) } try { - return array_combine($this->fields, $tuple); + $instance = array_combine($this->fields, $tuple); } catch (ValueError) { $instance = []; foreach ($this->fields as $n => $field) { $instance[$field] = array_key_exists($n, $tuple) ? $tuple[$n] : null; } + } + + if ($this->mapper->arrays) { return $instance; } + + return (object) $instance; } public function getKey($query, ?array $index = null): array diff --git a/tests/MapperTest.php b/tests/MapperTest.php index 2989604..37f8c6c 100644 --- a/tests/MapperTest.php +++ b/tests/MapperTest.php @@ -55,8 +55,8 @@ public function testDifferentIndexPartConfiguration() { $mapper = $this->createMapper(); foreach ($mapper->find('_vspace') as $space) { - if ($space['id'] >= 512) { - $mapper->getSpace($space['id'])->drop(); + if ($space->id >= 512) { + $mapper->getSpace($space->id)->drop(); } } @@ -97,8 +97,8 @@ public function testCreateRow() { $mapper = $this->createMapper(); foreach ($mapper->find('_vspace') as $space) { - if ($space['id'] >= 512) { - $mapper->getSpace($space['id'])->drop(); + if ($space->id >= 512) { + $mapper->getSpace($space->id)->drop(); } } @@ -156,8 +156,8 @@ public function testFindOrCreateRow() { $mapper = $this->createMapper(); foreach ($mapper->find('_vspace') as $space) { - if ($space['id'] >= 512) { - $mapper->getSpace($space['id'])->drop(); + if ($space->id >= 512) { + $mapper->getSpace($space->id)->drop(); } } @@ -184,8 +184,8 @@ public function testFindOrCreateRow() $findRow = $tester->findOrCreate(['nick' => 'Billy']); $result = $mapper->client->evaluate("return box.space.tester.index.nick:select('Jimmy')")[0]; $this->assertTrue($result[0][1] == 0); - $this->assertSame($secondRow['id'], $result[0][1]); - $this->assertSame($firstRow, $findRow); + $this->assertSame($secondRow->id, $result[0][1]); + $this->assertEquals($firstRow, $findRow); $tester->drop(); //id is first field @@ -199,8 +199,8 @@ public function testFindOrCreateRow() $findRow = $tester->findOrCreate(['nick' => 'Jimmy']); $result = $mapper->client->evaluate("return box.space.tester.index.nick:select('Jimmy')")[0]; $this->assertTrue($result[0][0] == 2); - $this->assertSame($secondRow['id'], $result[0][0]); - $this->assertSame($secondRow, $findRow); + $this->assertSame($secondRow->id, $result[0][0]); + $this->assertEquals($secondRow, $findRow); $tester->drop(); } @@ -208,8 +208,8 @@ public function testLua() { $mapper = $this->createMapper(); foreach ($mapper->find('_vfunc') as $func) { - if (strpos($func['name'], 'evaluate_') === 0) { - $mapper->client->call('box.schema.func.drop', $func['name']); + if (strpos($func->name, 'evaluate_') === 0) { + $mapper->client->call('box.schema.func.drop', $func->name); } } @@ -237,8 +237,8 @@ public function testSpaces() $mapper = $this->createMapper(); foreach ($mapper->find('_vspace') as $space) { - if ($space['id'] >= 512) { - $mapper->getSpace($space['id'])->drop(); + if ($space->id >= 512) { + $mapper->getSpace($space->id)->drop(); } } @@ -263,10 +263,17 @@ public function testSpaces() $space->addProperty('name', 'string'); $space->addProperty('nick', 'string', ['default' => 'nick']); + $space = $mapper->createSpace('object'); + $space->addProperty('id', 'unsigned'); + $space->addProperty('name', 'string'); + $space->addProperty('nick', 'string', ['default' => 'nick']); + $todo = array_keys($userTypes); $todo[] = 'array'; + $todo[] = 'object'; foreach ($todo as $nick) { + $mapper->arrays = $nick == 'array'; $space = $mapper->getSpace($nick); $this->assertSame($space->getFields(), ['id', 'name', 'nick']); $this->assertEquals($space->getFieldFormat('id'), [