ldc-gen-game-web/app/Lib/Discount.php
2026-10-20 12:27:49 +02:00

139 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Lib;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Arr;
use JsonSerializable;
use ReturnTypeWillChange;
use RuntimeException;
use stdClass;
use TypeError;
/**
* Ligne de Coupon stocké dans settings
*
* @property int $web_coupon_identifiant EAN 13 format (Mais ne représente pas un EAN 13),
* Format utilisé dans MesBr.
* @property int $percentage Pourcentage de chance de gagner le lot nombre de 1 à 100.
* @property int $face_value Valeur faciale en centimes.
* @property string $valid_on Text valable sur.. (duplicat de ce qui est inscrit dans MesBr)
* Pour affichage sur la page de gain est dans le mail.
* @property string $picture Image locale du Coupon.
*/
class Discount extends stdClass implements Arrayable, Jsonable, JsonSerializable
{
/**
* Properties list
*
* @var array<string>
*/
private static array $propertiesList = [
'picture',
'valid_on',
'face_value',
'percentage',
'web_coupon_identifiant',
];
/**
* Get property
*
*
* @throws TypeError If prop is undefined.
*/
public function __get(string $name): mixed
{
if (
in_array($name, self::$propertiesList)
) {
return $this->{$name};
}
throw new TypeError("undefined property {$name}");
}
/**
* Make and instance
*
* @param string $picture Image locale du Coupon.
* @param string $valid_on Text valable sur.. (duplicat de ce qui est inscrit dans MesBr)
* Pour affichage sur la page de gain est dans le mail.
* @param int $face_value Valeur faciale en centimes.
* @param int $percentage Pourcentage de chance de gagner le lot nombre de 1 à 100.
* @param int $web_coupon_identifiant EAN 13 format (Mais ne représente pas un EAN 13),
* format utilisé dans MesBr.
*
* @throws RuntimeException If $type or $data is wrong.
*/
public static function make(
string $picture,
string $valid_on,
int $face_value,
int $percentage,
int $web_coupon_identifiant,
): self {
$ipTypeData = new self();
$ipTypeData->web_coupon_identifiant = $web_coupon_identifiant;
$ipTypeData->percentage = $percentage;
$ipTypeData->face_value = $face_value;
$ipTypeData->valid_on = $valid_on;
$ipTypeData->picture = $picture;
self::assertIsInputTypeData($ipTypeData);
return $ipTypeData;
}
/**
* Confirm $data is of type InputTypeData
*
*
* @throws RuntimeException If $data is wrong.
*/
public static function assertIsInputTypeData(Discount $data): void
{
throw_unless(\is_int($data->web_coupon_identifiant), RuntimeException::class, "InputTypeData 'web_coupon_identifiant' should be an integer");
throw_unless(\is_int($data->percentage), RuntimeException::class, "InputTypeData 'percentage' should be an integer");
throw_unless(\is_int($data->face_value), RuntimeException::class, "InputTypeData 'face_value' should be an integer");
throw_unless(\is_string($data->valid_on), RuntimeException::class, "InputTypeData 'valid_on' should be a string");
throw_unless(\is_string($data->picture), RuntimeException::class, "InputTypeData 'picture' should be a string");
}
// * TRANSFORMERS
/**
* Get the instance as an array.
*/
public function toArray(): array
{
return collect(Arr::mapWithKeys(self::$propertiesList, fn (string $property): array => [
$property => $this->{$property},
]))->filter()->all();
}
/**
* Convert the object to its JSON representation.
*
* @param int $options
*
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
*/
public function toJson($options = 0): string
{
// phpcs:enable
return \json_encode($this->jsonSerialize(), $options);
}
/**
* Convert the object into something JSON serializable.
*
* @return array
*/
#[ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();
}
}