Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adapting typed object repository to relaxed object repository
- Loading branch information
1 parent
0694dfc
commit 7165753
Showing
16 changed files
with
420 additions
and
563 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Copyright 2019 SignpostMarv | ||
Copyright 2020 SignpostMarv | ||
|
||
# Daft-Typed-Object-Repository | ||
[![Coverage Status](https://coveralls.io/repos/github/daft-framework/Daft-Typed-Object-Repository/badge.svg?branch=master)](https://coveralls.io/github/daft-framework/Daft-Typed-Object-Repository?branch=master) | ||
[![Build Status](https://travis-ci.org/daft-framework/Daft-Typed-Object-Repository.svg?branch=master)](https://travis-ci.org/daft-framework/Daft-Typed-Object-Repository) | ||
[![Type Coverage](https://shepherd.dev/github/daft-framework/Daft-Typed-Object-Repository/coverage.svg)](https://shepherd.dev/github/daft-framework/Daft-Typed-Object-Repository) | ||
# daft-relaxed-object-repository | ||
[![Coverage Status](https://coveralls.io/repos/github/daft-framework/daft-relaxed-object-repository/badge.svg?branch=master)](https://coveralls.io/github/daft-framework/daft-relaxed-object-repository?branch=master) | ||
[![Build Status](https://travis-ci.org/daft-framework/daft-relaxed-object-repository.svg?branch=master)](https://travis-ci.org/daft-framework/daft-relaxed-object-repository) | ||
[![Type Coverage](https://shepherd.dev/github/daft-framework/daft-relaxed-object-repository/coverage.svg)](https://shepherd.dev/github/daft-framework/daft-relaxed-object-repository) | ||
|
||
Typed Object Repository: Simplified version of [signpostmarv/daft-object-repository](https://github.com/SignpostMarv/daft-object-repository) for [signpostmarv/daft-typed-object](https://github.com/daft-framework/Daft-Typed-Object) | ||
Relaxed Object Repository: Simplified version of [signpostmarv/daft-typed-object-repository](https://github.com/daft-framework/Daft-Typed-Object-Repository) for objects other than [signpostmarv/daft-typed-object](https://github.com/daft-framework/Daft-Typed-Object) implementations |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php | ||
/** | ||
* @author SignpostMarv | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace SignpostMarv\DaftRelaxedObjectRepository\Fixtures; | ||
|
||
use RuntimeException; | ||
use SignpostMarv\DaftRelaxedObjectRepository\AbstractObjectRepository; | ||
use SignpostMarv\DaftRelaxedObjectRepository\AppendableObjectRepository; | ||
use SignpostMarv\DaftRelaxedObjectRepository\ConvertingRepository; | ||
use SignpostMarv\DaftRelaxedObjectRepository\PatchableObjectRepository; | ||
use Throwable; | ||
|
||
/** | ||
* @template T1 as Thing | ||
* @template T2 as array{id:int} | ||
* @template S1 as array{name:string} | ||
* @template S2 as array{id:int, name:string} | ||
* | ||
* @template-extends AbstractObjectRepository<T1, T2> | ||
* | ||
* @template-implements AppendableObjectRepository<T1, T2, S1> | ||
* @template-implements ConvertingRepository<T1, S2, T2> | ||
* @template-implements PatchableObjectRepository<T1, T2, S1> | ||
*/ | ||
class MemoryRepository extends AbstractObjectRepository implements | ||
AppendableObjectRepository, | ||
ConvertingRepository, | ||
PatchableObjectRepository | ||
{ | ||
/** | ||
* @var array<string, array{id:int, name:string}> | ||
*/ | ||
protected array $data = []; | ||
|
||
/** | ||
* @var array<string, T1> | ||
*/ | ||
protected array $memory = []; | ||
|
||
/** | ||
* @param T1 $object | ||
* | ||
* @return T1 | ||
*/ | ||
public function AppendObject( | ||
object $object | ||
) : object { | ||
/** | ||
* @var T1 | ||
*/ | ||
return $this->AppendObjectFromArray([ | ||
'name' => $object->name, | ||
]); | ||
} | ||
|
||
public function AppendObjectFromArray( | ||
array $data | ||
) : object { | ||
$new_id = max(0, count($this->data)) + 1; | ||
|
||
$data = [ | ||
'id' => $new_id, | ||
'name' => $data['name'], | ||
]; | ||
|
||
$hash = static::RelaxedObjectHash(['id' => $new_id]); | ||
|
||
$this->data[$hash] = $data; | ||
|
||
$object = $this->ConvertSimpleArrayToObject($data); | ||
|
||
$this->memory[$hash] = $object; | ||
|
||
/** | ||
* @var T1 | ||
*/ | ||
return $object; | ||
} | ||
|
||
/** | ||
* @param T1 $object | ||
*/ | ||
public function UpdateObject( | ||
object $object | ||
) : void { | ||
$id = $this->ObtainIdFromObject($object); | ||
|
||
$hash = static::RelaxedObjectHash($id); | ||
|
||
parent::UpdateObject($object); | ||
|
||
$this->data[$hash] = $this->ConvertObjectToSimpleArray($object); | ||
} | ||
|
||
/** | ||
* @param T2 $id | ||
*/ | ||
public function RemoveObject(array $id) : void | ||
{ | ||
$hash = static::RelaxedObjectHash($id); | ||
|
||
$this->ForgetObject($id); | ||
unset($this->data[$hash]); | ||
} | ||
|
||
/** | ||
* @param T2 $id | ||
* | ||
* @return T1|null | ||
*/ | ||
public function MaybeRecallObject( | ||
array $id | ||
) : ? object { | ||
$maybe = parent::MaybeRecallObject($id); | ||
|
||
if (is_null($maybe)) { | ||
$hash = static::RelaxedObjectHash($id); | ||
|
||
$row = $this->data[$hash] ?? null; | ||
|
||
if (is_array($row)) { | ||
$object = $this->ConvertSimpleArrayToObject($row); | ||
|
||
$this->UpdateObject($object); | ||
|
||
return $object; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
return $maybe; | ||
} | ||
|
||
/** | ||
* @param T2 $id | ||
* @param S1 $data | ||
*/ | ||
public function PatchObjectData(array $id, array $data) : void | ||
{ | ||
/** | ||
* @var array<string, scalar|null> | ||
*/ | ||
$id = $id; | ||
|
||
/** | ||
* @var array<string, scalar|null> | ||
*/ | ||
$data = $data; | ||
|
||
/** @var S2 */ | ||
$from_array_args = $id + $data; | ||
|
||
$object = $this->ConvertSimpleArrayToObject($from_array_args); | ||
|
||
$this->UpdateObject($object); | ||
} | ||
|
||
/** | ||
* @param T1 $object | ||
* | ||
* @return T2 | ||
*/ | ||
public function ObtainIdFromObject(object $object) : array | ||
{ | ||
/** @var T2 */ | ||
return [ | ||
'id' => $object->id, | ||
]; | ||
} | ||
|
||
public function ConvertSimpleArrayToObject(array $array) : object | ||
{ | ||
/** @var T1 */ | ||
return new Thing($array['id'], $array['name']); | ||
} | ||
|
||
public function ConvertObjectToSimpleArray(object $object) : array | ||
{ | ||
/** @var S2 */ | ||
return [ | ||
'id' => $object->id, | ||
'name' => $object->name, | ||
]; | ||
} | ||
} |
Oops, something went wrong.