Completed
Push — master ( 2df407...de4379 )
by Neomerx
05:05
created

StringToDateTime::toBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php namespace Limoncello\Validation\Rules\Converters;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of 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,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use DateTimeImmutable;
20
use DateTimeInterface;
21
use Limoncello\Validation\Contracts\Errors\ErrorCodes;
22
use Limoncello\Validation\Contracts\Execution\ContextInterface;
23
use Limoncello\Validation\Rules\ExecuteRule;
24
25
/**
26
 * @package Limoncello\Validation
27
 */
28
final class StringToDateTime extends ExecuteRule
29
{
30
    /**
31
     * Property key.
32
     */
33
    const PROPERTY_FORMAT = self::PROPERTY_LAST + 1;
34
35
    /**
36
     * @param string $format
37
     */
38 3
    public function __construct(string $format)
39
    {
40 3
        assert(!empty($format));
41
42 3
        parent::__construct([
43 3
            self::PROPERTY_FORMAT => $format,
44
        ]);
45
    }
46
47
    /**
48
     * @param mixed            $value
49
     * @param ContextInterface $context
50
     *
51
     * @return array
52
     *
53
     * @SuppressWarnings(PHPMD.StaticAccess)
54
     */
55 2
    public static function execute($value, ContextInterface $context): array
56
    {
57 2
        $format = $context->getProperties()->getProperty(self::PROPERTY_FORMAT);
58 2
        if (is_string($value) === true && ($parsed = static::parseFromFormat($value, $format)) !== null) {
0 ignored issues
show
Comprehensibility introduced by
Since Limoncello\Validation\Ru...erters\StringToDateTime is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
59 1
            return static::createSuccessReply($parsed);
60 2
        } elseif ($value instanceof DateTimeInterface) {
61 1
            return static::createSuccessReply($value);
62
        }
63
64 2
        return static::createErrorReply(
65 2
            $context,
66 2
            $value,
67 2
            ErrorCodes::IS_DATE_TIME,
68 2
            [self::PROPERTY_FORMAT => $format]
69
        );
70
    }
71
72
    /**
73
     * @param string $input
74
     * @param string $format
75
     *
76
     * @return DateTimeInterface|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use DateTimeImmutable|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
77
     *
78
     * @SuppressWarnings(PHPMD.StaticAccess)
79
     */
80 2
    private static function parseFromFormat(string $input, string $format)
81
    {
82 2
        $parsedOrNull = null;
83
84 2
        if (($value = DateTimeImmutable::createFromFormat($format, $input)) !== false) {
85 1
            $parsedOrNull = $value;
86
        }
87
88 2
        return $parsedOrNull;
89
    }
90
}
91