1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace CCT\Component\ODMElasticsearch\Metadata; |
6
|
|
|
|
7
|
|
|
use CCT\Component\ODMElasticsearch\Metadata\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class VirtualPropertyMetadata extends PropertyMetadata |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Name of method to call |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $methodName; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Virtual property is read only |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $readOnly; |
24
|
|
|
|
25
|
4 |
|
public function __construct($class, $methodName) |
26
|
|
|
{ |
27
|
4 |
|
if (0 === strpos($methodName, 'get')) { |
28
|
1 |
|
$fieldName = lcfirst(substr($methodName, 3)); |
29
|
|
|
} else { |
30
|
3 |
|
$fieldName = $methodName; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
try { |
34
|
4 |
|
parent::__construct($class, $fieldName); |
35
|
3 |
|
} catch (\ReflectionException $e) { |
36
|
|
|
// Horrible hack to call on parent constructor due to vendor lib not using an interface |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
$this->getter = $methodName; |
40
|
4 |
|
$this->readOnly = true; |
41
|
4 |
|
} |
42
|
|
|
|
43
|
1 |
|
public function setValue($obj, $value) |
44
|
|
|
{ |
45
|
1 |
|
throw new \LogicException('VirtualPropertyMetadata is immutable.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set property setter function |
50
|
|
|
* |
51
|
|
|
* @param null|string $setter |
52
|
|
|
*/ |
53
|
|
|
public function setSetterAccessor($setter = null): void |
54
|
|
|
{ |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set property getter function |
59
|
|
|
* |
60
|
|
|
* @param null|string $getter |
61
|
|
|
*/ |
62
|
|
|
public function setGetterAccessor($getter = null): void |
63
|
|
|
{ |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
public function getValue($obj) |
67
|
|
|
{ |
68
|
3 |
|
if (null === $this->getter) { |
69
|
|
|
throw new InvalidArgumentException('Getter value has not been set'); |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
if (false === method_exists($obj, $this->getter)) { |
73
|
1 |
|
throw new InvalidArgumentException( |
74
|
1 |
|
sprintf('Object does not have a method name called %s', $this->getter) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
return $obj->{$this->getter}(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getAccessor() |
82
|
|
|
{ |
83
|
|
|
return $this->getter; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|