Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
42.86% |
9 / 21 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
FlowActions | |
42.86% |
9 / 21 |
|
25.00% |
1 / 4 |
38.87 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getActions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasValue | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
getValue | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
5.27 |
1 | <?php |
2 | |
3 | namespace Flow; |
4 | |
5 | use Flow\Data\Utils\MultiDimArray; |
6 | |
7 | class FlowActions { |
8 | /** |
9 | * @var MultiDimArray |
10 | */ |
11 | protected $actions; |
12 | |
13 | /** |
14 | * @param array $actions |
15 | */ |
16 | public function __construct( array $actions ) { |
17 | $this->actions = new MultiDimArray(); |
18 | $this->actions[] = $actions; |
19 | } |
20 | |
21 | /** |
22 | * @return string[] |
23 | */ |
24 | public function getActions() { |
25 | return array_keys( $this->actions->all() ); |
26 | } |
27 | |
28 | /** |
29 | * Function can be overloaded depending on how deep the desired value is. |
30 | * |
31 | * @param string $action |
32 | * @param string $type |
33 | * @return bool True when the requested parameter exists and is not null |
34 | */ |
35 | public function hasValue( $action, $type = null /* [, $option = null [, ...]] */ ) { |
36 | $arguments = func_get_args(); |
37 | try { |
38 | return isset( $this->actions[$arguments] ); |
39 | } catch ( \OutOfBoundsException $e ) { |
40 | // Do nothing; the whole remainder of this method is fail-case. |
41 | } |
42 | |
43 | /* |
44 | * If no value is found, check if the action is not actually referencing |
45 | * another action (for BC reasons), then try fetching the requested data |
46 | * from that action. |
47 | */ |
48 | try { |
49 | $referencedAction = $this->actions[$action]; |
50 | if ( is_string( $referencedAction ) && $referencedAction != $action ) { |
51 | // Replace action name in arguments. |
52 | $arguments[0] = $referencedAction; |
53 | return isset( $this->actions[$arguments] ); |
54 | } |
55 | } catch ( \OutOfBoundsException $e ) { |
56 | // Do nothing; the whole remainder of this method is fail-case. |
57 | } |
58 | |
59 | return false; |
60 | } |
61 | |
62 | /** |
63 | * Function can be overloaded depending on how deep the desired value is. |
64 | * |
65 | * @param string $action |
66 | * @param string $type |
67 | * @return mixed|null Requested value or null if missing |
68 | */ |
69 | public function getValue( $action, $type = null /* [, $option = null [, ...]] */ ) { |
70 | $arguments = func_get_args(); |
71 | |
72 | try { |
73 | return $this->actions[$arguments]; |
74 | } catch ( \OutOfBoundsException $e ) { |
75 | // Do nothing; the whole remainder of this method is fail-case. |
76 | } |
77 | |
78 | /* |
79 | * If no value is found, check if the action is not actually referencing |
80 | * another action (for BC reasons), then try fetching the requested data |
81 | * from that action. |
82 | */ |
83 | try { |
84 | $referencedAction = $this->actions[$action]; |
85 | if ( is_string( $referencedAction ) && $referencedAction != $action ) { |
86 | // Remove action name from arguments |
87 | array_shift( $arguments ); |
88 | |
89 | return $this->getValue( $referencedAction, ...$arguments ); |
90 | } |
91 | } catch ( \OutOfBoundsException $e ) { |
92 | // Do nothing; the whole remainder of this method is fail-case. |
93 | } |
94 | |
95 | return null; |
96 | } |
97 | } |