Completed
Push — master ( e31061...1587a0 )
by Joschi
02:44
created

SelectorFactory::castInt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Factory;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Domain\Model\Object\Revision;
41
use Apparat\Object\Domain\Repository\InvalidArgumentException;
42
use Apparat\Object\Domain\Repository\Selector as RepositorySelector;
43
44
/**
45
 * Object selector factory
46
 *
47
 * @package Apparat\Object
48
 * @subpackage Apparat\Object\Domain
49
 */
50
class SelectorFactory
51
{
52
    /**
53
     * Date PCRE pattern
54
     *
55
     * @var array
56
     * @see ObjectUrl::$datePattern
57
     */
58
    protected static $datePattern = [
59
        'Y' => '/(?P<year>\d{4}|\*)',
60
        'm' => '(?:/(?P<month>\d{2}|\*)',
61
        'd' => '(?:/(?P<day>\d{2}|\*)',
62
        'H' => '(?:/(?P<hour>\d{2}|\*)',
63
        'i' => '(?:/(?P<minute>\d{2}|\*)',
64
        's' => '(?:/(?P<second>\d{2}|\*)',
65
    ];
66
67
    /**
68
     * Parse and instantiate an object selector
69
     *
70
     * @param string $selector String selector
71
     * @return \Apparat\Object\Domain\Repository\Selector Object selector
72
     * @throws InvalidArgumentException If the selector is invalid
73
     */
74 11
    public static function createFromString($selector)
75
    {
76 11
        $datePrecision = intval(getenv('OBJECT_DATE_PRECISION'));
77 11
        $selectorPattern = '/(?P<id>(?:\d+)|\*)\.(?P<type>(?:[a-z]+)|\*)(?:/\\k<id>(?:-(?P<revision>\d+))?)?';
78
79
        // If the creation date is used as selector component
80 11
        if ($datePrecision) {
81 11
            $selectorPattern = '(?:'.$selectorPattern.str_repeat(')?', $datePrecision);
82 11
            $selectorPattern = implode('', array_slice(self::$datePattern, 0, $datePrecision)).$selectorPattern;
83
        }
84 11
        $selectorPattern = '%^'.$selectorPattern.'$%';
85
86
        // If the selector is invalid
87 11
        if (!strlen($selector) ||
88 11
            !preg_match(
89
                $selectorPattern,
90
                $selector,
91
                $selectorParts
92
            ) ||
93 11
            !strlen($selectorParts[0])
94
        ) {
95 1
            throw new InvalidArgumentException(
96 1
                sprintf('Invalid repository selector "%s"', $selector),
97 1
                InvalidArgumentException::INVALID_REPOSITORY_SELECTOR
98
            );
99
        }
100
101 10
        $year = $month = $day = $hour = $minute = $second = null;
102 10
        if (($datePrecision > 0)) {
103 10
            $year = isset($selectorParts['year']) ? self::castInt(
104 10
                $selectorParts['year']
105 10
            ) : RepositorySelector::WILDCARD;
106
        }
107 10
        if (($datePrecision > 1)) {
108 10
            $month = isset($selectorParts['month']) ? self::castInt(
109 8
                $selectorParts['month']
110 10
            ) : RepositorySelector::WILDCARD;
111
        }
112 10
        if (($datePrecision > 2)) {
113 10
            $day = isset($selectorParts['day']) ? self::castInt($selectorParts['day']) : RepositorySelector::WILDCARD;
114
        }
115 10
        if (($datePrecision > 3)) {
116 3
            $hour = isset($selectorParts['hour']) ? self::castInt(
117 2
                $selectorParts['hour']
118 3
            ) : RepositorySelector::WILDCARD;
119
        }
120 10
        if (($datePrecision > 4)) {
121 3
            $minute = isset($selectorParts['minute']) ? self::castInt(
122 2
                $selectorParts['minute']
123 3
            ) : RepositorySelector::WILDCARD;
124
        }
125 10
        if (($datePrecision > 5)) {
126 3
            $second = isset($selectorParts['second']) ? self::castInt(
127 2
                $selectorParts['second']
128 3
            ) : RepositorySelector::WILDCARD;
129
        }
130 10
        $uid = isset($selectorParts['id']) ? self::castInt($selectorParts['id']) : RepositorySelector::WILDCARD;
131
132 10
        $type = empty($selectorParts['type']) ? RepositorySelector::WILDCARD : trim($selectorParts['type']);
133 10
        $revision = (isset($selectorParts['revision']) && strlen($selectorParts['revision'])) ? intval(
134 2
            $selectorParts['revision']
135 10
        ) : Revision::CURRENT;
136
137 10
        return Kernel::create(
138 10
            RepositorySelector::class,
139 10
            [$year, $month, $day, $hour, $minute, $second, $uid, $type, $revision]
140
        );
141
    }
142
143
    /**
144
     * Cast a value as integer if it's not a wildcard
145
     *
146
     * @param string $value Value
147
     * @return int|string Integer value or wildcard
148
     */
149 10
    protected static function castInt($value)
150
    {
151 10
        return ($value === RepositorySelector::WILDCARD) ? $value : intval($value);
152
    }
153
}
154