-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVersionCollection.php
127 lines (104 loc) · 2.71 KB
/
VersionCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* This file is part of the PHPCR Migrations package
*
* (c) Daniel Leech <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPCR\Migrations;
class VersionCollection
{
private $versions;
public function __construct(array $versions)
{
ksort($versions, SORT_STRING);
$this->versions = $versions;
}
public function has($versionTimestamp)
{
return isset($this->versions[$versionTimestamp]);
}
public function getAllVersions()
{
return $this->versions;
}
public function getVersions($from, $to)
{
$direction = $from > $to ? 'down' : 'up';
$result = [];
$versions = $this->versions;
if ($from == $to) {
return [];
}
if ('up' === $direction) {
ksort($versions, SORT_STRING);
} else {
krsort($versions, SORT_STRING);
}
$found = null === $from ? true : false;
foreach ($versions as $timestamp => $version) {
if ($timestamp == $from) {
$found = true;
if ('down' == $direction) {
$result[$timestamp] = $version;
}
continue;
}
if (false === $found) {
continue;
}
if ($timestamp == $to) {
if ('up' == $direction) {
$result[$timestamp] = $version;
}
break;
}
$result[$timestamp] = $version;
}
return $result;
}
public function getLatestVersion()
{
end($this->versions);
return key($this->versions);
}
/**
* Return the version after the given version.
*
* @param string|null $from
*/
public function getNextVersion($from)
{
$found = false;
foreach (array_keys($this->versions) as $timestamp) {
if (null === $from) {
return $timestamp;
}
if ($timestamp == $from) {
$found = true;
continue;
}
if ($found) {
return $timestamp;
}
}
}
/**
* Return the version before the given version.
*
* @param string $from
*/
public function getPreviousVersion($from)
{
$lastTimestamp = 0;
foreach (array_keys($this->versions) as $timestamp) {
if ($timestamp == $from) {
return $lastTimestamp;
}
$lastTimestamp = $timestamp;
}
return 0;
}
}