ldc-enum/src/BaseEnumRoutable.php

96 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace Kwaadpepper\Enum;
use BadMethodCallException;
use Illuminate\Contracts\Routing\UrlRoutable;
use Kwaadpepper\Enum\Exceptions\EnumNotRoutableException;
use Kwaadpepper\Enum\Exceptions\NotImplementedException;
use TypeError;
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.
*/
abstract class BaseEnumRoutable extends BaseEnum implements UrlRoutable
{
/**
* Get the value of the model's route key.
*
* @return mixed
*/
public function getRouteKey()
{
return $this->value;
}
/**
* Get the route key for the model.
*
2023-05-30 09:27:27 +02:00
* @phpcs:disable Squiz.Commenting.FunctionComment.InvalidNoReturn
*
* @return string
2023-05-30 09:27:27 +02:00
* @throws \Kwaadpepper\Enum\Exceptions\NotImplementedException GetRouteKeyName not implemented,
* should not be used.
*/
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
}
/**
* 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
*/
public function resolveRouteBinding(mixed $value, $field = null)
{
2023-05-30 09:27:27 +02:00
// phpcs:enable
// Try to cast numeric value first.
try {
2023-05-30 09:27:27 +02:00
// Try cast to int.
$iValue = (is_numeric($value) and floatval(intval($value)) === floatval($value)) ?
(int)$value : $value;
return static::make($iValue);
} 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);
} 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;
}
}
}
/**
* 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
* @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
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
*/
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
}
}