-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.php
More file actions
154 lines (138 loc) · 3.62 KB
/
Log.php
File metadata and controls
154 lines (138 loc) · 3.62 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Phug\Split\Git;
use ArrayAccess;
use ArrayObject;
use Countable;
use Exception;
use IteratorAggregate;
/**
* @implements IteratorAggregate<int, Commit>
* @implements ArrayAccess<int, Commit>
*/
class Log implements IteratorAggregate, ArrayAccess, Countable
{
/**
* List of git commits.
*
* @var Commit[]
*/
protected $commits = [];
/**
* Retrieve an external iterator.
*
* @link https://php.net/manual/en/iteratoraggregate.getiterator.php
*
* @return ArrayObject<int, Commit> An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
*/
public function getIterator(): ArrayObject
{
return new ArrayObject($this->commits);
}
public function __construct(array $commits)
{
if (!count($commits)) {
throw new EmptyLogList();
}
foreach ($commits as $commit) {
if (!($commit instanceof Commit)) {
$type = gettype($commit);
if ($type === 'object') {
$type = get_class($commit);
}
throw new InvalidGitLogUnit($type);
}
}
$this->commits = $commits;
}
/**
* Create a Log instance (list of Commit) from a git log commit string.
*
* @param string $log raw string from git log command.
*
* @throws InvalidGitLogStringException|Exception
*
* @suppressWarnings(PHPMD.StaticAccess)
*
* @return self
*/
public static function fromGitLogString(string $log): self
{
$commitLogs = preg_split('/[\n\r](?=commit )/', $log);
return new self($commitLogs === false ? [] : array_map(static function (string $commitLog) {
return Commit::fromGitLogString($commitLog);
}, $commitLogs));
}
/**
* Whether an offset exists.
*
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param int $offset An offset to check for.
*
* @return bool true on success or false on failure.
*/
public function offsetExists($offset): bool
{
return isset($this->commits[$offset]);
}
/**
* Offset to retrieve.
*
* @link https://php.net/manual/en/arrayaccess.offsetget.php
*
* @param int $offset The offset to retrieve.
*
* @return Commit
*/
public function offsetGet($offset): Commit
{
return $this->commits[$offset];
}
/**
* Change a commit of the list is forbidden.
*
* @link https://php.net/manual/en/arrayaccess.offsetset.php
*
* @param int $offset The offset to assign the value to.
* @param Commit $value The value to set.
*
* @throws ImmutableLogException
*
* @suppressWarnings(PHPMD.UnusedFormalParameter)
*
* @return void
*/
public function offsetSet($offset, $value): void
{
throw new ImmutableLogException();
}
/**
* Remove a commit of the list is forbidden.
*
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
*
* @param int $offset The offset to unset.
*
* @throws ImmutableLogException
*
* @suppressWarnings(PHPMD.UnusedFormalParameter)
*
* @return void
*/
public function offsetUnset($offset): void
{
throw new ImmutableLogException();
}
/**
* Count elements of an object.
*
* @link https://php.net/manual/en/countable.count.php
*
* @return int The custom count as an integer.
*/
public function count(): int
{
return count($this->commits);
}
}