Passed
Push — master ( 9cead0...545cea )
by
unknown
12:43
created

ConditionalizableTrait::condition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Ui;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Provides an entity with a condition.
9
 *
10
 * Implementation of {@see \Charcoal\Ui\ConditionalizableInterface}
11
 */
12
trait ConditionalizableTrait
13
{
14
    /**
15
     * Condition needed to render the entity.
16
     *
17
     * @var string|boolean
18
     */
19
    private $condition;
20
21
    /**
22
     * rendered condition.
23
     *
24
     * @var string|boolean
25
     */
26
    private $resolvedCondition;
27
28
    /**
29
     * @return boolean
30
     */
31
    public function resolvedCondition()
32
    {
33
        if (!isset($this->resolvedCondition)) {
34
            if (!isset($this->condition)) {
35
                $this->resolvedCondition = true;
36
            } else {
37
                $this->resolvedCondition = $this->parseConditionalLogic(
38
                    $this->condition()
39
                );
40
            }
41
        }
42
43
        return $this->resolvedCondition;
44
    }
45
46
    /**
47
     * @return boolean|string
48
     */
49
    public function condition()
50
    {
51
        return $this->condition;
52
    }
53
54
    /**
55
     * @param boolean|string $condition A condition to evaluate.
56
     * @throws InvalidArgumentException If the condition is not a string nor boolean.
57
     * @return self
58
     */
59
    public function setCondition($condition)
60
    {
61
        if (!is_bool($condition) && !is_string($condition)) {
62
            throw new InvalidArgumentException(
63
                'Condition must be a string or boolean'
64
            );
65
        }
66
67
        unset($this->resolvedCondition);
68
        $this->condition = $condition;
69
        return $this;
70
    }
71
72
    /**
73
     * Resolve the conditional logic.
74
     *
75
     * @param  mixed $condition The condition.
76
     * @return boolean|null
77
     */
78
    final protected function parseConditionalLogic($condition)
79
    {
80
        if ($condition === null) {
81
            return null;
82
        }
83
84
        if (is_bool($condition)) {
85
            return $condition;
86
        }
87
88
        $not = false;
89
        if (is_string($condition)) {
90
            $not = ($condition[0] === '!');
91
            if ($not) {
92
                $condition = ltrim($condition, '!');
93
            }
94
        }
95
96
        $result = $this->resolveConditionalLogic($condition);
97
98
        return $not ? !$result : $result;
99
    }
100
101
    /**
102
     * Parse the widget's conditional logic.
103
     *
104
     * @param  callable|string $condition The callable or renderable condition.
105
     * @return boolean
106
     */
107
    protected function resolveConditionalLogic($condition)
108
    {
109
        if (is_callable([ $this, $condition ])) {
110
            return !!$this->{$condition}();
111
        } elseif (is_callable($condition)) {
112
            return !!$condition();
113
        } elseif ($this->form()->obj()->view()) {
0 ignored issues
show
Bug introduced by
It seems like form() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
114
            return !!$this->form()->obj()->renderTemplate($condition);
0 ignored issues
show
Bug introduced by
It seems like form() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
115
        }
116
117
        return !!$condition;
118
    }
119
}
120