|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Palladium\Entity; |
|
6
|
|
|
use Palladium\Mapper; |
|
7
|
|
|
use Palladium\Contract\CanCreateMapper; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class Identity |
|
12
|
|
|
{ |
|
13
|
|
|
private $list = [ |
|
14
|
|
|
Entity\Identity::class => Mapper\Identity::class, |
|
15
|
|
|
Entity\EmailIdentity::class => Mapper\EmailIdentity::class, |
|
16
|
|
|
Entity\CookieIdentity::class => Mapper\CookieIdentity::class, |
|
17
|
|
|
Entity\NonceIdentity::class => Mapper\NonceIdentity::class, |
|
18
|
|
|
Entity\IdentityCollection::class => Mapper\IdentityCollection::class, |
|
19
|
|
|
'IdentityAccount' => Mapper\IdentityAccount::class, |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
public function __construct(CanCreateMapper $mapperFactory) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->mapperFactory = $mapperFactory; |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
public function define(string $entity, string $mapper) |
|
30
|
|
|
{ |
|
31
|
|
|
if (class_exists($entity) === false) { |
|
32
|
|
|
throw new RuntimeException("Class '{$entity}' was not found!"); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (class_exists($mapper) === false) { |
|
36
|
|
|
throw new RuntimeException("Class '{$mapper}' was not found!"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->list[$entity] = $mapper; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
public function load($identity, string $override = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$key = $this->computeKey(get_class($identity), $override); |
|
47
|
|
|
$mapper = $this->mapperFactory->create($this->list[$key]); |
|
48
|
|
|
$mapper->fetch($identity); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
public function save($identity, string $override = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$key = $this->computeKey(get_class($identity), $override); |
|
55
|
|
|
$mapper = $this->mapperFactory->create($this->list[$key]); |
|
56
|
|
|
$mapper->store($identity); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
public function delete($identity, string $override = null) |
|
61
|
|
|
{ |
|
62
|
|
|
$key = $this->computeKey(get_class($identity), $override); |
|
63
|
|
|
$mapper = $this->mapperFactory->create($this->list[$key]); |
|
64
|
|
|
$mapper->remove($identity); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
public function has($identity, string $override = null) |
|
69
|
|
|
{ |
|
70
|
|
|
$key = $this->computeKey(get_class($identity), $override); |
|
71
|
|
|
$mapper = $this->mapperFactory->create($this->list[$key]); |
|
72
|
|
|
return $mapper->exists($identity); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
private function computeKey(string $key, string $override = null) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($override !== null) { |
|
79
|
|
|
$key = $override; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// var_dump([$key, $this->list]); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (array_key_exists($key, $this->list) === false) { |
|
85
|
|
|
throw new RuntimeException("No mapper for class '{$key}' has been defined!"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $key; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: