parent
59e9cf6503
commit
1ac81d7349
@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace IQBall\Core\Data;
|
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
|
|
||||||
class Color implements \JsonSerializable {
|
|
||||||
/**
|
|
||||||
* @var string that represents an hexadecimal color code
|
|
||||||
*/
|
|
||||||
private string $hex;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $value 6 bytes unsigned int that represents an RGB color
|
|
||||||
* @throws InvalidArgumentException if the value is negative or greater than 0xFFFFFF
|
|
||||||
*/
|
|
||||||
|
|
||||||
private function __construct(string $value) {
|
|
||||||
$this->hex = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getValue(): string {
|
|
||||||
return $this->hex;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function from(string $value): Color {
|
|
||||||
$color = self::tryFrom($value);
|
|
||||||
if ($color == null) {
|
|
||||||
|
|
||||||
throw new InvalidArgumentException("The string is not an hexadecimal code");
|
|
||||||
}
|
|
||||||
return $color;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function tryFrom(string $value): ?Color {
|
|
||||||
if (!preg_match('/#(?:[0-9a-fA-F]{6})/', $value)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new Color($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function jsonSerialize() {
|
|
||||||
return get_object_vars($this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in new issue