diff --git a/src/Commit/Body.php b/src/Commit/Body.php new file mode 100644 index 0000000..7a62d4d --- /dev/null +++ b/src/Commit/Body.php @@ -0,0 +1,23 @@ +content = $content; + } + + public function __toString(): string + { + return $this->content; + } +} diff --git a/src/Commit/Description.php b/src/Commit/Description.php new file mode 100644 index 0000000..3e66800 --- /dev/null +++ b/src/Commit/Description.php @@ -0,0 +1,23 @@ +content = $content; + } + + public function __toString(): string + { + return $this->content; + } +} diff --git a/src/Commit/Footer.php b/src/Commit/Footer.php new file mode 100644 index 0000000..db65d7f --- /dev/null +++ b/src/Commit/Footer.php @@ -0,0 +1,33 @@ +token = $token; + $this->value = $value; + } + + public function __toString(): string + { + return $this->token . ': ' . $this->value; + } +} diff --git a/src/Commit/Parser.php b/src/Commit/Parser.php new file mode 100644 index 0000000..89057f0 --- /dev/null +++ b/src/Commit/Parser.php @@ -0,0 +1,203 @@ +raw = Format::clean($commit); + + if (!$this->isValid()) { + return; + } + + $rows = explode("\n", $commit); + $count = count($rows); + // Commit info + $this->hash = $rows[$count - 1]; + $header = $rows[0]; + $message = ''; + // Get message + foreach ($rows as $i => $row) { + if ($i !== 0 && $i !== $count) { + $message .= $row . "\n"; + } + } + $this->parseHeader($header); + $this->parseMessage($message); + } + + /** + * Parse header. + */ + protected function parseHeader(string $header) + { + preg_match(self::PATTERN_HEADER, $header, $matches); + $this->type = new Type($matches['type']); + $this->scope = new Scope($matches['scope']); + $this->important = !empty($matches['important']) ? true : false; + $this->description = new Description($matches['description']); + } + + /** + * Parse message. + */ + protected function parseMessage(string $message) + { + $body = Format::clean($message); + if (preg_match_all(self::PATTERN_FOOTER, $body, $matches, PREG_SET_ORDER, 0)) { + foreach ($matches as $match) { + $footer = $match[0]; + $body = str_replace($footer, '', $body); + $value = ltrim($match['value'], ':'); + $this->footers[] = new Footer($match['token'], $value); + } + } + $body = Format::clean($body); + $this->body = new Body($body); + } + + public function isValid(): bool + { + return preg_match(self::PATTERN_HEADER, $this->raw); + } + + public function getRaw(): string + { + return $this->raw; + } + + public function getHash(): string + { + return $this->hash; + } + + public function getShortHash(): string + { + return substr($this->hash, 0, 6); + } + + public function getType(): Type + { + return $this->type; + } + + public function getScope(): Scope + { + return $this->scope; + } + + public function isImportant(): bool + { + return $this->important; + } + + public function getDescription(): Description + { + return $this->description; + } + + public function getBody(): Body + { + return $this->body; + } + + /** + * @return Footer[] + */ + public function getFooters(): array + { + return $this->footers; + } + + public function getHeader() + { + $header = $this->type; + if (!empty((string)$this->scope)) { + $header .= '(' . $this->scope . ')'; + } + if ($this->important) { + $header .= '!'; + } + $header .= ': ' . $this->description; + + return $header; + } + + public function getMessage() + { + $footer = implode("\n", $this->footers); + + return $this->body . "\n\n" . $footer; + } + + public function __toString(): string + { + $header = $this->getHeader(); + $message = $this->getMessage(); + $string = $header . "\n\n" . $message; + + return Format::clean($string); + } +} diff --git a/src/Commit/Scope.php b/src/Commit/Scope.php new file mode 100644 index 0000000..74c0d49 --- /dev/null +++ b/src/Commit/Scope.php @@ -0,0 +1,31 @@ +content = $content; + } + + public function __toString(): string + { + $string = $this->content; + $string = preg_replace('/[_]+/m', ' ', $string); + $string = ucfirst($string); + $string = preg_replace('/((?<=\p{Ll})\p{Lu})|((?!\A)\p{Lu}(?>\p{Ll}))/u', ' $0', $string); + $string = preg_replace('/\.(php|md|json|txt|csv|js)($|\s)/', '', $string); + $string = Format::clean($string); + + return $string; + } +} diff --git a/src/Commit/Type.php b/src/Commit/Type.php new file mode 100644 index 0000000..e15f5ed --- /dev/null +++ b/src/Commit/Type.php @@ -0,0 +1,23 @@ +content = strtolower($content); + } + + public function __toString(): string + { + return $this->content; + } +} diff --git a/src/Type/Stringable.php b/src/Type/Stringable.php new file mode 100644 index 0000000..44b78d0 --- /dev/null +++ b/src/Type/Stringable.php @@ -0,0 +1,13 @@ +