Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git
<?php
require_once __DIR__ . '/../render/CommitRenderer.php';
require_once __DIR__ . '/UrlBuilder.php';

class Commit {
  private string $sha;
  private string $message;
  private string $author;
  private string $email;
  private int    $date;
  private string $parentSha;

  public function __construct( string $sha, string $rawData ) {
    $this->sha = $sha;

    $this->author = \preg_match( '/^author (.*?) </m', $rawData, $m )
      ? \trim( $m[1] )
      : 'Unknown';

    $this->email = \preg_match( '/^author .*? <(.*?)>/m', $rawData, $m )
      ? \trim( $m[1] )
      : '';

    $this->date = \preg_match( '/^author .*? <.*?> (\d+)/m', $rawData, $m )
      ? (int)$m[1]
      : 0;

    $this->parentSha = \preg_match( '/^parent (.*)$/m', $rawData, $m )
      ? \trim( $m[1] )
      : '';

    $pos = \strpos( $rawData, "\n\n" );

    $this->message = $pos !== false
      ? \trim( \substr( $rawData, $pos + 2 ) )
      : '';
  }

  public function provideParent( callable $callback ): void {
    if( $this->parentSha !== '' ) {
      $callback( $this->parentSha );
    }
  }

  public function hasParent(): bool {
    return $this->parentSha !== '';
  }

  public function isSha( string $sha ): bool {
    return $this->sha === $sha;
  }

  public function toUrl( UrlBuilder $builder ): string {
    return $builder->withHash( $this->sha )->build();
  }

  public function isEmpty(): bool {
    return $this->sha === '';
  }

  public function render( CommitRenderer $renderer ): void {
    $renderer->render(
      $this->sha,
      $this->message,
      $this->author,
      $this->date
    );
  }

  public function renderTime( CommitRenderer $renderer ): void {
    $renderer->renderTime( $this->date );
  }
}