|
| 1 | +<?php |
| 2 | + |
| 3 | +class ChangelogParser |
| 4 | +{ |
| 5 | + private string $version; |
| 6 | + private array $globalChanges = []; |
| 7 | + private array $packageChanges = []; |
| 8 | + private array $monorepoNotes = []; |
| 9 | + |
| 10 | + public function parseLatestVersion(string $content): void |
| 11 | + { |
| 12 | + $sections = []; |
| 13 | + $currentSection = null; |
| 14 | + $currentLevel = 0; |
| 15 | + |
| 16 | + foreach (explode("\n", $content) as $line) { |
| 17 | + // Get first version number |
| 18 | + if (str_starts_with($line, '## ') && !$this->version) { |
| 19 | + $this->version = trim(str_replace('## ', '', $line)); |
| 20 | + continue; |
| 21 | + } |
| 22 | + |
| 23 | + // Parse sections |
| 24 | + if (str_starts_with($line, '### ')) { |
| 25 | + $currentSection = trim(str_replace('### ', '', $line)); |
| 26 | + $currentLevel = 3; |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + // Store content based on section |
| 31 | + $trimmedLine = trim($line); |
| 32 | + if ($trimmedLine && $currentSection) { |
| 33 | + if ($currentSection === 'All') { |
| 34 | + $this->globalChanges[] = $trimmedLine; |
| 35 | + } elseif ($currentLevel === 3) { |
| 36 | + $this->packageChanges[$currentSection][] = $trimmedLine; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public function isPackageAffected(string $package): bool |
| 43 | + { |
| 44 | + return isset($this->packageChanges[$package]) || $this->isMajorVersion(); |
| 45 | + } |
| 46 | + |
| 47 | + public function isMajorVersion(): bool |
| 48 | + { |
| 49 | + $parts = explode('.', $this->version); |
| 50 | + return isset($parts[1]) && $parts[1] === '0' && $parts[2] === '0'; |
| 51 | + } |
| 52 | + |
| 53 | + // Getters |
| 54 | + public function getVersion(): string |
| 55 | + { |
| 56 | + return $this->version; |
| 57 | + } |
| 58 | + |
| 59 | + public function getGlobalChanges(): array |
| 60 | + { |
| 61 | + return $this->globalChanges; |
| 62 | + } |
| 63 | + |
| 64 | + public function getPackageChanges(string $package): array |
| 65 | + { |
| 66 | + return $this->packageChanges[$package] ?? []; |
| 67 | + } |
| 68 | +} |
0 commit comments