|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Finite\State\Accessor; |
|
4
|
|
|
|
|
5
|
|
|
use Finite\Exception\NoSuchPropertyException; |
|
6
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException as SymfonyNoSuchPropertyException; |
|
7
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Property path implementation of state accessor. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Yohan Giarelli <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class PropertyPathStateAccessor implements StateAccessorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $propertyPath; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var PropertyAccessorInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $propertyAccessor; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $propertyPath |
|
29
|
|
|
* @param PropertyAccessorInterface $propertyAccessor |
|
30
|
|
|
*/ |
|
31
|
55 |
|
public function __construct($propertyPath = 'finiteState', PropertyAccessorInterface $propertyAccessor = null) |
|
32
|
|
|
{ |
|
33
|
55 |
|
$this->propertyPath = $propertyPath; |
|
34
|
55 |
|
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); |
|
35
|
55 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
15 |
|
public function getState($object) |
|
41
|
|
|
{ |
|
42
|
|
|
try { |
|
43
|
15 |
|
return $this->propertyAccessor->getValue($object, $this->propertyPath); |
|
44
|
5 |
|
} catch (SymfonyNoSuchPropertyException $e) { |
|
45
|
5 |
|
throw new NoSuchPropertyException(sprintf( |
|
46
|
5 |
|
'Property path "%s" on object "%s" does not exist.', |
|
47
|
5 |
|
$this->propertyPath, |
|
48
|
5 |
|
get_class($object) |
|
49
|
5 |
|
), $e->getCode(), $e); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
15 |
|
public function setState(&$object, $value) |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
15 |
|
$this->propertyAccessor->setValue($object, $this->propertyPath, $value); |
|
60
|
11 |
|
} catch (SymfonyNoSuchPropertyException $e) { |
|
61
|
5 |
|
throw new NoSuchPropertyException(sprintf( |
|
62
|
5 |
|
'Property path "%s" on object "%s" does not exist.', |
|
63
|
5 |
|
$this->propertyPath, |
|
64
|
5 |
|
get_class($object) |
|
65
|
5 |
|
), $e->getCode(), $e); |
|
66
|
|
|
} |
|
67
|
10 |
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|