Completed
Push — master ( 77b530...acb106 )
by Jonathan
02:05
created

Timezone::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Validate\Rule;
22
23
/**
24
 * Tests strings as valid timezone identifier.
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class Timezone implements \Caridea\Validate\Rule
30
{
31
    /**
32
     * @var array The comparison value
33
     */
34
    private $zones;
35
36
    /**
37
     * Creates a new Timezone Rule.
38
     */
39 1
    protected function __construct()
40
    {
41 1
        $this->zones = timezone_identifiers_list(\DateTimeZone::ALL_WITH_BC);
42 1
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 1
    public function apply($value, $data = []): ?array
48
    {
49 1
        if (!is_string($value)) {
50 1
            return ['FORMAT_ERROR'];
51
        }
52 1
        return in_array($value, $this->zones, true) ? null : ['WRONG_TIMEZONE'];
53
    }
54
55
    /**
56
     * Gets a rule that tests a string as a valid timezone.
57
     *
58
     * @return \Caridea\Validate\Rule\Timezone the created rule
59
     */
60 1
    public static function timezone(): Timezone
61
    {
62 1
        return new Timezone();
63
    }
64
}
65