Completed
Push — master ( 6fedad...267515 )
by Basil
15:23
created

ObjectHelper::instanceOf()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
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 array.
18
     *
19
     * @param string $variable The variable to type check against instances.
20
     * @param string|array $instances A list of classes
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 instanceOf($variable, $instances, $throwException = true)
27
    {
28
        $instances = (array) $instances;
29
        
30
        foreach ($instances as $class) {
31
            if ($variable instanceof  $class) {
32
                return true;
33
            }
34
        }
35
        
36
        if ($throwException) {
37
            throw new Exception("The variable must be an instance of: " . implode(",", $instances));
38
        }
39
        
40
        return false;
41
    }
42
    
43
    /**
44
     * Convert Object to Array
45
     *
46
     * @param object $object
47
     * @return array
48
     */
49
    public static function toArray($object)
50
    {
51
        return (array) $object;
52
    }
53
    
54
    /**
55
     * Call a method and ensure arguments.
56
     *
57
     * Call a class method with arguments and verify the arguments if they are in the list of method arguments or not.
58
     *
59
     * ```php
60
     * ObjectHelper::callMethodSanitizeArguments(new MyClass(), 'methodToCall', ['paramName' => 'paramValue']);
61
     * ```
62
     *
63
     * @param object $object The class object where the method must be found.
64
     * @param string $method The class method to call inside the object.
65
     * @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.
66
     * @throws \luya\Exception Throws an exception if a argument coult not be found.
67
     * @return object
68
     */
69
    public static function callMethodSanitizeArguments($object, $method, array $argumentsList = [])
70
    {
71
        // get class reflection object
72
        $reflection = new ReflectionMethod($object, $method);
73
        // array where the sanitized arguemnts will be stored
74
        $methodArgs = [];
75
76
        foreach ($reflection->getParameters() as $param) {
77
            // add the argument into the method list when existing
78
            if (array_key_exists($param->name, $argumentsList)) {
79
                $methodArgs[] = $argumentsList[$param->name];
80
            }
81
            // check if the provided arguemnt is optional or not
82
            if (!$param->isOptional() && !array_key_exists($param->name, $argumentsList)) {
83
                throw new Exception(sprintf("The argument '%s' is required for method '%s' in class '%s'.", $param->name, $method, get_class($object)));
84
            }
85
        }
86
87
        return call_user_func_array([$object, $method], $methodArgs);
88
    }
89
}
90