1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/helpers package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2018 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Sauls\Component\Helper; |
14
|
|
|
|
15
|
|
|
use Sauls\Component\Helper\Exception\ClassPropertyNotSetException; |
16
|
|
|
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @throws PropertyNotAccessibleException |
20
|
|
|
*/ |
21
|
|
|
function configure_object(object $object, array $properties, array $methodPrefixes = ['set', 'add']): object |
22
|
|
|
{ |
23
|
|
|
try { |
24
|
2 |
|
foreach ($properties as $property => $value) { |
25
|
2 |
|
if (false === object_assign_value_using_setter_methods($object, [$property, $value], $methodPrefixes)) { |
26
|
2 |
|
$object->$property = $value; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
return $object; |
31
|
1 |
|
} catch (\Throwable $t) { |
32
|
1 |
|
throw new PropertyNotAccessibleException($t->getMessage()); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function object_assign_value_using_setter_methods(object $object, array $parameters, array $methodPrefixes): bool |
37
|
|
|
{ |
38
|
2 |
|
[$property, $value] = $parameters; |
39
|
2 |
|
foreach ($methodPrefixes as $setterMethodPrefix) { |
40
|
|
|
|
41
|
2 |
|
$setterMethod = concat_object_method($setterMethodPrefix, $property); |
42
|
|
|
|
43
|
2 |
|
if (\method_exists($object, $setterMethod)) { |
44
|
1 |
|
$object->$setterMethod($value); |
45
|
2 |
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return null|mixed |
54
|
|
|
* |
55
|
|
|
* @throws PropertyNotAccessibleException |
56
|
|
|
*/ |
57
|
|
|
function get_object_property_value(object $object, string $property, array $methodPrefixes = ['get', 'is']) |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
3 |
|
foreach ($methodPrefixes as $getterMethodPrefix) { |
61
|
3 |
|
$getterMethod = concat_object_method($getterMethodPrefix, $property); |
62
|
|
|
|
63
|
3 |
|
if (\method_exists($object, $getterMethod)) { |
64
|
3 |
|
return $object->$getterMethod(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
return $object->$property; |
69
|
1 |
|
} catch (\Throwable $t) { |
70
|
1 |
|
throw new PropertyNotAccessibleException($t->getMessage()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function concat_object_method(string $prefix, string $property): string |
75
|
|
|
{ |
76
|
5 |
|
return $prefix.ucfirst($property); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $value |
81
|
|
|
* |
82
|
|
|
* @throws ClassPropertyNotSetException |
83
|
|
|
*/ |
84
|
|
|
function set_object_property_value(object $object, string $property, $value): void |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
2 |
|
$reflectionClass = new \ReflectionClass($object); |
88
|
2 |
|
$reflectionProperty = $reflectionClass->getProperty($property); |
89
|
1 |
|
$reflectionProperty->setAccessible(true); |
90
|
1 |
|
$reflectionProperty->setValue($object, $value); |
91
|
1 |
|
} catch (\Exception $e) { |
92
|
1 |
|
throw new ClassPropertyNotSetException( |
93
|
1 |
|
sprintf('Failed to set `%s` class `%s` property value.', \get_class($object), $property) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|