workaround PHP 8.4 deprecations

lifted from https://github.com/dmester/jdenticon-php/pull/10
This commit is contained in:
El RIDO 2025-07-05 20:09:43 +02:00
parent 7b444bf62b
commit 811b62b538
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
2 changed files with 121 additions and 121 deletions

View file

@ -2,10 +2,10 @@
/**
* This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/
*
*
* Copyright (c) 2018 Daniel Mester Pirttijärvi
*
* For full license information, please see the LICENSE file that was
*
* For full license information, please see the LICENSE file that was
* distributed with this source code.
*/
@ -28,29 +28,29 @@ class Identicon
* @var mixed
*/
private $value;
/**
* @var boolean
*/
private $valueSet = false;
/**
* Defaults to hash of an empty string.
*
*
* @var string
*/
private $hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
/**
* @var integer
*/
private $size = 100;
/**
* @var Jdenticon\Rendering\IconGenerator
*/
private $iconGenerator;
/**
* @var Jdenticon\IdenticonStyle
*/
@ -64,15 +64,15 @@ class Identicon
/**
* Creates an Identicon instance with the specified hash.
*
* @param string $hash A binary string containing the hash that will be used
* @param string $hash A binary string containing the hash that will be used
* as base for this icon. The hash must contain at least 6 bytes.
* @param int|float|double $size The size of the icon in pixels (the icon
* @param int|float|double $size The size of the icon in pixels (the icon
* is quadratic).
*/
public function __construct($options = null)
{
$this->iconGenerator = IconGenerator::getDefaultGenerator();
if ($options !== null) {
$this->setOptions($options);
}
@ -81,11 +81,11 @@ class Identicon
$this->style = new IdenticonStyle();
}
}
/**
* Creates an Identicon instance from a specified hash.
*
* @param string $hash A binary string containing the hash that will be used
* @param string $hash A binary string containing the hash that will be used
* as base for this icon. The hash must contain at least 6 bytes.
* @param int $size The size of the icon in pixels (the icon is quadratic).
* @return \Jdenticon\Identicon
@ -98,8 +98,8 @@ class Identicon
/**
* Creates an Identicon instance from a specified value.
*
* @param mixed $value The value that will be used as base for this icon.
* The value will be converted to a UTF8 encoded string and then hashed
* @param mixed $value The value that will be used as base for this icon.
* The value will be converted to a UTF8 encoded string and then hashed
* using SHA1.
* @param int $size The size of the icon in pixels (the icon is quadratic).
* @return \Jdenticon\Identicon
@ -108,38 +108,38 @@ class Identicon
{
return new Identicon(array('value' => $value, 'size' => $size));
}
/**
* Gets an associative array of all options of this identicon.
*
* @return array
*/
public function getOptions()
public function getOptions()
{
$options = array();
if ($this->valueSet) {
$options['value'] = $this->getValue();
} elseif ($this->hash !== null) {
$options['hash'] = $this->getHash();
}
$options['size'] = $this->getSize();
$options['style'] = $this->getStyle()->getOptions();
if ($this->enableImageMagick !== null) {
$options['enableImageMagick'] = $this->getEnableImageMagick();
}
if ($this->iconGenerator !== IconGenerator::getDefaultGenerator()) {
$options['iconGenerator'] = $this->getIconGenerator();
}
return $options;
}
/**
* Sets options in this identicon by specifying an associative array of
* Sets options in this identicon by specifying an associative array of
* option values.
*
* @param array $options Options to set.
@ -152,7 +152,7 @@ class Identicon
}
return $this;
}
public function __get($name)
{
switch (strtolower($name)) {
@ -173,7 +173,7 @@ class Identicon
"Unknown Identicon option '$name'.");
}
}
public function __set($name, $value)
{
switch (strtolower($name)) {
@ -200,15 +200,15 @@ class Identicon
"Unknown Identicon option '$name'.");
}
}
/**
/**
* Gets the size of the icon in pixels.
*/
public function getSize()
{
return $this->size;
}
/**
* Sets the size of this icon in pixels.
*
@ -221,25 +221,25 @@ class Identicon
"An invalid identicon size was specified. ".
"A numeric value >= 1 was expected. Specified value: $size.");
}
$this->size = (int)$size;
}
/**
/**
* Gets the size of the icon in pixels.
*/
public function getEnableImageMagick()
{
// Enable ImageMagick on PHP < 7. On PHP 7 the performance increase
// is not as obvious as on PHP 5. Since the ImageMagick renderer has a
// is not as obvious as on PHP 5. Since the ImageMagick renderer has a
// lot of quirks, we don't want to use it unless really needed.
if ($this->enableImageMagick === null) {
return PHP_MAJOR_VERSION < 7 && extension_loaded('imagick');
}
return $this->enableImageMagick;
}
/**
* Sets whether ImageMagick should be used to generate PNG icons.
*
@ -258,10 +258,10 @@ class Identicon
'Failed to enable ImageMagick. '.
'The Imagick PHP extension was not found on this system.');
}
$this->enableImageMagick = $enable;
}
/**
* Gets the {@see IconGenerator} used to generate icons.
*
@ -271,11 +271,11 @@ class Identicon
{
return $this->iconGenerator;
}
/**
* Sets the {@see IconGenerator} used to generate icons.
*
* @param \Jdenticon\Rendering\IconGenerator $iconGenerator Icon generator
* @param \Jdenticon\Rendering\IconGenerator $iconGenerator Icon generator
* that will render the shapes of the identicon.
* @return \Jdenticon\Identicon
*/
@ -287,7 +287,7 @@ class Identicon
$this->iconGenerator = $iconGenerator;
return $this;
}
/**
* Gets or sets the style of the icon.
*
@ -297,11 +297,11 @@ class Identicon
{
return $this->style;
}
/**
* Gets or sets the style of the icon.
*
* @param array|\Jdenticon\IdenticonStyle $style The new style of the icon.
* @param array|\Jdenticon\IdenticonStyle $style The new style of the icon.
* NULL will revert the identicon to use the default style.
* @return self
*/
@ -319,21 +319,21 @@ class Identicon
"Allowed values are IdenticonStyle instances and associative ".
"arrays containing IdenticonStyle options.");
}
return $this;
}
/**
* Gets a binary string containing the hash that is used as base for this
* Gets a binary string containing the hash that is used as base for this
* icon.
*/
public function getHash()
{
return $this->hash;
}
/**
* Sets a binary string containing the hash that is used as base for this
* Sets a binary string containing the hash that is used as base for this
* icon. The string should contain at least 6 bytes.
*
* @param string $hash Binary string containing the hash.
@ -347,25 +347,25 @@ class Identicon
}
if (strlen($hash) < 6) {
throw new \InvalidArgumentException(
'An invalid $hash was passed to Identicon. ' .
'An invalid $hash was passed to Identicon. ' .
'The hash was expected to contain at least 6 bytes.');
}
$this->hash = $hash;
$this->value = null;
$this->valueSet = false;
return $this;
}
/**
* Gets a binary string containing the hash that is used as base for this
* Gets a binary string containing the hash that is used as base for this
* icon.
*/
public function getValue()
{
return $this->value;
}
/**
* Sets a value that will be used as base for this icon. The value will
* be converted to a string and then hashed using SHA1.
@ -379,24 +379,24 @@ class Identicon
$this->valueSet = true;
return $this;
}
/**
* Gets the bounds of the icon excluding its padding.
*
* @return \Jdenticon\Rendering\Rectangle
*/
public function getIconBounds()
public function getIconBounds()
{
// Round padding to nearest integer
$padding = (int)($this->style->getPadding() * $this->size + 0.5);
return new Rectangle(
$padding, $padding,
$this->size - $padding * 2,
$this->size - $padding * 2);
}
private function getRenderer($imageFormat)
private function getRenderer($imageFormat)
{
switch (strtolower($imageFormat)) {
case 'svg':
@ -408,25 +408,25 @@ class Identicon
new InternalPngRenderer($this->size, $this->size);
}
}
/**
* Draws this icon using a specified renderer.
*
* This method is only intended for usage with custom renderers. A custom
* renderer could as an example render an Identicon in a file format not
* natively supported by Jdenticon. To implement a new file format,
* This method is only intended for usage with custom renderers. A custom
* renderer could as an example render an Identicon in a file format not
* natively supported by Jdenticon. To implement a new file format,
* implement {@see \Jdenticon\Rendering\RendererInterface}.
*
* @param \Jdenticon\Rendering\RendererInterface $renderer The renderer used
* @param \Jdenticon\Rendering\RendererInterface $renderer The renderer used
* to render this icon.
* @param \Jdenticon\Rendering\Rectangle $rect The bounds of the rendered
* @param \Jdenticon\Rendering\Rectangle $rect The bounds of the rendered
* icon. No padding will be applied to the rectangle. If the parameter
* is omitted, the rectangle is calculated from the current icon
* size and padding.
*/
public function draw(
\Jdenticon\Rendering\RendererInterface $renderer,
\Jdenticon\Rendering\Rectangle $rect = null)
\Jdenticon\Rendering\RendererInterface $renderer,
?\Jdenticon\Rendering\Rectangle $rect = null)
{
if ($rect === null) {
$rect = $this->getIconBounds();
@ -438,11 +438,11 @@ class Identicon
/**
* Renders the icon directly to the page output.
*
* The method will set the 'Content-Type' HTTP header. You are recommended
* to set an appropriate 'Cache-Control' header before calling this method
* The method will set the 'Content-Type' HTTP header. You are recommended
* to set an appropriate 'Cache-Control' header before calling this method
* to ensure the icon is cached client side.
*
* @param string $imageFormat The image format of the output.
* @param string $imageFormat The image format of the output.
* Supported values are 'png' and 'svg'.
*/
public function displayImage($imageFormat = 'png')
@ -454,11 +454,11 @@ class Identicon
header("Content-Type: $mimeType");
echo $data;
}
/**
* Renders the icon to a binary string.
*
* @param string $imageFormat The image format of the output string.
* @param string $imageFormat The image format of the output string.
* Supported values are 'png' and 'svg'.
* @return string
*/
@ -468,14 +468,14 @@ class Identicon
$this->draw($renderer, $this->getIconBounds());
return $renderer->getData();
}
/**
* Renders the icon as a data URI. It is recommended to avoid using this
* method unless really necessary, since it will effectively disable client
* caching of generated icons, and will also cause the same icon to be
* Renders the icon as a data URI. It is recommended to avoid using this
* method unless really necessary, since it will effectively disable client
* caching of generated icons, and will also cause the same icon to be
* rendered multiple times, when used multiple times on a single page.
*
* @param string $imageFormat The image format of the data URI.
* @param string $imageFormat The image format of the data URI.
* Supported values are 'png' and 'svg'.
* @return string
*/

View file

@ -2,10 +2,10 @@
/**
* This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/
*
*
* Copyright (c) 2018 Daniel Mester Pirttijärvi
*
* For full license information, please see the LICENSE file that was
*
* For full license information, please see the LICENSE file that was
* distributed with this source code.
*/
@ -18,42 +18,42 @@ 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;
public function __construct(array $options = null)
public function __construct(?array $options = null)
{
$this->backgroundColor = self::getDefaultBackgroundColor();
$this->padding = self::getDefaultPadding();
@ -61,12 +61,12 @@ class IdenticonStyle
$this->grayscaleSaturation = self::getDefaultGrayscaleSaturation();
$this->colorLightness = self::getDefaultColorLightness();
$this->grayscaleLightness = self::getDefaultGrayscaleLightness();
if ($options !== null) {
$this->setOptions($options);
}
}
/**
* Gets an associative array of all options of this style.
*
@ -89,7 +89,7 @@ class IdenticonStyle
return $options;
}
/**
* Sets options in this style by specifying an associative array of option
* values.
@ -104,7 +104,7 @@ class IdenticonStyle
}
return $this;
}
public function __get($name)
{
switch (strtolower($name)) {
@ -127,7 +127,7 @@ class IdenticonStyle
"Unknown IdenticonStyle option '$name'.");
}
}
public function __set($name, $value)
{
switch (strtolower($name)) {
@ -157,14 +157,14 @@ class IdenticonStyle
"Unknown IdenticonStyle option '$name'.");
}
}
/**
* Normalizes a hue to the first turn [0, 360).
*
*
* @param mixed $hue
* @return integer
*/
private static function normalizeHue($hue)
private static function normalizeHue($hue)
{
if (!is_numeric($hue)) {
throw new \InvalidArgumentException(
@ -178,7 +178,7 @@ class IdenticonStyle
return $hue;
}
/**
* Gets an array of allowed hues, or null if there are no restrictions.
*
@ -214,7 +214,7 @@ class IdenticonStyle
$this->hues = empty($hues) ? null : $hues;
return $this;
}
/**
* Gets the padding of an icon in percents in the range [0.0, 0.4].
*
@ -224,7 +224,7 @@ class IdenticonStyle
{
return $this->padding;
}
/**
* Sets the padding of an icon in percents.
*
@ -241,7 +241,7 @@ class IdenticonStyle
$this->padding = (float)$value;
return $this;
}
/**
* Gets the color of the identicon background.
*
@ -251,7 +251,7 @@ class IdenticonStyle
{
return $this->backgroundColor;
}
/**
* Sets the color of the identicon background.
*
@ -265,10 +265,10 @@ class IdenticonStyle
} else {
$this->backgroundColor = Color::parse($value);
}
return $this;
}
/**
* Gets the saturation of the originally grayscale identicon shapes.
*
@ -278,7 +278,7 @@ class IdenticonStyle
{
return $this->grayscaleSaturation;
}
/**
* Sets the saturation of the originally grayscale identicon shapes.
*
@ -297,7 +297,7 @@ class IdenticonStyle
$this->grayscaleSaturation = (float)$value;
return $this;
}
/**
* Gets the saturation of the colored identicon shapes.
*
@ -307,7 +307,7 @@ class IdenticonStyle
{
return $this->colorSaturation;
}
/**
* Sets the saturation of the colored identicon shapes.
*
@ -326,7 +326,7 @@ class IdenticonStyle
$this->colorSaturation = (float)$value;
return $this;
}
/**
* Gets the value of the ColorLightness property.
*
@ -336,7 +336,7 @@ class IdenticonStyle
{
return $this->colorLightness;
}
/**
* Sets the value of the ColorLightness property.
*
@ -357,11 +357,11 @@ class IdenticonStyle
"The value passed to setColorLightness was invalid. ".
"Please check the documentation.");
}
$this->colorLightness = array((float)$value[0], (float)$value[1]);
return $this;
}
/**
* Gets the value of the GrayscaleLightness property.
*
@ -371,7 +371,7 @@ class IdenticonStyle
{
return $this->grayscaleLightness;
}
/**
* Sets the value of the GrayscaleLightness property.
*
@ -395,9 +395,9 @@ class IdenticonStyle
$this->grayscaleLightness = array((float)$value[0], (float)$value[1]);
return $this;
}
/**
* Gets the default value of the BackgroundColor property. Resolves to transparent.
*
@ -407,7 +407,7 @@ class IdenticonStyle
{
return Color::fromRgb(255, 255, 255, 255);
}
/**
* Gets the default value of the Padding property. Resolves to 0.08.
*
@ -417,7 +417,7 @@ class IdenticonStyle
{
return 0.08;
}
/**
* Gets the default value of the ColorSaturation property. Resolves to 0.5.
*
@ -427,7 +427,7 @@ class IdenticonStyle
{
return 0.5;
}
/**
* Gets the default value of the GrayscaleSaturation property. Resolves to 0.
*
@ -437,7 +437,7 @@ class IdenticonStyle
{
return 0;
}
/**
* Gets the default value of the ColorLightness property. Resolves to [0.4, 0.8].
*
@ -447,7 +447,7 @@ class IdenticonStyle
{
return array(0.4, 0.8);
}
/**
* Gets the default value of the GrayscaleLightness property. Resolves to [0.3, 0.9].
*