1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of SebastianFeldmann\Git. |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace SebastianFeldmann\Git\Command\Log\Commits; |
13
|
|
|
|
14
|
|
|
use SebastianFeldmann\Cli\Command\OutputFormatter; |
15
|
|
|
use SebastianFeldmann\Git\Log\Commit; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Jsonized |
19
|
|
|
* |
20
|
|
|
* @package SebastianFeldmann\Git |
21
|
|
|
* @author Sebastian Feldmann <[email protected]> |
22
|
|
|
* @link https://github.com/sebastianfeldmann/git |
23
|
|
|
* @since Class available since Release 0.9.0 |
24
|
|
|
*/ |
25
|
|
|
class Jsonized implements OutputFormatter |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Git log format to use. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public const FORMAT = '{"hash": "%h", "names": "%d", "subject": "%s", "date": "%ci", "author": "%an"}'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Format the output. |
36
|
|
|
* |
37
|
|
|
* @param array $output |
38
|
|
|
* @return iterable |
39
|
1 |
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
1 |
|
public function format(array $output): iterable |
42
|
1 |
|
{ |
43
|
1 |
|
$formatted = []; |
44
|
|
|
foreach ($output as $row) { |
45
|
1 |
|
$formatted[] = $this->createCommit($row); |
46
|
|
|
} |
47
|
|
|
return $formatted; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create a log commit object. |
52
|
|
|
* |
53
|
|
|
* @param string $row |
54
|
|
|
* @return \SebastianFeldmann\Git\Log\Commit |
55
|
1 |
|
* @throws \Exception |
56
|
|
|
*/ |
57
|
1 |
|
private function createCommit(string $row): Commit |
58
|
1 |
|
{ |
59
|
1 |
|
$std = json_decode($row); |
60
|
|
|
$date = new \DateTimeImmutable($std->date); |
61
|
1 |
|
$names = array_map('trim', explode(',', str_replace(['(', ')'], '', $std->names))); |
62
|
|
|
|
63
|
|
|
return new Commit($std->hash, $names, $std->subject, '', $date, $std->author); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|