-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextResolver.php
67 lines (51 loc) · 1.72 KB
/
ContextResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace Honeystone\Context;
use Honeystone\Context\Contracts\DefinesContext;
use Honeystone\Context\Contracts\ResolvesContext;
use Honeystone\Context\Exceptions\ContextIntegrityException;
use Illuminate\Support\Str;
use function method_exists;
abstract class ContextResolver implements ResolvesContext
{
public function define(DefinesContext $definition): void
{
//no action
}
public function verifyContextIntegrity(DefinesContext $definition, array $resolved = []): void
{
foreach ($definition->eachContext() as $name) {
$method = $this->getIntegrityCheckMethodName($name);
if (
method_exists($this, $method) &&
!$this->$method($definition, $resolved[$name] ?? null, $resolved)
) {
throw new ContextIntegrityException($name);
}
}
}
public function serialize(DefinesContext $definition, array $resolved = []): array
{
$data = [];
foreach ($definition->eachContext() as $name) {
$data[$name] = $this->getSerializedContext($name, $resolved[$name] ?? null);
}
return $data;
}
private function getSerializedContext(string $name, ?object $context): string|int|null
{
$method = $this->getSerializeMethodName($name);
if (method_exists($this, $method)) {
return $this->$method($context);
}
return $context?->id;
}
private function getIntegrityCheckMethodName(string $name): string
{
return Str::camel('check-'.$name);
}
private function getSerializeMethodName(string $name): string
{
return Str::camel('serialize-'.$name);
}
}