|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of Gitonomy. |
| 5 | + * |
| 6 | + * (c) Alexandre Salomé <alexandre.salome@gmail.com> |
| 7 | + * (c) Julien DIDIER <genzo.wm@gmail.com> |
| 8 | + * |
| 9 | + * This source file is subject to the GPL license that is bundled |
| 10 | + * with this source code in the file LICENSE. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Gitonomy\ChangeLog\Parser; |
| 14 | + |
| 15 | +use Gitonomy\ChangeLog\ChangeLog; |
| 16 | +use Gitonomy\ChangeLog\Node\Version; |
| 17 | +use Gitonomy\ChangeLog\Node\Feature; |
| 18 | + |
| 19 | +class ChangeLogParser extends Parser |
| 20 | +{ |
| 21 | + public function parse($content) |
| 22 | + { |
| 23 | + $this->cursor = 0; |
| 24 | + $this->content = $content; |
| 25 | + $this->length = strlen($this->content); |
| 26 | + |
| 27 | + $changeLog = new ChangeLog(); |
| 28 | + |
| 29 | + while (!$this->isFinished()) { |
| 30 | + |
| 31 | + if ($this->expects('* ')) { |
| 32 | + $version = $this->parseVersion($changeLog); |
| 33 | + } |
| 34 | + |
| 35 | + if (null !== $version && $this->expects(' * ')) { |
| 36 | + $this->parseFeature($version); |
| 37 | + } |
| 38 | + |
| 39 | + $this->consumeNewLine(); |
| 40 | + } |
| 41 | + |
| 42 | + return $changeLog; |
| 43 | + } |
| 44 | + |
| 45 | + protected function parseVersion(ChangeLog $changeLog) |
| 46 | + { |
| 47 | + $version = $this->consumeTo("\n"); |
| 48 | + |
| 49 | + preg_match('/^v([0-9\.]+)(?: \((\d{4}-\d{2}-\d{2})\))?$/', $version, $versionData); |
| 50 | + |
| 51 | + if (!$versionData) { |
| 52 | + $version = new Version($version); |
| 53 | + } |
| 54 | + elseif (isset($versionData[2])) { |
| 55 | + $version = new Version($versionData[1], $versionData[2]); |
| 56 | + } else { |
| 57 | + $version = new Version($versionData[1]); |
| 58 | + } |
| 59 | + |
| 60 | + $changeLog->addVersion($version); |
| 61 | + |
| 62 | + return $version; |
| 63 | + } |
| 64 | + |
| 65 | + protected function parseFeature(Version $version) |
| 66 | + { |
| 67 | + $feature = $this->consumeTo("\n"); |
| 68 | + |
| 69 | + preg_match('#(\w*) (.*)#', $feature, $featureData); |
| 70 | + |
| 71 | + if (!$featureData) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $version->addFeature(new Feature($featureData[1], $featureData[2])); |
| 76 | + |
| 77 | + return $feature; |
| 78 | + } |
| 79 | +} |
0 commit comments