Completed
Pull Request — wip/steps-v9 (#92)
by
unknown
04:42
created

SubstepDefinition::hash()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 1
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Form\Definition\Step\Step\Substep;
15
16
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent;
17
use Romm\Formz\Form\Definition\Condition\Activation;
18
use Romm\Formz\Form\Definition\Condition\ActivationInterface;
19
use Romm\Formz\Form\Definition\Step\Step\DivergenceStepDefinition;
20
use Romm\Formz\Form\Definition\Step\Step\Step;
21
22
class SubstepDefinition extends AbstractFormDefinitionComponent
23
{
24
    /**
25
     * @var string
26
     * @validate NotEmpty
27
     */
28
    protected $substep;
29
30
    /**
31
     * @var \Romm\Formz\Form\Definition\Condition\Activation
32
     * @validate Romm.Formz:Internal\ConditionIsValid
33
     */
34
    protected $activation;
35
36
    /**
37
     * @var \Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition
38
     */
39
    protected $next;
40
41
    /**
42
     * @var \Romm\Formz\Form\Definition\Step\Step\Substep\DivergenceSubstepDefinition[]
43
     */
44
    protected $divergence;
45
46
    /**
47
     * @return Substep
48
     */
49
    public function getSubstep()
50
    {
51
        return $this->withFirstParent(
52
            Substeps::class,
53
            function (Substeps $substeps) {
54
                return $substeps->getEntry($this->substep);
55
            }
56
        );
57
    }
58
59
    /**
60
     * @return Activation
61
     */
62
    public function getActivation()
63
    {
64
        return $this->activation;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function hasActivation()
71
    {
72
        return $this->activation instanceof ActivationInterface;
73
    }
74
75
    public function getLevel()
76
    {
77
        $level = 1;
78
79
        $this->alongParents(function ($parent) use (&$level) {
80
            if ($parent instanceof self) {
81
                $level++;
82
            }
83
84
            if ($parent instanceof Step) {
85
                return false;
86
            }
87
88
            return true;
89
        });
90
91
        return $level;
92
    }
93
94
    /**
95
     * @todo
96
     *
97
     * @return bool
98
     */
99
    public function isLast()
100
    {
101
        return false === $this->hasNextSubstep()
102
            && false === $this->hasDivergence();
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function hasNextSubstep()
109
    {
110
        return null !== $this->next;
111
    }
112
113
    /**
114
     * @return SubstepDefinition
115
     */
116
    public function getNextSubstep()
117
    {
118
        if (false === $this->hasNextSubstep()) {
119
            throw new \Exception('todo'); // @todo
120
        }
121
122
        return $this->next;
123
    }
124
125
    /**
126
     * @return DivergenceSubstepDefinition[]
127
     */
128
    public function getDivergenceSubsteps()
129
    {
130
        return $this->divergence;
131
    }
132
133
    /**
134
     * @return bool
135
     */
136
    public function hasDivergence()
137
    {
138
        return false === empty($this->divergence);
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function hash()
145
    {
146
        return serialize([
147
            $this->substep,
148
            (function () {
149
                if (!$this->activation) {
150
                    return null;
151
                }
152
153
                return [
154
                    $this->activation->getExpression(),
155
                    $this->activation->getAllConditions(),
156
                ];
157
            })(),
158
            $this->next ? $this->next->hash() : null,
159
            (function () {
160
                if (!$this->divergence) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->divergence of type Romm\Formz\Form\Definiti...enceSubstepDefinition[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
161
                    return null;
162
                }
163
164
                return array_map(function (DivergenceStepDefinition $divergenceStepDefinition) {
165
                    return $divergenceStepDefinition->hash();
166
                }, $this->divergence);
167
            })(),
168
        ]);
169
    }
170
}
171