Skip to content

Commit c3e28ba

Browse files
committed
space casting during update and delete
1 parent 273feae commit c3e28ba

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/Api.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99

1010
trait Api
1111
{
12-
abstract public function getSpace(string $name): Space;
12+
abstract public function getSpace(object|int|string $id): Space;
1313

1414
public function create(string $space, array $data)
1515
{
1616
return $this->getSpace($space)->create($data);
1717
}
1818

19-
public function delete(string $space, $instance)
19+
public function delete(object|string $space, array|object|null $instance = null)
2020
{
21+
if (is_object($space)) {
22+
$instance = $space;
23+
}
2124
$this->getSpace($space)->delete($instance);
2225
}
2326

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

53-
public function update(string $space, $instance, Operations|array $operations)
56+
public function update(string|object $space, object|array $instance, Operations|array|null $operations = null)
5457
{
58+
if (is_object($space)) {
59+
[$instance, $operations] = [$space, $instance];
60+
}
5561
$this->getSpace($space)->update($instance, $operations);
5662
}
5763
}

src/Mapper.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,16 @@ public function getClassSpace(int|string $class): int|string
143143
return $class;
144144
}
145145

146-
public function getSpace(int|string $id): Space
146+
public function getSpace(object|int|string $id): Space
147147
{
148148
if (!count($this->spaces)) {
149149
$this->setSchemaId(0);
150150
}
151151

152+
if (is_object($id)) {
153+
$id = get_class($id);
154+
}
155+
152156
$space = $this->getClassSpace($id);
153157
if ($space !== $id) {
154158
if (!$this->hasSpace($space)) {

src/Pool.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tarantool\Mapper;
66

77
use Closure;
8+
use LogicException;
89

910
class Pool
1011
{
@@ -13,7 +14,8 @@ class Pool
1314
private array $mappers = [];
1415

1516
public function __construct(
16-
public readonly Closure $callback
17+
public readonly Closure $mapperFactory,
18+
public readonly ?Closure $spaceCasting = null
1719
) {
1820
}
1921

@@ -40,15 +42,24 @@ public function getChanges(): array
4042
public function getMapper(string $name): Mapper
4143
{
4244
if (!array_key_exists($name, $this->mappers)) {
43-
$callback = $this->callback;
45+
$callback = $this->mapperFactory;
4446
$this->mappers[$name] = $callback($name);
4547
}
4648

4749
return $this->mappers[$name];
4850
}
4951

50-
public function getSpace(string $name): Space
52+
public function getSpace(object|int|string $name): Space
5153
{
54+
if (is_object($name) && $this->spaceCasting) {
55+
$callback = $this->spaceCasting;
56+
$name = $callback($name);
57+
}
58+
59+
if (!is_string($name)) {
60+
throw new LogicException("Space should be a string");
61+
}
62+
5263
[$mapper, $space] = explode('.', $name);
5364
return $this->getMapper($mapper)->getSpace($space);
5465
}

tests/MapperTest.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function testClassBased()
4646
$mapper->registerClass(TypedConstructor::class);
4747
$this->assertInstanceOf(TypedConstructor::class, $mapper->findOne('constructor'));
4848
$this->assertEquals($row, $mapper->findOne('constructor'));
49+
50+
$constructor = $mapper->findOne('constructor');
51+
52+
$mapper->update($constructor, ['nick' => 'space casting']);
53+
$this->assertSame($constructor->nick, 'space casting');
54+
55+
$mapper->delete($constructor);
56+
$this->assertNull($mapper->findOne('constructor'));
4957
}
5058

5159
public function testAttribute()
@@ -490,14 +498,20 @@ public function testSpaces()
490498

491499
$pool = new Pool(function () use ($mapper) {
492500
return $mapper;
501+
}, function ($instance) use ($mapper) {
502+
$name = get_class($instance)::getSpaceName();
503+
return "prefix.$name";
493504
});
494505

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

498509
$changes = $pool->getChanges();
499510
$this->assertCount(4, $changes);
500511
$this->assertSame($changes[0]->space, 'first.array');
501512
$this->assertSame($changes[2]->space, 'second.array');
513+
514+
// validate pool space casting
515+
$pool->update($constructor, ['nick' => 'tester']);
502516
}
503517
}

0 commit comments

Comments
 (0)