Completed
Push — master ( 126f4a...229ca6 )
by Basil
08:38
created

ObjectHelper::instanceOf()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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