-
Notifications
You must be signed in to change notification settings - Fork 1
/
MethodAccessor.php
43 lines (34 loc) · 1.04 KB
/
MethodAccessor.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
<?php
declare(strict_types=1);
namespace Chubbyphp\Validation\Accessor;
use Chubbyphp\Validation\ValidatorLogicException;
final class MethodAccessor implements AccessorInterface
{
public function __construct(private string $property)
{
}
/**
* @throws ValidatorLogicException
*
* @return mixed
*/
public function getValue(object $object)
{
$getMethodName = 'get'.ucfirst($this->property);
$hasMethodName = 'has'.ucfirst($this->property);
$isMethodName = 'is'.ucfirst($this->property);
if (method_exists($object, $getMethodName)) {
return $object->{$getMethodName}();
}
if (method_exists($object, $hasMethodName)) {
return $object->{$hasMethodName}();
}
if (method_exists($object, $isMethodName)) {
return $object->{$isMethodName}();
}
throw ValidatorLogicException::createMissingMethod(
$object::class,
[$getMethodName, $hasMethodName, $isMethodName]
);
}
}