Revision::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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\Model\Object;
38
39
use Apparat\Object\Domain\Contract\SerializablePropertyInterface;
40
41
/**
42
 * Object revision
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Domain
46
 */
47
class Revision implements SerializablePropertyInterface
48
{
49
    /**
50
     * Current revision
51
     *
52
     * @var null
53
     */
54
    const CURRENT = null;
55
    /**
56
     * Object revision number
57
     *
58
     * @var int|null
59
     */
60
    protected $revision = null;
61
    /**
62
     * Draft revision
63
     *
64
     * @var bool
65
     */
66
    protected $draft = false;
67
68
    /**
69
     * Revision constructor
70
     *
71
     * @param int $revision Object revision number
72
     * @param bool $draft Draft revision
73
     */
74 81
    public function __construct($revision, $draft = false)
75
    {
76
        // If the revision number is invalid
77 81
        if (!self::isValidRevision($revision)) {
78 1
            throw new InvalidArgumentException(
79 1
                sprintf('Invalid object revision number "%s"', $revision),
80 1
                InvalidArgumentException::INVALID_OBJECT_REVISION
81
            );
82
        }
83
84 80
        $this->revision = $revision;
85 80
        $this->draft = boolval($draft);
86 80
    }
87
88
    /**
89
     * Test whether a revision number is valid
90
     *
91
     * @param int|NULL $revision Revision number
92
     * @return bool Is valid revision
93
     */
94 117
    public static function isValidRevision($revision)
95
    {
96 117
        return ($revision === self::CURRENT) || (is_int($revision) && ($revision >= 1));
97
    }
98
99
    /**
100
     * Return a current revision instance
101
     *
102
     * @param boolean $draft Draft mode
103
     * @return Revision Current revision instance
104
     */
105 51
    public static function current($draft = false)
106
    {
107 51
        return new static(self::CURRENT, $draft);
108
    }
109
110
    /**
111
     * Unserialize the string representation of this property
112
     *
113
     * @param string $str Serialized property
114
     * @return Revision Revision property
115
     */
116 46
    public static function unserialize($str)
117
    {
118 46
        return new static(intval($str));
119
    }
120
121
    /**
122
     * Test whether this is the current revision
123
     *
124
     * @return bool Is current revision
125
     */
126 41
    public function isCurrent()
127
    {
128 41
        return $this->revision === self::CURRENT;
129
    }
130
131
    /**
132
     * Serialize the property
133
     *
134
     * @return mixed Property serialization
135
     */
136 2
    public function serialize()
137
    {
138 2
        return $this->getRevision();
139
    }
140
141
    /**
142
     * Return the object revision number
143
     *
144
     * @return int|null Object revision number
145
     */
146 53
    public function getRevision()
147
    {
148 53
        return $this->revision;
149
    }
150
151
    /**
152
     * Return an incremented revision
153
     *
154
     * @return Revision Incremented revision
155
     */
156 6
    public function increment()
157
    {
158 6
        return ($this->revision === self::CURRENT) ? $this : new static($this->revision + 1);
159
    }
160
161
    /**
162
     * Compare this revision to a given one
163
     *
164
     * @param Revision $revision Comparison revision
165
     * @return bool This revision equals the given one
166
     */
167 41
    public function equals(Revision $revision)
168
    {
169 41
        return ($this->revision == $revision->getRevision()) && ($this->draft == $revision->isDraft());
170
    }
171
172
    /**
173
     * Return whether this is a draft revision
174
     *
175
     * @return bool Draft revision
176
     */
177 51
    public function isDraft()
178
    {
179 51
        return $this->draft;
180
    }
181
182
    /**
183
     * Enable / disable the draft flag
184
     *
185
     * @param boolean $draft Enable / disable the draft flag
186
     * @return Revision Self reference
187
     */
188 47
    public function setDraft($draft)
189
    {
190 47
        $draft = boolval($draft);
191 47
        if ($draft !== $this->draft) {
192 22
            return new static($this->revision, $draft);
193
        }
194 33
        return $this;
195
    }
196
}
197