Completed
Push — master ( c8a849...ce79e5 )
by Nikola
02:29
created

AbstractParser::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * This file is part of the Version package.
5
 *
6
 * Copyright (c) Nikola Posa <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Version\Constraint\Parser;
13
14
use Version\Constraint\ConstraintInterface;
15
use Version\Constraint\Constraint;
16
use Version\Version;
17
use Version\Exception\InvalidConstraintStringException;
18
use Version\Exception\Exception;
19
20
/**
21
 * @author Nikola Posa <[email protected]>
22
 */
23
abstract class AbstractParser implements ParserInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $constraintString;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 13
    public function parse($constraintString)
34
    {
35 13
        if (!is_string($constraintString)) {
36 1
            throw InvalidConstraintStringException::forInvalidType($constraintString);
37
        }
38
39 12
        $constraintString = trim($constraintString);
40
41 12
        if ($constraintString == '') {
42 1
            throw InvalidConstraintStringException::forEmptyCostraintString();
43
        }
44
45 11
        $this->constraintString = $constraintString;
46
47 11
        return $this->doParse();
48
    }
49
50
    /**
51
     * @return ConstraintInterface
52
     */
53
    abstract protected function doParse();
54
55 4
    protected function error()
56
    {
57 4
        throw InvalidConstraintStringException::forConstraintString($this->constraintString);
58
    }
59
60
    /**
61
     * @param string $constraintStringUnit
62
     * @return Constraint
63
     */
64 11
    protected function buildConstraintFromStringUnit($constraintStringUnit)
65
    {
66 11
        list($operator, $operandString) = array_values(
67 11
            $this->parseConstraintStringUnit($constraintStringUnit)
68 11
        );
69
70 11
        if (empty($operandString)) {
71 1
            $this->error();
72
        }
73
74
        try {
75 10
            return Constraint::fromProperties(
76 10
                $operator ?: Constraint::OPERATOR_EQ,
77 10
                Version::fromString($operandString)
78 8
            );
79 2
        } catch (Exception $ex) {
80 2
            $this->error();
81
        }
82
    }
83
84
    /**
85
     * @param string $constraintStringUnit
86
     * @return array
87
     */
88 11
    protected function parseConstraintStringUnit($constraintStringUnit)
89
    {
90 11
        $i = 0;
91 11
        while (isset($constraintStringUnit[$i]) && !ctype_digit($constraintStringUnit[$i])) {
92 10
            $i++;
93 10
        }
94
95 11
        $operator = substr($constraintStringUnit, 0, $i);
96 11
        $operand = substr($constraintStringUnit, $i);
97
98
        return [
99 11
            'operator' => $operator,
100 11
            'operand' => $operand,
101 11
        ];
102
    }
103
}
104