ytkns/src/GradientPoint.php
2020-06-10 16:03:13 +00:00

65 lines
1.8 KiB
PHP

<?php
namespace YTKNS;
use InvalidArgumentException;
final class GradientPoint {
private int $offset = 0;
private $colour = null;
public function __construct(int $offset = 0, \Colour $colour = null) {
$this->setOffset($offset);
if($colour !== null)
$this->setColour($colour);
}
public function getArray(): array {
return [
'o' => $this->getOffset(),
'c' => $this->getColour()->getRaw(),
];
}
public static function fromArray($array): self {
$point = new static;
if(!empty($array)) {
if(is_string($array))
$array = json_decode($array);
if(is_object($array))
$array = (array)$array;
if(is_array($array)) {
if(isset($array['o']) && is_int($array['o']))
$point->setOffset($array['o']);
if(isset($array['c']))
$point->setColour(Colour::create($array['c']));
}
}
return $point;
}
public function getOffset(): int {
return $this->offset;
}
public function setOffset(int $offset): self {
if($offset < 0 || $offset > 100)
throw new InvalidArgumentException('offset must be between 0 and 100');
$this->offset = $offset;
return $this;
}
public function getColour(): Colour {
if($this->colour === null)
$this->colour = new Colour;
return $this->colour;
}
public function setColour(Colour $colour): self {
$this->colour = $colour;
return $this;
}
public function getCSS(): string {
return sprintf('#%s %d%%', $this->getColour()->getHex(), $this->getOffset());
}
}