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