Skip to content

Commit

Permalink
optional array result type
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Jun 4, 2024
1 parent 4ec0d08 commit 4af2483
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand Down
1 change: 1 addition & 0 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 21 additions & 14 deletions tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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
Expand All @@ -199,17 +199,17 @@ 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();
}

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);
}
}

Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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'), [
Expand Down

0 comments on commit 4af2483

Please sign in to comment.