Parser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php declare(strict_types = 1);
2
/**
3
 * Main IntervalParser class
4
 *
5
 * PHP version 7+
6
 *
7
 * @category   IntervalParser
8
 * @author     Ekin H. Bayar <[email protected]>
9
 * @version    0.2.0
10
 */
11
namespace IntervalParser;
12
13
class Parser
14
{
15
    /**
16
     * @var Normalizer
17
     */
18
    private $normalizer;
19
20
    /**
21
     * Creates instance
22
     *
23
     * @param Normalizer $normalizer
24
     */
25
    public function __construct(Normalizer $normalizer)
26
    {
27
        $this->normalizer = $normalizer;
28
    }
29
30
    /**
31
     * Normalizes any non-strtotime-compatible time string, then validates the interval and returns a DateInterval object.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
32
     * No leading or trailing data is accepted.
33
     *
34
     * @param string $input
35
     * @return \DateInterval
36
     * @throws FormatException
37
     */
38
    public function parse(string $input): \DateInterval
39
    {
40
        $input = trim($this->normalizer->normalize($input));
41
42
        $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')';
43
        $expression = $definition . Pattern::INTERVAL_ONLY;
44
45
        if (preg_match($expression, $input, $matches)) {
46
            return \DateInterval::createFromDateString($input);
47
        }
48
49
        throw new FormatException("Given string is not a valid time interval.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Given string is not a valid time interval. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
50
    }
51
}
52