Completed
Push — master ( 530e5d...e2eae8 )
by Andreas
17:20
created

midcom_services_rcs_history::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
6
 */
7
8
/**
9
 * Cached revision history for the object
10
 *
11
 * @package midcom.services.rcs
12
 */
13
class midcom_services_rcs_history
14
{
15
    /**
16
     * @var array
17
     */
18
    private $data;
19
20
    public function __construct(array $history)
21
    {
22
        $this->data = $history;
23
    }
24
25
    /**
26
     * Returns all versions (newest first)
27
     */
28
    public function all() : array
29
    {
30
        return $this->data;
31
    }
32
33
    /**
34
     * Check if a revision exists
35
     */
36
    public function version_exists(string $version) : bool
37
    {
38
        return array_key_exists($version, $this->data);
39
    }
40
41
    /**
42
     * Get the previous versionID
43
     */
44
    public function get_prev_version(string $version) : ?string
45
    {
46
        $versions = array_keys($this->data);
47
        $position = array_search($version, $versions);
48
49
        if (in_array($position, [false, count($versions) - 1], true)) {
50
            return null;
51
        }
52
        return $versions[$position + 1];
53
    }
54
55
    /**
56
     * Get the next versionID
57
     */
58
    public function get_next_version(string $version) : ?string
59
    {
60
        $versions = array_keys($this->data);
61
        $position = array_search($version, $versions);
62
63
        if (in_array($position, [false, 0], true)) {
64
            return null;
65
        }
66
        return $versions[$position - 1];
67
    }
68
69
    /**
70
     * Get the metadata of one revision.
71
     */
72
    public function get($revision) : ?array
73
    {
74
        return $this->data[$revision] ?? null;
75
    }
76
77
    /**
78
     * Get the metadata of the first (oldest) revision.
79
     */
80
    public function get_first() : ?array
81
    {
82
        return end($this->data) ?: null;
83
    }
84
85
    /**
86
     * Get the metadata of the last (newest) revision.
87
     */
88
    public function get_last() : ?array
89
    {
90
        return reset($this->data) ?: null;
91
    }
92
}
93