Completed
Pull Request — master (#16)
by Hannes
05:53
created

Version::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
 * <https://github.com/baleen/migrations>.
18
 */
19
20
namespace Baleen\Migrations;
21
22
use Baleen\Migrations\Exception\InvalidArgumentException;
23
use Baleen\Migrations\Migration\MigrationInterface;
24
use Baleen\Migrations\Version\LinkedVersion;
25
use Baleen\Migrations\Version\VersionInterface;
26
27
/**
28
 * {@inheritDoc}
29
 *
30
 * @author Gabriel Somoza <[email protected]>
31
 */
32
class Version implements VersionInterface
33
{
34
    /**
35
     * @var string
36
     */
37
    private $id;
38
39
    /**
40
     * @var bool
41
     */
42
    private $migrated;
43
44
    /**
45
     * @param $id string
46
     * @param bool $migrated
47
     *
48
     * @throws InvalidArgumentException
49
     */
50 78
    public function __construct($id, $migrated = false)
51
    {
52 78
        $id = trim((string) $id);
53 78
        if (empty($id)) {
54 1
            throw new InvalidArgumentException('A version\'s id cannot be empty.');
55
        }
56 77
        $this->id = $id;
57 77
        $this->migrated = (bool) $migrated;
58 77
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 126
    final public function getId()
64
    {
65 126
        return $this->id;
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71 68
    final public function isMigrated()
72
    {
73 68
        return $this->migrated;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 35
    final public function setMigrated($migrated)
80
    {
81 35
        $this->migrated = (bool) $migrated;
82 35
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 1
    final public function withMigration(MigrationInterface $migration)
88
    {
89 1
        return new LinkedVersion($this->getId(), $this->isMigrated(), $migration);
90
    }
91
92
    /**
93
     * Creates a list of versions based on specified IDs.
94
     *
95
     * @param mixed $versionIds
96
     *
97
     * @param bool $migrated
98
     * @param null $migration
99
     * @return Version\VersionInterface[]
100
     */
101 15
    final public static function fromArray($versionIds, $migrated = false, $migration = null)
102
    {
103 15
        $results = [];
104 15
        foreach ($versionIds as $id) {
105 15
            if (!is_string($id)) {
106 11
                $id = 'v' . (string) $id;
107 11
            }
108
            $class = null === $migration ? static::class : LinkedVersion::class;
109
            $results[] = new $class($id, $migrated, $migration);
110
        }
111
112
        return $results;
113
    }
114
115
    /**
116
     * __toString
117
     * @return string
118
     */
119
    final public function __toString()
120
    {
121
        return $this->getId();
122
    }
123
}
124