Completed
Push — master ( 2efa59...1e4da7 )
by Ryan
07:10
created

Evaluator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
c 2
b 1
f 0
lcom 0
cbo 1
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C evaluate() 0 30 7
A isTraversable() 0 4 1
1
<?php namespace Anomaly\Streams\Platform\Support;
2
3
use Illuminate\Contracts\Container\Container;
4
5
/**
6
 * Class Evaluator
7
 *
8
 * @link    http://anomaly.is/streams-platform
9
 * @author  AnomalyLabs, Inc. <[email protected]>
10
 * @author  Ryan Thompson <[email protected]>
11
 * @package Anomaly\Streams\Platform\Support
12
 */
13
class Evaluator
14
{
15
16
    /**
17
     * The IoC container.
18
     *
19
     * @var \Illuminate\Contracts\Container\Container
20
     */
21
    protected $container;
22
23
    /**
24
     * Create a new Evaluator instance.
25
     *
26
     * @param Container $container
27
     */
28
    public function __construct(Container $container)
29
    {
30
        $this->container = $container;
31
    }
32
33
    /**
34
     * Evaluate a target entity with arguments.
35
     *
36
     * @param        $target
37
     * @param  array $arguments
38
     * @return mixed
39
     */
40
    public function evaluate($target, array $arguments = [])
41
    {
42
        /**
43
         * If the target is an instance of Closure then
44
         * call through the IoC it with the arguments.
45
         */
46
        if ($target instanceof \Closure) {
47
            return $this->container->call($target, $arguments);
48
        }
49
50
        /**
51
         * If the target is an array then evaluate
52
         * each of it's values.
53
         */
54
        if (is_array($target)) {
55
            foreach ($target as &$value) {
56
                $value = $this->evaluate($value, $arguments);
57
            }
58
        }
59
60
        /**
61
         * if the target is a string and is in a traversable
62
         * format then traverse the target using the arguments.
63
         */
64
        if (is_string($target) && !isset($arguments[$target]) && $this->isTraversable($target)) {
65
            $target = data_get($arguments, $target, $target);
66
        }
67
68
        return $target;
69
    }
70
71
    /**
72
     * Check if a string is in a traversable format.
73
     *
74
     * @param  $target
75
     * @return bool
76
     */
77
    protected function isTraversable($target)
78
    {
79
        return (!preg_match('/[^a-z._]/', $target));
80
    }
81
}
82