1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\SM\Metadata; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use SM\SMException; |
7
|
|
|
|
8
|
|
|
class MetadataStore implements MetadataStoreInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $config; |
14
|
|
|
|
15
|
66 |
|
public function __construct(array $config) |
16
|
|
|
{ |
17
|
66 |
|
$this->config = $config; |
18
|
22 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
6 |
|
public function graph($key = null, $default = null) |
24
|
|
|
{ |
25
|
6 |
|
return $this->get($this->config, $key, $default); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
9 |
|
public function state($state, $key = null, $default = null) |
32
|
|
|
{ |
33
|
9 |
|
foreach ($this->config['states'] as $value) { |
34
|
9 |
|
if ($value['name'] == $state) { |
35
|
9 |
|
return $this->get($value, $key, $default); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
throw new SMException(sprintf( |
40
|
3 |
|
'State "%s" does not exist on graph "%s".', |
41
|
2 |
|
$state, |
42
|
3 |
|
$this->config['graph'] |
43
|
2 |
|
)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
6 |
|
public function transition($transition, $key = null, $default = null) |
50
|
|
|
{ |
51
|
6 |
|
if (! isset($this->config['transitions'][$transition])) { |
52
|
3 |
|
throw new SMException(sprintf( |
53
|
3 |
|
'Transition "%s" does not exist on graph "%s"', |
54
|
2 |
|
$transition, |
55
|
3 |
|
$this->config['graph'] |
56
|
2 |
|
)); |
57
|
|
|
} |
58
|
|
|
|
59
|
6 |
|
return $this->get($this->config['transitions'][$transition], $key, $default); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get a metadata value from a subject. |
64
|
|
|
* |
65
|
|
|
* @param array $subject |
66
|
|
|
* @param string|null $key |
67
|
|
|
* @param mixed $default |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
21 |
|
protected function get(array $subject, $key, $default) |
71
|
|
|
{ |
72
|
21 |
|
$metadata = Arr::get($subject, 'metadata', []); |
73
|
|
|
|
74
|
21 |
|
return Arr::get($metadata, $key, $default); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|