2021-08-13 12:11:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kwaadpepper\Enum;
|
|
|
|
|
|
|
|
|
|
use BadMethodCallException;
|
|
|
|
|
use Illuminate\Contracts\Routing\UrlRoutable;
|
2021-09-12 01:20:58 +02:00
|
|
|
use Kwaadpepper\Enum\Exceptions\EnumNotRoutableException;
|
2021-08-13 12:11:43 +02:00
|
|
|
use Kwaadpepper\Enum\Exceptions\NotImplementedException;
|
2021-09-12 01:20:58 +02:00
|
|
|
use TypeError;
|
2021-08-13 12:11:43 +02:00
|
|
|
|
2021-09-11 11:11:21 +02:00
|
|
|
/**
|
|
|
|
|
* This Base Enum will be routable with laravel.
|
|
|
|
|
* So you can use it with service container passing an
|
|
|
|
|
* enumeration as any route parameter directly as
|
|
|
|
|
* a controller parameter.
|
|
|
|
|
*/
|
2021-08-13 12:11:43 +02:00
|
|
|
abstract class BaseEnumRoutable extends BaseEnum implements UrlRoutable
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Get the value of the model's route key.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getRouteKey()
|
|
|
|
|
{
|
2021-09-16 09:43:04 +02:00
|
|
|
return $this->value;
|
2021-08-13 12:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the route key for the model.
|
|
|
|
|
*
|
2023-05-30 09:27:27 +02:00
|
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.InvalidNoReturn
|
|
|
|
|
*
|
2021-08-13 12:11:43 +02:00
|
|
|
* @return string
|
2023-05-30 09:27:27 +02:00
|
|
|
* @throws \Kwaadpepper\Enum\Exceptions\NotImplementedException GetRouteKeyName not implemented,
|
|
|
|
|
* should not be used.
|
2021-08-13 12:11:43 +02:00
|
|
|
*/
|
|
|
|
|
public function getRouteKeyName()
|
|
|
|
|
{
|
2023-05-30 09:27:27 +02:00
|
|
|
// phpcs:enable
|
2021-09-12 00:12:49 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2023-05-30 09:27:27 +02:00
|
|
|
throw new NotImplementedException('GetRouteKeyName not implemented, should not be used.');
|
2021-09-12 00:12:49 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-08-13 12:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the model for a bound value.
|
|
|
|
|
*
|
2023-05-30 09:27:27 +02:00
|
|
|
* @param mixed $value
|
|
|
|
|
* @param string|null $field
|
2021-09-11 13:35:17 +02:00
|
|
|
* @return \Kwaadpepper\Enum\BaseEnum|null
|
2023-05-30 09:27:27 +02:00
|
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed
|
2021-08-13 12:11:43 +02:00
|
|
|
*/
|
|
|
|
|
public function resolveRouteBinding($value, $field = null)
|
|
|
|
|
{
|
2023-05-30 09:27:27 +02:00
|
|
|
// phpcs:enable
|
|
|
|
|
// Try to cast numeric value first.
|
2021-08-13 12:11:43 +02:00
|
|
|
try {
|
2023-05-30 09:27:27 +02:00
|
|
|
// Try cast to int.
|
2021-09-11 22:44:54 +02:00
|
|
|
$iValue = (is_numeric($value) and floatval(intval($value)) === floatval($value)) ?
|
|
|
|
|
(int)$value : $value;
|
|
|
|
|
return static::make($iValue);
|
2021-09-12 01:20:58 +02:00
|
|
|
} catch (BadMethodCallException | EnumNotRoutableException | TypeError $e) {
|
2023-05-30 09:27:27 +02:00
|
|
|
// Try string value after that.
|
2021-09-11 11:11:21 +02:00
|
|
|
try {
|
|
|
|
|
return static::make($value);
|
2021-09-12 01:20:58 +02:00
|
|
|
} catch (BadMethodCallException | EnumNotRoutableException | TypeError $e) {
|
2023-05-30 09:27:27 +02:00
|
|
|
// Could not find a suitable value.
|
2021-09-11 11:11:21 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
2021-08-13 12:11:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the child model for a bound value.
|
|
|
|
|
*
|
2023-05-30 09:27:27 +02:00
|
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.InvalidNoReturn
|
|
|
|
|
*
|
|
|
|
|
* @param string $childType
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @param string|null $field
|
2021-08-13 12:11:43 +02:00
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
2023-05-30 09:27:27 +02:00
|
|
|
* @throws \Kwaadpepper\Enum\Exceptions\NotImplementedException ResolveChildRouteBinding not implemented,
|
|
|
|
|
* should not be used.
|
|
|
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
|
2021-08-13 12:11:43 +02:00
|
|
|
*/
|
|
|
|
|
public function resolveChildRouteBinding($childType, $value, $field)
|
|
|
|
|
{
|
2023-05-30 09:27:27 +02:00
|
|
|
// phpcs:enable
|
2021-09-12 00:12:49 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2023-05-30 09:27:27 +02:00
|
|
|
throw new NotImplementedException('ResolveChildRouteBinding not implemented, should not be used.');
|
2021-09-12 00:12:49 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-08-13 12:11:43 +02:00
|
|
|
}
|
|
|
|
|
}
|