Skip to content

Commit

Permalink
adapting typed object repository to relaxed object repository
Browse files Browse the repository at this point in the history
  • Loading branch information
SignpostMarv committed Feb 19, 2020
1 parent 0694dfc commit 7165753
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 563 deletions.
12 changes: 6 additions & 6 deletions README.md
@@ -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
170 changes: 0 additions & 170 deletions Tests/Fixtures/DaftTypedObjectMemoryRepository.php

This file was deleted.

189 changes: 189 additions & 0 deletions Tests/Fixtures/MemoryRepository.php
@@ -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,
];
}
}

0 comments on commit 7165753

Please sign in to comment.