147 lines
4.7 KiB
PHP
147 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Lib;
|
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
use Illuminate\Contracts\Support\Jsonable;
|
|
use Illuminate\Support\Arr;
|
|
|
|
/**
|
|
* Ligne de Coupon stocké dans settings
|
|
*
|
|
* @property integer $web_coupon_identifiant EAN 13 format (Mais ne représente pas un EAN 13),
|
|
* Format utilisé dans MesBr.
|
|
* @property integer $percentage Pourcentage de chance de gagner le lot nombre de 1 à 100.
|
|
* @property integer $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.
|
|
* @package App\Lib\Inputs
|
|
*/
|
|
class Discount extends \stdClass implements Jsonable, Arrayable, \JsonSerializable
|
|
{
|
|
/**
|
|
* Properties list
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
private static array $propertiesList = [
|
|
'web_coupon_identifiant',
|
|
'percentage',
|
|
'face_value',
|
|
'valid_on',
|
|
'picture',
|
|
];
|
|
|
|
/**
|
|
* Get property
|
|
*
|
|
* @param string $name
|
|
* @return mixed
|
|
* @throws \TypeError If prop is undefined.
|
|
*/
|
|
public function __get(string $name)
|
|
{
|
|
if (
|
|
in_array($name, self::$propertiesList)
|
|
) {
|
|
return $this->{$name};
|
|
}
|
|
throw new \TypeError("undefined property {$name}");
|
|
}
|
|
|
|
/**
|
|
* Make and instance
|
|
*
|
|
* @param integer $web_coupon_identifiant EAN 13 format (Mais ne représente pas un EAN 13),
|
|
* Format utilisé dans MesBr.
|
|
* @param integer $percentage Pourcentage de chance de gagner le lot nombre de 1 à 100.
|
|
* @param integer $face_value Valeur faciale en centimes.
|
|
* @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 string $picture Image locale du Coupon.
|
|
*
|
|
* @return \App\Lib\Discount
|
|
* @throws \RuntimeException If $type or $data is wrong.
|
|
*/
|
|
public static function make(
|
|
int $web_coupon_identifiant,
|
|
int $percentage,
|
|
int $face_value,
|
|
string $valid_on,
|
|
string $picture
|
|
): 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
|
|
*
|
|
* @param \App\Lib\Discount $data
|
|
* @return void
|
|
* @throws \RuntimeException If $data is wrong.
|
|
*/
|
|
public static function assertIsInputTypeData(Discount $data)
|
|
{
|
|
if (!\is_int($data->web_coupon_identifiant)) {
|
|
throw new \RuntimeException("InputTypeData 'web_coupon_identifiant' should be an integer");
|
|
}
|
|
if (!\is_int($data->percentage)) {
|
|
throw new \RuntimeException("InputTypeData 'percentage' should be an integer");
|
|
}
|
|
if (!\is_int($data->face_value)) {
|
|
throw new \RuntimeException("InputTypeData 'face_value' should be an integer");
|
|
}
|
|
if (!\is_string($data->valid_on)) {
|
|
throw new \RuntimeException("InputTypeData 'valid_on' should be a string");
|
|
}
|
|
if (!\is_string($data->picture)) {
|
|
throw new \RuntimeException("InputTypeData 'picture' should be a string");
|
|
}
|
|
}
|
|
|
|
// * TRANSFORMERS
|
|
|
|
/**
|
|
* Get the instance as an array.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return collect(Arr::mapWithKeys(self::$propertiesList, fn (string $property) => [
|
|
$property => $this->{$property}
|
|
]))->filter()->all();
|
|
}
|
|
|
|
/**
|
|
* Convert the object to its JSON representation.
|
|
*
|
|
* @param integer $options
|
|
* @return string
|
|
* @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();
|
|
}
|
|
}
|