|
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; |
|
21
|
|
|
|
|
22
|
|
|
use Baleen\Migrations\Exception\InvalidArgumentException; |
|
23
|
|
|
use Baleen\Migrations\Migration\MigrationInterface; |
|
24
|
|
|
use Baleen\Migrations\Shared\ValueObjectInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The simplest form of a Version |
|
28
|
|
|
* |
|
29
|
|
|
* @author Gabriel Somoza <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
final class VersionId implements ValueObjectInterface |
|
32
|
|
|
{ |
|
33
|
|
|
const HASH_ALGORITHM = 'sha1'; |
|
34
|
|
|
|
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
private $hash; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* VersionId constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $hash |
|
42
|
|
|
* @throws InvalidArgumentException |
|
43
|
|
|
*/ |
|
44
|
140 |
|
public function __construct($hash) |
|
45
|
|
|
{ |
|
46
|
140 |
|
$hash = (string) $hash; |
|
47
|
140 |
|
if (empty($hash)) { |
|
48
|
1 |
|
throw new InvalidArgumentException('A version\'s hash cannot be empty.'); |
|
49
|
|
|
} |
|
50
|
139 |
|
$this->hash = $hash; |
|
51
|
139 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns a string representation of the value. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
158 |
|
public function toString() |
|
59
|
|
|
{ |
|
60
|
158 |
|
return $this->hash; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
11 |
|
public function isSameValueAs(ValueObjectInterface $id) |
|
67
|
|
|
{ |
|
68
|
11 |
|
if (!$id instanceof VersionId) { |
|
69
|
1 |
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
10 |
|
return $this->toString() === $id->toString(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* __toString |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
58 |
|
public function __toString() |
|
80
|
|
|
{ |
|
81
|
58 |
|
return $this->toString(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* fromArray |
|
86
|
|
|
* @param $ids |
|
87
|
|
|
* @return VersionId[] |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function fromArray(array $ids) { |
|
90
|
5 |
|
return array_map(function ($id) { |
|
91
|
4 |
|
return self::fromNative($id); |
|
92
|
5 |
|
}, $ids); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritdoc |
|
97
|
|
|
* |
|
98
|
|
|
* @throws InvalidArgumentException |
|
99
|
|
|
*/ |
|
100
|
27 |
|
public static function fromNative($value) |
|
101
|
|
|
{ |
|
102
|
27 |
|
$str = (string) $value; |
|
103
|
27 |
|
if (empty($str)) { |
|
104
|
5 |
|
throw new InvalidArgumentException( |
|
105
|
|
|
'Refusing to create a VersionId from an empty string or any other type of value that casts into an ' . |
|
106
|
5 |
|
'empty string.' |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
22 |
|
return new static(hash(self::HASH_ALGORITHM, $str)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Creates a VersionId based on a migration class. |
|
115
|
|
|
* |
|
116
|
|
|
* @param MigrationInterface $migration |
|
117
|
|
|
* |
|
118
|
|
|
* @return VersionId |
|
119
|
|
|
* |
|
120
|
|
|
* @throws InvalidArgumentException |
|
121
|
|
|
*/ |
|
122
|
17 |
|
public static function fromMigration(MigrationInterface $migration) |
|
123
|
|
|
{ |
|
124
|
17 |
|
$class = get_class($migration); |
|
125
|
17 |
|
return self::fromNative($class); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|