Expression::hasCondition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Charcoal\Source;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-core'
8
use Charcoal\Source\AbstractExpression;
9
use Charcoal\Source\ExpressionInterface;
10
use Charcoal\Source\RawExpressionInterface;
11
12
/**
13
 * Represents a query expression.
14
 */
15
class Expression extends AbstractExpression implements
16
    RawExpressionInterface
17
{
18
    /**
19
     * Custom query expression.
20
     *
21
     * @var mixed
22
     */
23
    protected $condition;
24
25
    /**
26
     * Set the expression data.
27
     *
28
     * @param  array<string,mixed> $data The expression data;
29
     *     as an associative array.
30
     * @return self
31
     */
32
    public function setData(array $data)
33
    {
34
        parent::setData($data);
35
36
        if (isset($data['condition'])) {
37
            $this->setCondition($data['condition']);
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * Retrieve the default values.
45
     *
46
     * @return array<string,mixed> An associative array.
47
     */
48
    public function defaultData()
49
    {
50
        return [
51
            'condition' => null,
52
            'active'    => true,
53
            'name'      => null,
54
        ];
55
    }
56
57
    /**
58
     * Retrieve the expression data structure.
59
     *
60
     * @return array<string,mixed> An associative array.
61
     */
62
    public function data()
63
    {
64
        return [
65
            'condition' => $this->condition(),
66
            'active'    => $this->active(),
67
            'name'      => $this->name(),
68
        ];
69
    }
70
71
    /**
72
     * Set the custom query expression.
73
     *
74
     * @param  string|null $condition The custom query expression.
75
     * @throws InvalidArgumentException If the parameter is not a valid string expression.
76
     * @return self
77
     */
78
    public function setCondition($condition)
79
    {
80
        if ($condition === null) {
81
            $this->condition = $condition;
82
            return $this;
83
        }
84
85
        if (!is_string($condition)) {
0 ignored issues
show
introduced by
The condition is_string($condition) is always true.
Loading history...
86
            throw new InvalidArgumentException(
87
                'Custom expression must be a string.'
88
            );
89
        }
90
91
        $condition = trim($condition);
92
        if ($condition === '') {
93
            $condition = null;
94
        }
95
96
        $this->condition = $condition;
97
        return $this;
98
    }
99
100
    /**
101
     * Determine if the expression has a custom condition.
102
     *
103
     * @return boolean
104
     */
105
    public function hasCondition()
106
    {
107
        return !(empty($this->condition) && !is_numeric($this->condition));
108
    }
109
110
    /**
111
     * Retrieve the custom query expression.
112
     *
113
     * @return mixed
114
     */
115
    public function condition()
116
    {
117
        return $this->condition;
118
    }
119
}
120