Validator   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 19
c 6
b 0
f 1
lcom 3
cbo 2
dl 0
loc 212
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setOperator() 0 4 1
A setDataType() 0 4 1
A setPrompt() 0 6 1
A setError() 0 6 1
A allowBlank() 0 4 1
A onInvalidStop() 0 4 1
A onInvalidWarn() 0 4 1
A onInvalidInfo() 0 4 1
A setFormula1() 0 4 1
A setFormula2() 0 4 1
A getOptions() 0 22 3
A setShowDropDown() 0 4 1
A getData() 0 16 1
A packFormula() 0 12 3
1
<?php
2
3
namespace Xls;
4
5
class Validator
6
{
7
    const OP_BETWEEN = 0x00;
8
    const OP_NOTBETWEEN = 0x01;
9
    const OP_EQUAL = 0x02;
10
    const OP_NOTEQUAL = 0x03;
11
    const OP_GT = 0x04;
12
    const OP_LT = 0x05;
13
    const OP_GTE = 0x06;
14
    const OP_LTE = 0x07;
15
16
    const TYPE_ANY = 0x00;
17
    const TYPE_INTEGER = 0x01;
18
    const TYPE_DECIMAL = 0x02;
19
    const TYPE_USER_LIST = 0x03;
20
    const TYPE_DATE = 0x04;
21
    const TYPE_TIME = 0x05;
22
    const TYPE_TEXT_LENGTH = 0x06;
23
    const TYPE_FORMULA = 0x07;
24
25
    const ERROR_STOP = 0x00;
26
    const ERROR_WARNING = 0x01;
27
    const ERROR_INFO = 0x02;
28
29
    protected $dataType = self::TYPE_INTEGER;
30
    protected $errorStyle = self::ERROR_STOP;
31
    protected $allowBlank = false;
32
    protected $showDropDown = false;
33
    protected $showPrompt = false;
34
    protected $showError = true;
35
    protected $titlePrompt = "\x00";
36
    protected $descrPrompt = "\x00";
37
    protected $titleError = "\x00";
38
    protected $descrError = "\x00";
39
    protected $operator = self::OP_BETWEEN;
40
    protected $formula1 = '';
41
    protected $formula2 = '';
42
43
    /**
44
     * The parser from the workbook. Used to parse validation formulas also
45
     *
46
     * @var FormulaParser
47
     */
48
    protected $formulaParser;
49
50
    /**
51
     * @param FormulaParser $formulaParser
52
     */
53
    public function __construct(FormulaParser $formulaParser)
54
    {
55
        $this->formulaParser = $formulaParser;
56
    }
57
58
    /**
59
     * @param int $operator
60
     */
61
    public function setOperator($operator)
62
    {
63
        $this->operator = $operator;
64
    }
65
66
    /**
67
     * @param int $dataType
68
     */
69
    public function setDataType($dataType)
70
    {
71
        $this->dataType = $dataType;
72
    }
73
74
    /**
75
     * @param string $promptTitle
76
     * @param string $promptDescription
77
     * @param bool   $showPrompt
78
     */
79
    public function setPrompt($promptTitle = "\x00", $promptDescription = "\x00", $showPrompt = true)
80
    {
81
        $this->showPrompt = $showPrompt;
82
        $this->titlePrompt = $promptTitle;
83
        $this->descrPrompt = $promptDescription;
84
    }
85
86
    /**
87
     * @param string $errorTitle
88
     * @param string $errorDescription
89
     * @param bool   $showError
90
     */
91
    public function setError($errorTitle = "\x00", $errorDescription = "\x00", $showError = true)
92
    {
93
        $this->showError = $showError;
94
        $this->titleError = $errorTitle;
95
        $this->descrError = $errorDescription;
96
    }
97
98
    /**
99
     *
100
     */
101
    public function allowBlank()
102
    {
103
        $this->allowBlank = true;
104
    }
105
106
    /**
107
     *
108
     */
109
    public function onInvalidStop()
110
    {
111
        $this->errorStyle = self::ERROR_STOP;
112
    }
113
114
    /**
115
     *
116
     */
117
    public function onInvalidWarn()
118
    {
119
        $this->errorStyle = self::ERROR_WARNING;
120
    }
121
122
    /**
123
     *
124
     */
125
    public function onInvalidInfo()
126
    {
127
        $this->errorStyle = self::ERROR_INFO;
128
    }
129
130
    /**
131
     * @param $formula
132
     *
133
     */
134
    public function setFormula1($formula)
135
    {
136
        $this->formula1 = $formula;
137
    }
138
139
    /**
140
     * @param $formula
141
     *
142
     */
143
    public function setFormula2($formula)
144
    {
145
        $this->formula2 = $formula;
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getOptions()
152
    {
153
        $options = 0x00;
154
155
        $options |= $this->dataType;
156
        $options |= $this->errorStyle << 4;
157
158
        if ($this->dataType === self::TYPE_USER_LIST
159
            && preg_match('/^\".*\"$/', $this->formula1)
160
        ) {
161
            //explicit list options, separated by comma
162
            $options |= 0x01 << 7;
163
        }
164
165
        $options |= intval($this->allowBlank) << 8;
166
        $options |= intval(!$this->showDropDown) << 9;
167
        $options |= intval($this->showPrompt) << 18;
168
        $options |= intval($this->showError) << 19;
169
        $options |= $this->operator << 20;
170
171
        return $options;
172
    }
173
174
    /**
175
     * @param boolean $showDropDown
176
     */
177
    public function setShowDropDown($showDropDown = true)
178
    {
179
        $this->showDropDown = $showDropDown;
180
    }
181
182
    /**
183
     * @param Range $range
184
     *
185
     * @return string
186
     */
187
    public function getData(Range $range)
188
    {
189
        $data = pack("V", $this->getOptions());
190
191
        $data .= pack("vC", strlen($this->titlePrompt), 0x00) . $this->titlePrompt;
192
        $data .= pack("vC", strlen($this->titleError), 0x00) . $this->titleError;
193
        $data .= pack("vC", strlen($this->descrPrompt), 0x00) . $this->descrPrompt;
194
        $data .= pack("vC", strlen($this->descrError), 0x00) . $this->descrError;
195
196
        $data .= $this->packFormula($this->formula1);
197
        $data .= $this->packFormula($this->formula2);
198
199
        $data .= \Xls\Subrecord\Range::getData(array($range));
200
201
        return $data;
202
    }
203
204
    protected function packFormula($formula)
205
    {
206
        if ($this->dataType === self::TYPE_USER_LIST) {
207
            $formula = str_replace(',', chr(0), $formula);
208
        }
209
210
        if ($formula != '') {
211
            $formula = $this->formulaParser->getReversePolish($formula);
212
        }
213
214
        return pack("vv", strlen($formula), 0x00) . $formula;
215
    }
216
}
217