| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | namespace Std\Lib\Git; |
| 39 | |
| 40 | /** |
| 41 | * 注釈付きタグ |
| 42 | */ |
| 43 | final class Tag { |
| 44 | public string $object = ''; |
| 45 | public string $type = 'commit'; |
| 46 | public string $tag = ''; |
| 47 | public ?Person $tagger = null; |
| 48 | public string $message = ''; |
| 49 | public array $headers = []; |
| 50 | public ?string $id = null; |
| 51 | |
| 52 | public static function parse(string $data, ?string $id = null): self { |
| 53 | $t = new self(); |
| 54 | $t->id = $id; |
| 55 | $t->headers = self::parseHeaders($data, $t->message); |
| 56 | foreach ($t->headers as [$k, $v]) { |
| 57 | match ($k) { |
| 58 | 'object' => $t->object = $v, |
| 59 | 'type' => $t->type = $v, |
| 60 | 'tag' => $t->tag = $v, |
| 61 | 'tagger' => $t->tagger = Person::parse($v), |
| 62 | default => null, |
| 63 | }; |
| 64 | } |
| 65 | return $t; |
| 66 | } |
| 67 | |
| 68 | public static function create(string $object, string $type, string $name, ?Person $tagger, string $message): self { |
| 69 | $t = new self(); |
| 70 | $t->object = $object; |
| 71 | $t->type = $type; |
| 72 | $t->tag = $name; |
| 73 | $t->tagger = $tagger; |
| 74 | $t->message = $message; |
| 75 | $t->headers = [['object', $object], ['type', $type], ['tag', $name]]; |
| 76 | if ($tagger !== null) $t->headers[] = ['tagger', (string)$tagger]; |
| 77 | return $t; |
| 78 | } |
| 79 | |
| 80 | public function toString(): string { |
| 81 | return self::buildHeaders($this->headers).$this->message; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * ヘッダーとメッセージ(コミットとタグで同じ形) |
| 86 | * |
| 87 | * @return array [名前, 値]の配列 |
| 88 | */ |
| 89 | public static function parseHeaders(string $data, ?string &$message): array { |
| 90 | $end = strpos($data, "\n\n"); |
| 91 | $head = $end === false ? rtrim($data, "\n") : substr($data, 0, $end); |
| 92 | $message = $end === false ? '' : substr($data, $end + 2); |
| 93 | $headers = []; |
| 94 | foreach ($head === '' ? [] : explode("\n", $head) as $line) { |
| 95 | if ($line !== '' && $line[0] === ' ' && !empty($headers)) { |
| 96 | // 前のヘッダーの続き(gpgsig等) |
| 97 | $headers[count($headers) - 1][1] .= "\n".substr($line, 1); |
| 98 | continue; |
| 99 | } |
| 100 | $sp = strpos($line, ' '); |
| 101 | $headers[] = $sp === false ? [$line, ''] : [substr($line, 0, $sp), substr($line, $sp + 1)]; |
| 102 | } |
| 103 | return $headers; |
| 104 | } |
| 105 | |
| 106 | public static function buildHeaders(array $headers): string { |
| 107 | $out = ''; |
| 108 | foreach ($headers as [$k, $v]) $out .= $k.' '.str_replace("\n", "\n ", $v)."\n"; |
| 109 | return $out."\n"; |
| 110 | } |
| 111 | } |