|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Isswp101\Persimmon\Collection\Collection; |
|
6
|
|
|
use Isswp101\Persimmon\Collection\ICollection; |
|
7
|
|
|
use Isswp101\Persimmon\Contracts\Storable; |
|
8
|
|
|
use Isswp101\Persimmon\Exceptions\MethodNotImplementedException; |
|
9
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
|
10
|
|
|
|
|
11
|
|
|
class RuntimeCacheRepository implements ICacheRepository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Storable[] |
|
15
|
|
|
*/ |
|
16
|
|
|
private $collection; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->collection = new Collection(); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
private function getHash(string $class, string $id): string |
|
24
|
|
|
{ |
|
25
|
|
|
return $class . '@' . $id; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function instantiate(string $class): Storable |
|
29
|
|
|
{ |
|
30
|
|
|
throw new MethodNotImplementedException(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function find(string $id, string $class, array $columns = []): ?Storable |
|
34
|
|
|
{ |
|
35
|
|
|
$model = $this->collection->get($this->getHash($class, $id)); |
|
|
|
|
|
|
36
|
|
|
if ($model != null && $columns) { |
|
|
|
|
|
|
37
|
|
|
return $model->fill(array_intersect_key($model->toArray(), array_flip($columns))); |
|
38
|
|
|
} |
|
39
|
|
|
return $model; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->collection; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function insert(Storable $model): void |
|
48
|
|
|
{ |
|
49
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
|
50
|
|
|
$this->collection->put($hash, $model); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function update(Storable $model): void |
|
54
|
|
|
{ |
|
55
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
|
56
|
|
|
if (!$this->collection->has($hash)) { |
|
|
|
|
|
|
57
|
|
|
$this->collection->put($hash, $model); |
|
|
|
|
|
|
58
|
|
|
} else { |
|
59
|
|
|
$cachedModel = $this->collection->get($hash); |
|
|
|
|
|
|
60
|
|
|
foreach ($model->toArray() as $key => $value) { |
|
61
|
|
|
$cachedModel->{$key} = $value; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function delete(Storable $model): void |
|
67
|
|
|
{ |
|
68
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
|
69
|
|
|
$this->collection->forget($hash); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getCachedAttributes(string $id, string $class): array |
|
73
|
|
|
{ |
|
74
|
|
|
$model = $this->find($id, $class); |
|
75
|
|
|
return $model != null ? array_keys($model->toArray()) : []; |
|
76
|
|
|
} |
|
77
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..