Completed
Push — master ( 3cf61a...455b58 )
by Saulius
01:58
created

configure_object()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 4
nop 3
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 6
rs 8.5906
c 0
b 0
f 0
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
            $valueAssigned = false;
26 2
            foreach ($methodPrefixes as $setterMethodPrefix) {
27
28 2
                $setterMethod = concat_object_method($setterMethodPrefix, $property);
29
30 2
                if (\method_exists($object, $setterMethod)) {
31 1
                    $object->$setterMethod($value);
32 2
                    $valueAssigned = true;
33
                }
34
            }
35
36 2
            if (false === $valueAssigned) {
37 2
                $object->$property = $value;
38
            }
39
        }
40
41 1
        return $object;
42 1
    } catch (\Throwable $t) {
43 1
        throw new PropertyNotAccessibleException($t->getMessage());
44
    }
45
}
46
47
/**
48
 * @return null|mixed
49
 *
50
 * @throws PropertyNotAccessibleException
51
 */
52
function get_object_property_value(object $object, string $property, array $methodPrefixes = ['get', 'is'])
53
{
54
    try {
55 3
        foreach ($methodPrefixes as $getterMethodPrefix) {
56 3
            $getterMethod = concat_object_method($getterMethodPrefix, $property);
57
58 3
            if (\method_exists($object, $getterMethod)) {
59 3
                return $object->$getterMethod();
60
            }
61
        }
62
63 3
        return $object->$property;
64 1
    } catch (\Throwable $t) {
65 1
        throw new PropertyNotAccessibleException($t->getMessage());
66
    }
67
}
68
69
function concat_object_method(string $prefix, string $property): string
70
{
71 5
    return $prefix.ucfirst($property);
72
}
73
74
/**
75
 * @param mixed $value
76
 *
77
 * @throws ClassPropertyNotSetException
78
 */
79
function set_object_property_value(object $object, string $property, $value): void
80
{
81
    try {
82 2
        $reflectionClass = new \ReflectionClass($object);
83 2
        $reflectionProperty = $reflectionClass->getProperty($property);
84 1
        $reflectionProperty->setAccessible(true);
85 1
        $reflectionProperty->setValue($object, $value);
86 1
    } catch (\Exception $e) {
87 1
        throw new ClassPropertyNotSetException(
88 1
            sprintf('Failed to set `%s` class `%s` property value.', \get_class($object), $property)
89
        );
90
    }
91
}
92