|
|
@@ -3,7 +3,8 @@
|
|
|
* This file is part of Jdenticon for PHP.
|
|
|
* https://github.com/dmester/jdenticon-php/
|
|
|
*
|
|
|
- * Copyright (c) 2018 Daniel Mester Pirttijärvi
|
|
|
+ * Copyright (c) 2025 Daniel Mester Pirttijärvi
|
|
|
+ * Copyright (c) 2024 Peter Putzer
|
|
|
*
|
|
|
* For full license information, please see the LICENSE file that was
|
|
|
* distributed with this source code.
|
|
|
@@ -18,40 +19,13 @@ use Jdenticon\Color;
|
|
|
*/
|
|
|
class IdenticonStyle
|
|
|
{
|
|
|
- /**
|
|
|
- * @var \Jdenticon\Color
|
|
|
- */
|
|
|
- private $backgroundColor;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var float
|
|
|
- */
|
|
|
- private $padding;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var float
|
|
|
- */
|
|
|
- private $colorSaturation;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var float
|
|
|
- */
|
|
|
- private $grayscaleSaturation;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var array(float)
|
|
|
- */
|
|
|
- private $colorLightness;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var array(float)
|
|
|
- */
|
|
|
- private $grayscaleLightness;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var array(integer)
|
|
|
- */
|
|
|
- private $hues;
|
|
|
+ private Color $backgroundColor;
|
|
|
+ private float $padding;
|
|
|
+ private float $colorSaturation;
|
|
|
+ private float $grayscaleSaturation;
|
|
|
+ private array $colorLightness;
|
|
|
+ private array $grayscaleLightness;
|
|
|
+ private ?array $hues = null;
|
|
|
|
|
|
public function __construct(?array $options = null)
|
|
|
{
|
|
|
@@ -72,9 +46,9 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function getOptions()
|
|
|
+ public function getOptions(): array
|
|
|
{
|
|
|
- $options = array();
|
|
|
+ $options = [];
|
|
|
|
|
|
$options['backgroundColor'] = $this->getBackgroundColor()->__toString();
|
|
|
$options['padding'] = $this->getPadding();
|
|
|
@@ -97,7 +71,7 @@ class IdenticonStyle
|
|
|
* @param array $options Options to set.
|
|
|
* @return self
|
|
|
*/
|
|
|
- public function setOptions(array $options)
|
|
|
+ public function setOptions(array $options): self
|
|
|
{
|
|
|
foreach ($options as $key => $value) {
|
|
|
$this->__set($key, $value);
|
|
|
@@ -105,7 +79,7 @@ class IdenticonStyle
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
- public function __get($name)
|
|
|
+ public function __get(string $name)
|
|
|
{
|
|
|
switch (strtolower($name)) {
|
|
|
case 'backgroundcolor':
|
|
|
@@ -128,7 +102,7 @@ class IdenticonStyle
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function __set($name, $value)
|
|
|
+ public function __set(string $name, $value): void
|
|
|
{
|
|
|
switch (strtolower($name)) {
|
|
|
case 'backgroundcolor':
|
|
|
@@ -161,17 +135,17 @@ class IdenticonStyle
|
|
|
/**
|
|
|
* Normalizes a hue to the first turn [0, 360).
|
|
|
*
|
|
|
- * @param mixed $hue
|
|
|
+ * @param int|float $hue
|
|
|
* @return integer
|
|
|
*/
|
|
|
- private static function normalizeHue($hue)
|
|
|
+ private static function normalizeHue($hue): int
|
|
|
{
|
|
|
if (!is_numeric($hue)) {
|
|
|
throw new \InvalidArgumentException(
|
|
|
"'$hue' is not a valid hue.");
|
|
|
}
|
|
|
|
|
|
- $hue = $hue % 360;
|
|
|
+ $hue = (int)$hue % 360;
|
|
|
if ($hue < 0) {
|
|
|
$hue += 360;
|
|
|
}
|
|
|
@@ -182,9 +156,9 @@ class IdenticonStyle
|
|
|
/**
|
|
|
* Gets an array of allowed hues, or null if there are no restrictions.
|
|
|
*
|
|
|
- * @return array(int)|null
|
|
|
+ * @return array<int>|null
|
|
|
*/
|
|
|
- public function getHues()
|
|
|
+ public function getHues(): ?array
|
|
|
{
|
|
|
return $this->hues;
|
|
|
}
|
|
|
@@ -192,14 +166,14 @@ class IdenticonStyle
|
|
|
/**
|
|
|
* Sets the allowed hues of generated icons.
|
|
|
*
|
|
|
- * @param array(integer)|integer|null $value A hue specified in degrees,
|
|
|
+ * @param array<integer>|integer|null $value A hue specified in degrees,
|
|
|
* or an array of hues specified in degrees. If set to null, the hue
|
|
|
* list is cleared.
|
|
|
* @return self
|
|
|
*/
|
|
|
- public function setHues($value)
|
|
|
+ public function setHues($value): self
|
|
|
{
|
|
|
- $hues = array();
|
|
|
+ $hues = [];
|
|
|
|
|
|
if ($value !== null) {
|
|
|
if (is_array($value)) {
|
|
|
@@ -220,7 +194,7 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return float
|
|
|
*/
|
|
|
- public function getPadding()
|
|
|
+ public function getPadding(): float
|
|
|
{
|
|
|
return $this->padding;
|
|
|
}
|
|
|
@@ -231,9 +205,9 @@ class IdenticonStyle
|
|
|
* @param float $value New padding in the range [0.0, 0.4].
|
|
|
* @return self
|
|
|
*/
|
|
|
- public function setPadding($value)
|
|
|
+ public function setPadding(float $value): self
|
|
|
{
|
|
|
- if (!is_numeric($value) || $value < 0 || $value > 0.4) {
|
|
|
+ if ($value < 0 || $value > 0.4) {
|
|
|
throw new \InvalidArgumentException(
|
|
|
"Padding '$value' out of range. ".
|
|
|
"Values in the range [0.0, 0.4] are allowed.");
|
|
|
@@ -247,7 +221,7 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return \Jdenticon\Color
|
|
|
*/
|
|
|
- public function getBackgroundColor()
|
|
|
+ public function getBackgroundColor(): Color
|
|
|
{
|
|
|
return $this->backgroundColor;
|
|
|
}
|
|
|
@@ -258,7 +232,7 @@ class IdenticonStyle
|
|
|
* @param \Jdenticon\Color|string $value New background color.
|
|
|
* @return \Jdenticon\IdenticonStyle
|
|
|
*/
|
|
|
- public function setBackgroundColor($value)
|
|
|
+ public function setBackgroundColor($value): self
|
|
|
{
|
|
|
if ($value instanceof Color) {
|
|
|
$this->backgroundColor = $value;
|
|
|
@@ -274,7 +248,7 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return float Saturation in the range [0.0, 1.0].
|
|
|
*/
|
|
|
- public function getGrayscaleSaturation()
|
|
|
+ public function getGrayscaleSaturation(): float
|
|
|
{
|
|
|
return $this->grayscaleSaturation;
|
|
|
}
|
|
|
@@ -285,11 +259,9 @@ class IdenticonStyle
|
|
|
* @param $value float Saturation in the range [0.0, 1.0].
|
|
|
* @return self
|
|
|
*/
|
|
|
- public function setGrayscaleSaturation($value)
|
|
|
+ public function setGrayscaleSaturation(float $value): self
|
|
|
{
|
|
|
- if (!is_numeric($value) ||
|
|
|
- $value < 0 || $value > 1
|
|
|
- ) {
|
|
|
+ if ($value < 0 || $value > 1) {
|
|
|
throw new \InvalidArgumentException(
|
|
|
"The grayscale saturation was invalid. ".
|
|
|
"Only values in the range [0.0, 1.0] are allowed.");
|
|
|
@@ -303,7 +275,7 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return float Saturation in the range [0.0, 1.0].
|
|
|
*/
|
|
|
- public function getColorSaturation()
|
|
|
+ public function getColorSaturation(): float
|
|
|
{
|
|
|
return $this->colorSaturation;
|
|
|
}
|
|
|
@@ -314,11 +286,9 @@ class IdenticonStyle
|
|
|
* @param $value float Saturation in the range [0.0, 1.0].
|
|
|
* @return self
|
|
|
*/
|
|
|
- public function setColorSaturation($value)
|
|
|
+ public function setColorSaturation(float $value): self
|
|
|
{
|
|
|
- if (!is_numeric($value) ||
|
|
|
- $value < 0 || $value > 1
|
|
|
- ) {
|
|
|
+ if ($value < 0 || $value > 1) {
|
|
|
throw new \InvalidArgumentException(
|
|
|
"The color saturation was invalid. ".
|
|
|
"Only values in the range [0.0, 1.0] are allowed.");
|
|
|
@@ -330,9 +300,9 @@ class IdenticonStyle
|
|
|
/**
|
|
|
* Gets the value of the ColorLightness property.
|
|
|
*
|
|
|
- * @return array(float, float)
|
|
|
+ * @return array<float>
|
|
|
*/
|
|
|
- public function getColorLightness()
|
|
|
+ public function getColorLightness(): array
|
|
|
{
|
|
|
return $this->colorLightness;
|
|
|
}
|
|
|
@@ -340,13 +310,12 @@ class IdenticonStyle
|
|
|
/**
|
|
|
* Sets the value of the ColorLightness property.
|
|
|
*
|
|
|
- * @param $value array(float, float) Lightness range.
|
|
|
+ * @param array<float> $value Lightness range.
|
|
|
* @return self
|
|
|
*/
|
|
|
- public function setColorLightness($value)
|
|
|
+ public function setColorLightness(array $value): self
|
|
|
{
|
|
|
- if (!is_array($value) ||
|
|
|
- !array_key_exists(0, $value) ||
|
|
|
+ if (!array_key_exists(0, $value) ||
|
|
|
!array_key_exists(1, $value) ||
|
|
|
!is_numeric($value[0]) ||
|
|
|
!is_numeric($value[1]) ||
|
|
|
@@ -358,16 +327,16 @@ class IdenticonStyle
|
|
|
"Please check the documentation.");
|
|
|
}
|
|
|
|
|
|
- $this->colorLightness = array((float)$value[0], (float)$value[1]);
|
|
|
+ $this->colorLightness = [(float)$value[0], (float)$value[1]];
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Gets the value of the GrayscaleLightness property.
|
|
|
*
|
|
|
- * @return array(float, float)
|
|
|
+ * @return array<float>
|
|
|
*/
|
|
|
- public function getGrayscaleLightness()
|
|
|
+ public function getGrayscaleLightness(): array
|
|
|
{
|
|
|
return $this->grayscaleLightness;
|
|
|
}
|
|
|
@@ -375,13 +344,12 @@ class IdenticonStyle
|
|
|
/**
|
|
|
* Sets the value of the GrayscaleLightness property.
|
|
|
*
|
|
|
- * @param $value array(float, float) Lightness range.
|
|
|
+ * @param array<float> $value Lightness range.
|
|
|
* @return self
|
|
|
*/
|
|
|
- public function setGrayscaleLightness($value)
|
|
|
+ public function setGrayscaleLightness(array $value): self
|
|
|
{
|
|
|
- if (!is_array($value) ||
|
|
|
- !array_key_exists(0, $value) ||
|
|
|
+ if (!array_key_exists(0, $value) ||
|
|
|
!array_key_exists(1, $value) ||
|
|
|
!is_numeric($value[0]) ||
|
|
|
!is_numeric($value[1]) ||
|
|
|
@@ -392,7 +360,7 @@ class IdenticonStyle
|
|
|
"The value passed to setGrayscaleLightness was invalid. ".
|
|
|
"Please check the documentation.");
|
|
|
}
|
|
|
- $this->grayscaleLightness = array((float)$value[0], (float)$value[1]);
|
|
|
+ $this->grayscaleLightness = [(float)$value[0], (float)$value[1]];
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
@@ -403,7 +371,7 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return \Jdenticon\Color
|
|
|
*/
|
|
|
- public static function getDefaultBackgroundColor()
|
|
|
+ public static function getDefaultBackgroundColor(): Color
|
|
|
{
|
|
|
return Color::fromRgb(255, 255, 255, 255);
|
|
|
}
|
|
|
@@ -413,7 +381,7 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return float
|
|
|
*/
|
|
|
- public static function getDefaultPadding()
|
|
|
+ public static function getDefaultPadding(): float
|
|
|
{
|
|
|
return 0.08;
|
|
|
}
|
|
|
@@ -423,7 +391,7 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return float
|
|
|
*/
|
|
|
- public static function getDefaultColorSaturation()
|
|
|
+ public static function getDefaultColorSaturation(): float
|
|
|
{
|
|
|
return 0.5;
|
|
|
}
|
|
|
@@ -433,7 +401,7 @@ class IdenticonStyle
|
|
|
*
|
|
|
* @return float
|
|
|
*/
|
|
|
- public static function getDefaultGrayscaleSaturation()
|
|
|
+ public static function getDefaultGrayscaleSaturation(): float
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -441,20 +409,20 @@ class IdenticonStyle
|
|
|
/**
|
|
|
* Gets the default value of the ColorLightness property. Resolves to [0.4, 0.8].
|
|
|
*
|
|
|
- * @return array
|
|
|
+ * @return array<float>
|
|
|
*/
|
|
|
- public static function getDefaultColorLightness()
|
|
|
+ public static function getDefaultColorLightness(): array
|
|
|
{
|
|
|
- return array(0.4, 0.8);
|
|
|
+ return [0.4, 0.8];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Gets the default value of the GrayscaleLightness property. Resolves to [0.3, 0.9].
|
|
|
*
|
|
|
- * @return array
|
|
|
+ * @return array<float>
|
|
|
*/
|
|
|
- public static function getDefaultGrayscaleLightness()
|
|
|
+ public static function getDefaultGrayscaleLightness(): array
|
|
|
{
|
|
|
- return array(0.3, 0.9);
|
|
|
+ return [0.3, 0.9];
|
|
|
}
|
|
|
}
|