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\ClassTypeErrorException; |
9
|
|
|
use Isswp101\Persimmon\Model\IEloquent; |
10
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
11
|
|
|
|
12
|
|
|
class RuntimeCacheRepository implements ICacheRepository |
13
|
|
|
{ |
14
|
|
|
private $collection; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->collection = new Collection(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private function getHash(string $class, string $id): string |
22
|
|
|
{ |
23
|
|
|
return $class . '@' . $id; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function instantiate(string $class): Storable |
27
|
|
|
{ |
28
|
|
|
$instance = new $class; |
29
|
|
|
if (!$instance instanceof IEloquent) { |
30
|
|
|
throw new ClassTypeErrorException(IEloquent::class); |
31
|
|
|
} |
32
|
|
|
return $instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function find(string $id, string $class, array $columns = []): ?Storable |
36
|
|
|
{ |
37
|
|
|
$model = $this->collection->get($this->getHash($class, $id)); |
38
|
|
|
if ($model != null && $columns) { |
|
|
|
|
39
|
|
|
return $model->fill(array_intersect_key($model->toArray(), array_flip($columns))); |
40
|
|
|
} |
41
|
|
|
return $model; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection |
45
|
|
|
{ |
46
|
|
|
return $this->collection; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function insert(Storable $model) |
50
|
|
|
{ |
51
|
|
|
// TODO: Implement insert() method. |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function update(Storable $model) |
55
|
|
|
{ |
56
|
|
|
// TODO: Implement update() method. |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function delete(Storable $model) |
60
|
|
|
{ |
61
|
|
|
// TODO: Implement delete() method. |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getCachedAttributes(string $id, string $class): array |
65
|
|
|
{ |
66
|
|
|
// TODO: Implement getCachedAttributes() method. |
67
|
|
|
} |
68
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.