1 | <?php |
||
33 | class Log implements \ArrayAccess, \Countable, \Iterator |
||
34 | { |
||
35 | /** |
||
36 | * @var \GitElephant\Repository |
||
37 | */ |
||
38 | private $repository; |
||
39 | |||
40 | /** |
||
41 | * the commits related to this log |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $commits = []; |
||
46 | |||
47 | /** |
||
48 | * the cursor position |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | private $position = 0; |
||
53 | |||
54 | /** |
||
55 | * static method to generate standalone log |
||
56 | * |
||
57 | * @param \GitElephant\Repository $repository repo |
||
58 | * @param array $outputLines output lines from command.log |
||
59 | * |
||
60 | * @return \GitElephant\Objects\Log |
||
61 | */ |
||
62 | 1 | public static function createFromOutputLines(Repository $repository, array $outputLines): \GitElephant\Objects\Log |
|
63 | { |
||
64 | 1 | $log = new self($repository); |
|
65 | 1 | $log->parseOutputLines($outputLines); |
|
66 | |||
67 | 1 | return $log; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Class constructor |
||
72 | * |
||
73 | * @param Repository $repository |
||
74 | * @param string $ref |
||
75 | * @param string|null $path |
||
76 | * @param int $limit |
||
77 | * @param int|null $offset |
||
78 | * @param bool $firstParent |
||
79 | */ |
||
80 | 19 | public function __construct( |
|
81 | Repository $repository, |
||
82 | $ref = 'HEAD', |
||
83 | $path = null, |
||
84 | int $limit = 15, |
||
85 | int $offset = null, |
||
86 | bool $firstParent = false |
||
87 | ) { |
||
88 | 19 | $this->repository = $repository; |
|
89 | 19 | $this->createFromCommand($ref, $path, $limit, $offset, $firstParent); |
|
90 | 19 | } |
|
91 | |||
92 | /** |
||
93 | * get the commit properties from command |
||
94 | * |
||
95 | * @param string $ref treeish reference |
||
96 | * @param string $path path |
||
97 | * @param int $limit limit |
||
98 | * @param string $offset offset |
||
99 | * @param boolean $firstParent first parent |
||
100 | * |
||
101 | * @throws \RuntimeException |
||
102 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
103 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
104 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
105 | * @see ShowCommand::commitInfo |
||
106 | */ |
||
107 | 19 | private function createFromCommand($ref, $path = null, int $limit = null, int $offset = null, bool $firstParent = false): void |
|
119 | |||
120 | 19 | private function parseOutputLines(array $outputLines): void |
|
121 | { |
||
133 | |||
134 | /** |
||
135 | * Get array representation |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | 1 | public function toArray(): array |
|
143 | |||
144 | /** |
||
145 | * Get the first commit |
||
146 | * |
||
147 | * @return Commit|null |
||
148 | */ |
||
149 | public function first(): ?\GitElephant\Objects\Commit |
||
153 | |||
154 | /** |
||
155 | * Get the last commit |
||
156 | * |
||
157 | * @return Commit|null |
||
158 | */ |
||
159 | 3 | public function last(): ?\GitElephant\Objects\Commit |
|
163 | |||
164 | /** |
||
165 | * Get commit at index |
||
166 | * |
||
167 | * @param int $index the commit index |
||
168 | * |
||
169 | * @return Commit|null |
||
170 | */ |
||
171 | 1 | public function index(int $index): ?\GitElephant\Objects\Commit |
|
175 | |||
176 | /** |
||
177 | * ArrayAccess interface |
||
178 | * |
||
179 | * @param int $offset offset |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function offsetExists($offset): bool |
||
187 | |||
188 | /** |
||
189 | * ArrayAccess interface |
||
190 | * |
||
191 | * @param int $offset offset |
||
192 | * |
||
193 | * @return Commit|null |
||
194 | */ |
||
195 | 14 | public function offsetGet($offset): ?\GitElephant\Objects\Commit |
|
199 | |||
200 | /** |
||
201 | * ArrayAccess interface |
||
202 | * |
||
203 | * @param int $offset offset |
||
204 | * @param mixed $value value |
||
205 | * |
||
206 | * @throws \RuntimeException |
||
207 | */ |
||
208 | public function offsetSet($offset, $value) |
||
212 | |||
213 | /** |
||
214 | * ArrayAccess interface |
||
215 | * |
||
216 | * @param int $offset offset |
||
217 | * |
||
218 | * @throws \RuntimeException |
||
219 | */ |
||
220 | public function offsetUnset($offset) |
||
224 | |||
225 | /** |
||
226 | * Countable interface |
||
227 | * |
||
228 | * @return int |
||
229 | */ |
||
230 | 8 | public function count(): int |
|
234 | |||
235 | /** |
||
236 | * Iterator interface |
||
237 | * |
||
238 | * @return Commit|null |
||
239 | */ |
||
240 | public function current(): ?\GitElephant\Objects\Commit |
||
244 | |||
245 | /** |
||
246 | * Iterator interface |
||
247 | */ |
||
248 | public function next(): void |
||
252 | |||
253 | /** |
||
254 | * Iterator interface |
||
255 | * |
||
256 | * @return int |
||
257 | */ |
||
258 | public function key(): int |
||
262 | |||
263 | /** |
||
264 | * Iterator interface |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function valid(): bool |
||
272 | |||
273 | /** |
||
274 | * Iterator interface |
||
275 | */ |
||
276 | public function rewind(): void |
||
280 | |||
281 | /** |
||
282 | * Repository setter |
||
283 | * |
||
284 | * @param \GitElephant\Repository $repository the repository variable |
||
285 | */ |
||
286 | public function setRepository(Repository $repository): void |
||
290 | |||
291 | /** |
||
292 | * Repository getter |
||
293 | * |
||
294 | * @return \GitElephant\Repository |
||
295 | */ |
||
296 | 19 | public function getRepository(): \GitElephant\Repository |
|
300 | } |
||
301 |