Skip to content

Commit

Permalink
implement partial patching method, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
SignpostMarv committed Sep 15, 2019
1 parent ee317be commit b0a0a45
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
91 changes: 91 additions & 0 deletions Tests/DaftTypedObjectRepositoryTest.php
Expand Up @@ -277,4 +277,95 @@ function ($property, $value) use ($object_type) {

$repo->RecallTypedObject($object->ObtainId(), new Exception($random));
}

/**
* @return array<
int,
array{
0:class-string<AppendableTypedObjectRepository&PatchableObjectRepository>,
1:array{type:class-string<T1>},
2:array<string, scalar|null>,
3:array<string, scalar|null>,
4:array<string, scalar|null>
}
>
*/
public function dataProviderPatchObject() : array
{
/**
* @var array<
int,
array{
0:class-string<AppendableTypedObjectRepository&PatchableObjectRepository>,
1:array{type:class-string<T1>},
2:array<string, scalar|null>,
3:array<string, scalar|null>,
4:array<string, scalar|null>
}
>
*/
return [
[
Fixtures\DaftTypedObjectMemoryRepository::class,
[
'type' => Fixtures\MutableForRepository::class,
],
[
'id' => 0,
'name' => 'foo',
],
[
'name' => 'bar',
],
[
'id' => '1',
'name' => 'bar',
],
],
];
}

/**
* @template K as key-of<S>
*
* @dataProvider dataProviderPatchObject
*
* @depends testAppendTypedObject
*
* @param class-string<AppendableTypedObjectRepository&PatchableObjectRepository> $repo_type
* @param array{type:class-string<T1>} $repo_args
* @param array<string, scalar|null> $append_this
* @param array<string, scalar|null> $patch_this
* @param array<string, scalar|null> $expect_this
*/
public function testPatchObject(
string $repo_type,
array $repo_args,
array $append_this,
array $patch_this,
array $expect_this
) : void {
$repo = new $repo_type(
$repo_args
);

$object_type = $repo_args['type'];

/**
* @var T1
*/
$object = $object_type::__fromArray($append_this);

/**
* @var T1
*/
$fresh = $repo->AppendTypedObject($object);

$repo->PatchTypedObjectData($fresh->ObtainId(), $patch_this);

$this->assertSame(
$expect_this,
$repo->RecallTypedObject($fresh->ObtainId())->__toArray()
);
}
}
37 changes: 36 additions & 1 deletion Tests/Fixtures/DaftTypedObjectMemoryRepository.php
Expand Up @@ -10,6 +10,7 @@
use SignpostMarv\DaftTypedObject\AbstractDaftTypedObjectRepository;
use SignpostMarv\DaftTypedObject\AppendableTypedObjectRepository;
use SignpostMarv\DaftTypedObject\DaftTypedObjectForRepository;
use SignpostMarv\DaftTypedObject\PatchableObjectRepository;
use Throwable;

/**
Expand All @@ -20,8 +21,11 @@
* @template-extends AbstractDaftTypedObjectRepository<T1, T2>
*
* @template-implements AppendableTypedObjectRepository<T1, T2, S1>
* @template-implements PatchableObjectRepository<T1, T2, S1>
*/
class DaftTypedObjectMemoryRepository extends AbstractDaftTypedObjectRepository implements AppendableTypedObjectRepository
class DaftTypedObjectMemoryRepository extends AbstractDaftTypedObjectRepository implements
AppendableTypedObjectRepository,
PatchableObjectRepository
{
/**
* @var array<string, array{id:int, name:string}>
Expand Down Expand Up @@ -145,4 +149,35 @@ public function MaybeRecallTypedObject(

return $maybe;
}

/**
* @param T2 $id
* @param S1 $data
*/
public function PatchTypedObjectData(array $id, array $data) : void
{
$type = $this->type;

/**
* @var array<string, scalar|null>
*/
$id = $id;

/**
* @var array<string, scalar|null>
*/
$data = $data;

/**
* @var array<string, scalar|null>
*/
$from_array_args = $id + $data;

/**
* @var T1
*/
$object = $type::__fromArray($from_array_args);

$this->UpdateTypedObject($object);
}
}
23 changes: 23 additions & 0 deletions src/PatchableObjectRepository.php
@@ -0,0 +1,23 @@
<?php
/**
* @author SignpostMarv
*/
declare(strict_types=1);

namespace SignpostMarv\DaftTypedObject;

/**
* @template T1 as DaftTypedObjectForRepository
* @template T2 as array<string, scalar>
* @template T3 as array<string, scalar|null>
*
* @template-extends DaftTypedObjectRepository<T1, T2>
*/
interface PatchableObjectRepository extends DaftTypedObjectRepository
{
/**
* @param T2 $id
* @param T3 $data
*/
public function PatchTypedObjectData(array $id, array $data) : void;
}

0 comments on commit b0a0a45

Please sign in to comment.