Passed
Push — master ( 386efc...fcbe67 )
by Sam
02:35
created

Variable::setWidth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
3
namespace SPSS\Sav;
4
5
use SPSS\Utils;
6
7
class Variable
8
{
9
    // const TYPE_NUMERIC = 1;
10
    // const TYPE_STRING = 2;
11
12
    const FORMAT_TYPE_A = 1;
13
    const FORMAT_TYPE_AHEX = 2;
14
    const FORMAT_TYPE_COMMA = 3;
15
    const FORMAT_TYPE_DOLLAR = 4;
16
    const FORMAT_TYPE_F = 5;
17
    const FORMAT_TYPE_IB = 6;
18
    const FORMAT_TYPE_PIBHEX = 7;
19
    const FORMAT_TYPE_P = 8;
20
    const FORMAT_TYPE_PIB = 9;
21
    const FORMAT_TYPE_PK = 10;
22
    const FORMAT_TYPE_RB = 11;
23
    const FORMAT_TYPE_RBHEX = 12;
24
    const FORMAT_TYPE_Z = 15;
25
    const FORMAT_TYPE_N = 16;
26
    const FORMAT_TYPE_E = 17;
27
    const FORMAT_TYPE_DATE = 20;
28
    const FORMAT_TYPE_TIME = 21;
29
    const FORMAT_TYPE_DATETIME = 22;
30
    const FORMAT_TYPE_ADATE = 23;
31
    const FORMAT_TYPE_JDATE = 24;
32
    const FORMAT_TYPE_DTIME = 25;
33
    const FORMAT_TYPE_WKDAY = 26;
34
    const FORMAT_TYPE_MONTH = 27;
35
    const FORMAT_TYPE_MOYR = 28;
36
    const FORMAT_TYPE_QYR = 29;
37
    const FORMAT_TYPE_WKYR = 30;
38
    const FORMAT_TYPE_PCT = 31;
39
    const FORMAT_TYPE_DOT = 32;
40
    const FORMAT_TYPE_CCA = 33;
41
    const FORMAT_TYPE_CCB = 34;
42
    const FORMAT_TYPE_CCC = 35;
43
    const FORMAT_TYPE_CCD = 36;
44
    const FORMAT_TYPE_CCE = 37;
45
    const FORMAT_TYPE_EDATE = 38;
46
    const FORMAT_TYPE_SDATE = 39;
47
48
    const ALIGN_LEFT = 0;
49
    const ALIGN_RIGHT = 1;
50
    const ALIGN_CENTER = 2;
51
52
    const MEASURE_UNKNOWN = 0;
53
    const MEASURE_NOMINAL = 1;
54
    const MEASURE_ORDINAL = 2;
55
    const MEASURE_SCALE = 3;
56
57
    const ROLE_INPUT = 0;
58
    const ROLE_TARGET = 1;
59
    const ROLE_BOTH = 2;
60
    const ROLE_NONE = 3;
61
    const ROLE_PARTITION = 4;
62
    const ROLE_SPLIT = 5;
63
64
    private $name;
65
    private $width = 8;
66
    public $decimals = 0;
67
    public $format = 0;
68
    public $columns;
69
    public $alignment;
70
    public $measure;
71
    public $role;
72
    public $label;
73
    public $values = [];
74
    public $missing = [];
75
76
    /**
77
     * @var array
78
     */
79
    public $attributes = [
80
        // '$@Role' => self::ROLE_BOTH
81
    ];
82
83
    /**
84
     * @var array
85
     */
86
    public $data = [];
87
88
    /**
89
     * Variable constructor.
90
     *
91
     * @param array $data
92
     */
93
    public function __construct(string $name, $data = [])
94
    {
95
        foreach ($data as $key => $value) {
96
            $this->{$key} = $value;
97
        }
98
        $this->name = $name;
99
    }
100
101
    public function setName(string $name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
    public function setName(/** @scrutinizer ignore-unused */ string $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        #if (! preg_match('/^[A-Za-z0-9_]+$/', $var->name)) {
104
        # UTF-8 and '.' characters could pass here
105
        if (! preg_match('/^[A-Za-z0-9_\.\x{4e00}-\x{9fa5}]+$/u', $this->name)) {
106
            throw new \InvalidArgumentException(
107
                sprintf('Variable name `%s` contains an illegal character.', $this->name)
108
            );
109
        }
110
    }
111
112
    public function setWidth(int $width)
113
    {
114
        if ($width <= 0) {
115
            throw new \InvalidArgumentException(
116
                sprintf('Invalid field width. Should be an integer number greater than zero.')
117
            );
118
        }
119
        $this->width = $width;
120
    }
121
122
    public function getName(): string
123
    {
124
        return $this->name;
125
    }
126
127
    public function getWidth(): int
128
    {
129
        return $this->width;
130
    }
131
132
    public function getOcts(): int
133
    {
134
        return Utils::widthToOcts($this->width);
135
    }
136
137
    /**
138
     * @param int $format
139
     * @return bool
140
     */
141
    public static function isNumberFormat($format)
142
    {
143
        return in_array($format, [
144
            self::FORMAT_TYPE_COMMA,
145
            self::FORMAT_TYPE_F,
146
            self::FORMAT_TYPE_DATETIME
147
        ]);
148
    }
149
150
    /**
151
     * This method returns the print / write format code of a variable.
152
     * The returned value is a tuple consisting of the format abbreviation
153
     * (string <= 8 chars) and a meaning (long string).
154
     * Non-existent codes have a (null, null) tuple returned.
155
     *
156
     * @param integer $format
157
     * @return array
158
     */
159
    public static function getFormatInfo($format)
160
    {
161
        switch ($format) {
162
            case 0:
163
                return ['', 'Continuation of string variable'];
164
            case self::FORMAT_TYPE_A:
165
                return ['A', 'Alphanumeric'];
166
            case self::FORMAT_TYPE_AHEX:
167
                return ['AHEX', 'alphanumeric hexadecimal'];
168
            case self::FORMAT_TYPE_COMMA:
169
                return ['COMMA', 'F format with commas'];
170
            case self::FORMAT_TYPE_DOLLAR:
171
                return ['DOLLAR', 'Commas and floating point dollar sign'];
172
            case self::FORMAT_TYPE_F:
173
                return ['F', 'F (default numeric) format'];
174
            case self::FORMAT_TYPE_IB:
175
                return ['IB', 'Integer binary'];
176
            case self::FORMAT_TYPE_PIBHEX:
177
                return ['PIBHEX', 'Positive binary integer - hexadecimal'];
178
            case self::FORMAT_TYPE_P:
179
                return ['P', 'Packed decimal'];
180
            case self::FORMAT_TYPE_PIB:
181
                return ['PIB', 'Positive integer binary (Unsigned)'];
182
            case self::FORMAT_TYPE_PK:
183
                return ['PK', 'Positive packed decimal (Unsigned)'];
184
            case self::FORMAT_TYPE_RB:
185
                return ['RB', 'Floating point binary'];
186
            case self::FORMAT_TYPE_RBHEX:
187
                return ['RBHEX', 'Floating point binary - hexadecimal'];
188
            case self::FORMAT_TYPE_Z:
189
                return ['Z', 'Zoned decimal'];
190
            case self::FORMAT_TYPE_N:
191
                return ['N', 'N format - unsigned with leading zeros'];
192
            case self::FORMAT_TYPE_E:
193
                return ['E', 'E format - with explicit power of ten'];
194
            case self::FORMAT_TYPE_DATE:
195
                return ['DATE', 'Date format dd-mmm-yyyy'];
196
            case self::FORMAT_TYPE_TIME:
197
                return ['TIME', 'Time format hh:mm:ss.s'];
198
            case self::FORMAT_TYPE_DATETIME:
199
                return ['DATETIME', 'Date and time'];
200
            case self::FORMAT_TYPE_ADATE:
201
                return ['ADATE', 'Date in mm/dd/yyyy form'];
202
            case self::FORMAT_TYPE_JDATE:
203
                return ['JDATE', 'Julian date - yyyyddd'];
204
            case self::FORMAT_TYPE_DTIME:
205
                return ['DTIME', 'Date-time dd hh:mm:ss.s'];
206
            case self::FORMAT_TYPE_WKDAY:
207
                return ['WKDAY', 'Day of the week'];
208
            case self::FORMAT_TYPE_MONTH:
209
                return ['MONTH', 'Month'];
210
            case self::FORMAT_TYPE_MOYR:
211
                return ['MOYR', 'mmm yyyy'];
212
            case self::FORMAT_TYPE_QYR:
213
                return ['QYR', 'q Q yyyy'];
214
            case self::FORMAT_TYPE_WKYR:
215
                return ['WKYR', 'ww WK yyyy'];
216
            case self::FORMAT_TYPE_PCT:
217
                return ['PCT', 'Percent - F followed by "%"'];
218
            case self::FORMAT_TYPE_DOT:
219
                return ['DOT', 'Like COMMA, switching dot for comma'];
220
            case self::FORMAT_TYPE_CCA:
221
                return ['CCA', 'User-programmable currency format (1)'];
222
            case self::FORMAT_TYPE_CCB:
223
                return ['CCB', 'User-programmable currency format (2)'];
224
            case self::FORMAT_TYPE_CCC:
225
                return ['CCC', 'User-programmable currency format (3)'];
226
            case self::FORMAT_TYPE_CCD:
227
                return ['CCD', 'User-programmable currency format (4)'];
228
            case self::FORMAT_TYPE_CCE:
229
                return ['CCE', 'User-programmable currency format (5)'];
230
            case self::FORMAT_TYPE_EDATE:
231
                return ['EDATE', 'Date in dd.mm.yyyy style'];
232
            case self::FORMAT_TYPE_SDATE:
233
                return ['SDATE', 'Date in yyyy/mm/dd style'];
234
        }
235
236
        return [null, null];
237
    }
238
239
    /**
240
     * @param int $alignment
241
     * @return string
242
     */
243
    public static function alignmentToString($alignment)
244
    {
245
        switch ($alignment) {
246
            case self::ALIGN_LEFT:
247
                return 'Left';
248
            case self::ALIGN_RIGHT:
249
                return 'Right';
250
            case self::ALIGN_CENTER:
251
                return 'Center';
252
        }
253
254
        return 'Invalid';
255
    }
256
257
    /**
258
     * @return int
259
     */
260
    public function getMeasure()
261
    {
262
        if ($this->measure !== null) {
263
            return $this->measure;
264
        }
265
266
        return $this->width == 0 ? self::MEASURE_UNKNOWN : self::MEASURE_NOMINAL;
267
    }
268
269
    /**
270
     * @return int
271
     */
272
    public function getAlignment()
273
    {
274
        if ($this->alignment !== null) {
275
            return $this->alignment;
276
        }
277
278
        return $this->width == 0 ? self::ALIGN_RIGHT : self::ALIGN_LEFT;
279
    }
280
281
    /**
282
     * @return int
283
     */
284
    public function getColumns()
285
    {
286
        if ($this->columns !== null) {
287
            return $this->columns;
288
        }
289
290
        return 8;
291
    }
292
}
293