Skip to content

Commit

Permalink
space casting during update and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Oct 17, 2024
1 parent 273feae commit c3e28ba
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

trait Api
{
abstract public function getSpace(string $name): Space;
abstract public function getSpace(object|int|string $id): Space;

public function create(string $space, array $data)
{
return $this->getSpace($space)->create($data);
}

public function delete(string $space, $instance)
public function delete(object|string $space, array|object|null $instance = null)
{
if (is_object($space)) {
$instance = $space;
}
$this->getSpace($space)->delete($instance);
}

Expand Down Expand Up @@ -50,8 +53,11 @@ public function get(string $space, int $id, bool $require = true)
return $this->getSpace($space)->findOne(compact('id'));
}

public function update(string $space, $instance, Operations|array $operations)
public function update(string|object $space, object|array $instance, Operations|array|null $operations = null)
{
if (is_object($space)) {
[$instance, $operations] = [$space, $instance];
}
$this->getSpace($space)->update($instance, $operations);
}
}
6 changes: 5 additions & 1 deletion src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,16 @@ public function getClassSpace(int|string $class): int|string
return $class;
}

public function getSpace(int|string $id): Space
public function getSpace(object|int|string $id): Space
{
if (!count($this->spaces)) {
$this->setSchemaId(0);
}

if (is_object($id)) {
$id = get_class($id);
}

$space = $this->getClassSpace($id);
if ($space !== $id) {
if (!$this->hasSpace($space)) {
Expand Down
17 changes: 14 additions & 3 deletions src/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tarantool\Mapper;

use Closure;
use LogicException;

class Pool
{
Expand All @@ -13,7 +14,8 @@ class Pool
private array $mappers = [];

public function __construct(
public readonly Closure $callback
public readonly Closure $mapperFactory,
public readonly ?Closure $spaceCasting = null
) {
}

Expand All @@ -40,15 +42,24 @@ public function getChanges(): array
public function getMapper(string $name): Mapper
{
if (!array_key_exists($name, $this->mappers)) {
$callback = $this->callback;
$callback = $this->mapperFactory;
$this->mappers[$name] = $callback($name);
}

return $this->mappers[$name];
}

public function getSpace(string $name): Space
public function getSpace(object|int|string $name): Space
{
if (is_object($name) && $this->spaceCasting) {
$callback = $this->spaceCasting;
$name = $callback($name);
}

if (!is_string($name)) {
throw new LogicException("Space should be a string");
}

[$mapper, $space] = explode('.', $name);
return $this->getMapper($mapper)->getSpace($space);
}
Expand Down
16 changes: 15 additions & 1 deletion tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function testClassBased()
$mapper->registerClass(TypedConstructor::class);
$this->assertInstanceOf(TypedConstructor::class, $mapper->findOne('constructor'));
$this->assertEquals($row, $mapper->findOne('constructor'));

$constructor = $mapper->findOne('constructor');

$mapper->update($constructor, ['nick' => 'space casting']);
$this->assertSame($constructor->nick, 'space casting');

$mapper->delete($constructor);
$this->assertNull($mapper->findOne('constructor'));
}

public function testAttribute()
Expand Down Expand Up @@ -490,14 +498,20 @@ public function testSpaces()

$pool = new Pool(function () use ($mapper) {
return $mapper;
}, function ($instance) use ($mapper) {
$name = get_class($instance)::getSpaceName();
return "prefix.$name";
});

$pool->create('first.array', ['nick' => 'qwerty']);
$pool->create('second.constructor', ['nick' => 'asdf']);
$constructor = $pool->create('second.constructor', ['nick' => 'asdf']);

$changes = $pool->getChanges();
$this->assertCount(4, $changes);
$this->assertSame($changes[0]->space, 'first.array');
$this->assertSame($changes[2]->space, 'second.array');

// validate pool space casting
$pool->update($constructor, ['nick' => 'tester']);
}
}

0 comments on commit c3e28ba

Please sign in to comment.