<?php
class Hyperlink
{
  /*
  * Private members
  */

  private string $uri;
  private string $title;
  private ?string $colour;

  /*
  * Constructor
  */

  public function __construct(string $uri, string $title, string $colour = null)
  {
    $this->uri = $uri;
    $this->title = $title;
    $this->setColour($colour);
  }

  /*
  * Getters and Setters
  */

  public function setColour(string $colour = null)
  {
    if ($colour === null) {
      $this->colour = null;
      return;
    }

    if (!preg_match("/^#([0-9A-Fa-f]{3,4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/", $colour)) {
      trigger_error("Colour invalid", E_USER_WARNING);
      return;
    }

    $this->colour = $colour;
  }

  public function getColour()
  {
    return $this->colour ?? "No colour specified";
  }

  /*
  * Meta
  */

  public function __toString()
  {
    $style = $this->colour ? "style=\"color:$this->colour;\"" : "";
    return <<<END
      <a href="$this->uri" $style>$this->title</a>
      END;
  }
}
