|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Baleen\Migrations\Version\Collection\Resolver; |
|
21
|
|
|
|
|
22
|
|
|
use Baleen\Migrations\Version\Collection\Collection; |
|
23
|
|
|
use Baleen\Migrations\Version\VersionInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class OffsetResolver. |
|
27
|
|
|
* |
|
28
|
|
|
* Resolves aliases in the format: ID{OPERATOR}[COUNT] |
|
29
|
|
|
* |
|
30
|
|
|
* Operators: |
|
31
|
|
|
* + will add |
|
32
|
|
|
* -, ^ or ~ will subtract |
|
33
|
|
|
* |
|
34
|
|
|
* Repeat operators consecutively works as a shortcut for COUNT. E.g. ++ will set COUNT to 2. |
|
35
|
|
|
* |
|
36
|
|
|
* Count (optional) should be a number if present and takes precedence over the previous rule. |
|
37
|
|
|
* |
|
38
|
|
|
* Example aliases: 123+, 123++ (same as 123+2), 123+++9 (will be simplified to 123+9) |
|
39
|
|
|
* |
|
40
|
|
|
* @author Gabriel Somoza <[email protected]> |
|
41
|
|
|
*/ |
|
42
|
|
|
final class OffsetResolver extends AbstractResolver |
|
43
|
|
|
{ |
|
44
|
|
|
const PATTERN = '/^(.*?)([\+\-\~\^]+)([0-9]+)?$/'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @{inheritdoc} |
|
48
|
|
|
* |
|
49
|
|
|
* IMPROVE: this method has an NPath complexity of 400. The configured NPath complexity threshold is 200. |
|
50
|
|
|
* |
|
51
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $alias |
|
54
|
|
|
* @param Collection $collection |
|
55
|
|
|
* |
|
56
|
|
|
* @return VersionInterface|null |
|
57
|
|
|
*/ |
|
58
|
65 |
|
protected function doResolve($alias, Collection $collection) |
|
59
|
|
|
{ |
|
60
|
|
|
// parse alias |
|
61
|
65 |
|
$matches = []; |
|
62
|
65 |
|
if (!preg_match(self::PATTERN, $alias, $matches)) { |
|
63
|
36 |
|
return null; |
|
64
|
|
|
} |
|
65
|
29 |
|
list(, $newAlias, $operator) = $matches; |
|
66
|
|
|
|
|
67
|
|
|
// resolve the new alias (this will allow to resolve e.g. HEAD-1) |
|
68
|
29 |
|
$absoluteVersion = $collection->get($newAlias); |
|
69
|
29 |
|
if (null === $absoluteVersion) { |
|
70
|
4 |
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// calculate the offset |
|
74
|
25 |
|
$count = !isset($matches[3]) ? strlen($operator) : (int) $matches[3]; |
|
75
|
25 |
|
if (strlen($operator) > 1) { |
|
76
|
8 |
|
$operator = substr($operator, 0, 1); |
|
77
|
|
|
} |
|
78
|
25 |
|
$multiplier = $operator === '+' ? 1 : -1; |
|
79
|
25 |
|
$offset = $count * $multiplier; |
|
80
|
|
|
|
|
81
|
|
|
// find version by absolute getPosition + offset |
|
82
|
25 |
|
$absolutePos = $collection->getPosition($absoluteVersion); |
|
83
|
25 |
|
return $collection->getByPosition($absolutePos + $offset); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|