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