Completed
Push — master ( 53b99e...05e097 )
by Basil
03:48
created

ObjectHelper::isInstanceOf()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.2728
c 0
b 0
f 0
cc 5
nc 10
nop 3
1
<?php
2
3
namespace luya\helpers;
4
5
use luya\Exception;
6
use ReflectionMethod;
7
8
/**
9
 * Helper methods when dealing with Objects.
10
 *
11
 * @author Basil Suter <[email protected]>
12
 * @since 1.0.0
13
 */
14
class ObjectHelper
15
{
16
    /**
17
     * Checks a given variable if its an instance of an element in the $instances list.
18
     *
19
     * @param string $variable The variable to type check against instances.
20
     * @param string|array|object $instances A list of classes, a string for a given class, or an object.
21
     * @param boolean $throwException Whether an exception should be thrown or not.
22
     * @throws \luya\Exception
23
     * @return boolean
24
     * @since 1.0.3
25
     */
26
    public static function isInstanceOf($variable, $instances, $throwException = true)
27
    {
28
        // if instances is an object (compare object directly) we have to extra the class name to compare with instanceof later
29
        if (is_object($instances)) {
30
            $instances = get_class($instances);
31
        }
32
        
33
        $instances = (array) $instances;
34
        
35
        foreach ($instances as $class) {
36
            if ($variable instanceof $class) {
37
                return true;
38
            }
39
        }
40
        
41
        if ($throwException) {
42
            throw new Exception("The given variable must be an instance of: " . implode(",", $instances));
43
        }
44
        
45
        return false;
46
    }
47
    
48
    /**
49
     * Convert Object to Array
50
     *
51
     * @param object $object
52
     * @return array
53
     */
54
    public static function toArray($object)
55
    {
56
        return (array) $object;
57
    }
58
    
59
    /**
60
     * Call a method and ensure arguments.
61
     *
62
     * Call a class method with arguments and verify the arguments if they are in the list of method arguments or not.
63
     *
64
     * ```php
65
     * ObjectHelper::callMethodSanitizeArguments(new MyClass(), 'methodToCall', ['paramName' => 'paramValue']);
66
     * ```
67
     *
68
     * The response is the return value from the called method of the object.
69
     *
70
     * @param object $object The class object where the method must be found.
71
     * @param string $method The class method to call inside the object.
72
     * @param array $argumentsList A massiv assigned list of array items, where the key is bind to the method argument and the value to be passed in the method on call.
73
     * @throws \luya\Exception Throws an exception if a argument coult not be found.
74
     * @return mixed
75
     */
76
    public static function callMethodSanitizeArguments($object, $method, array $argumentsList = [])
77
    {
78
        // get class reflection object
79
        $reflection = new ReflectionMethod($object, $method);
80
        // array where the sanitized arguemnts will be stored
81
        $methodArgs = [];
82
83
        foreach ($reflection->getParameters() as $param) {
84
            // add the argument into the method list when existing
85
            if (array_key_exists($param->name, $argumentsList)) {
86
                $methodArgs[] = $argumentsList[$param->name];
87
            }
88
            // check if the provided arguemnt is optional or not
89
            if (!$param->isOptional() && !array_key_exists($param->name, $argumentsList)) {
90
                throw new Exception(sprintf("The argument '%s' is required for method '%s' in class '%s'.", $param->name, $method, get_class($object)));
91
            }
92
        }
93
94
        return call_user_func_array([$object, $method], $methodArgs);
95
    }
96
}
97