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.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getRouteKeyName()
|
|
|
|
|
{
|
2021-09-12 00:12:49 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-09-11 22:44:06 +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.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @param string|null $field
|
2021-09-11 13:35:17 +02:00
|
|
|
* @return \Kwaadpepper\Enum\BaseEnum|null
|
2021-08-13 12:11:43 +02:00
|
|
|
*/
|
2021-09-11 11:11:21 +02:00
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed
|
2021-08-13 12:11:43 +02:00
|
|
|
public function resolveRouteBinding($value, $field = null)
|
|
|
|
|
{
|
|
|
|
|
// try to cast numeric value first
|
|
|
|
|
try {
|
2021-09-11 22:44:54 +02:00
|
|
|
// try cast to int
|
|
|
|
|
$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) {
|
2021-09-11 11:11:21 +02:00
|
|
|
// try string value after that
|
|
|
|
|
try {
|
|
|
|
|
return static::make($value);
|
2021-09-12 01:20:58 +02:00
|
|
|
} catch (BadMethodCallException | EnumNotRoutableException | TypeError $e) {
|
2021-09-11 11:11:21 +02:00
|
|
|
// could not find a suitable value
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-08-13 12:11:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the child model for a bound value.
|
|
|
|
|
*
|
|
|
|
|
* @param string $childType
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @param string|null $field
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
|
|
|
* @throws NotImplementedException
|
|
|
|
|
*/
|
|
|
|
|
public function resolveChildRouteBinding($childType, $value, $field)
|
|
|
|
|
{
|
2021-09-12 00:12:49 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-09-11 22:44:06 +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
|
|
|
}
|
|
|
|
|
}
|