service-captcha/app/Captcha/Config/ImageHead.php

148 lines
3.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace App\Captcha\Config;
use App\Captcha\Exceptions\CaptchaException;
use Illuminate\Support\Arr;
final readonly class ImageHead
{
public function __construct(
private array $backgrounds,
private array $fonts,
private array $fontColors,
private int $width = 150,
private int $height = 40,
private int $textPaddingTop = 5,
private int $textPaddingLeft = 10,
private int $angle = 20,
private int $numberLines = 3,
private array $lineColors = []
) {
if ($this->width <= 0) {
throw new CaptchaException('Incorrect $width settings.');
}
if ($this->height <= 0) {
throw new CaptchaException('Incorrect $height settings.');
}
if ($this->textPaddingTop < 0) {
throw new CaptchaException('Incorrect $textPaddingTop settings.');
}
if ($this->textPaddingLeft < 0) {
throw new CaptchaException('Incorrect $textPaddingLeft settings.');
}
if (count($this->backgrounds) < 1) {
throw new CaptchaException('Invalid $backgrounds parameter.');
}
if (count($this->fonts) < 1) {
throw new CaptchaException('Invalid $fonts parameter.');
}
if (count($this->fontColors) < 1) {
throw new CaptchaException('Invalid $fontColors parameter.');
}
if ($this->angle < 0) {
throw new CaptchaException('Incorrect $angle settings.');
}
if ($this->numberLines < 0) {
throw new CaptchaException('Incorrect $numberLines settings.');
}
}
/**
* @return array
*/
public function getBackgrounds(): array
{
return $this->backgrounds;
}
public function randomBackground(): string
{
return Arr::random($this->getBackgrounds());
}
/**
* @return array
*/
public function getFonts(): array
{
return $this->fonts;
}
public function randomFont(): string
{
return Arr::random($this->getFonts());
}
/**
* @return array
*/
public function getFontColors(): array
{
return $this->fontColors;
}
public function randomFontColor(): string
{
return Arr::random($this->getFontColors());
}
/**
* @return int
*/
public function getWidth(): int
{
return $this->width;
}
/**
* @return int
*/
public function getHeight(): int
{
return $this->height;
}
/**
* @return int
*/
public function getTextPaddingTop(): int
{
return $this->textPaddingTop;
}
/**
* @return int
*/
public function getTextPaddingLeft(): int
{
return $this->textPaddingLeft;
}
/**
* @return int
*/
public function getAngle(): int
{
return $this->angle;
}
public function randomAngle(): int
{
return mt_rand($this->getAngle() * -1, $this->getAngle());
}
public function getNumberLines(): int
{
return $this->numberLines;
}
/**
* @return array
*/
public function getLineColors(): array
{
return $this->lineColors;
}
}