diff --git a/src/Database/BindInterface.php b/src/Database/BindInterface.php new file mode 100644 index 0000000..c391ef9 --- /dev/null +++ b/src/Database/BindInterface.php @@ -0,0 +1,17 @@ +binding; - }, - $this->getReader()->getClassAnnotations($class) - ); + return $this->getReader()->getClassAnnotations($class); } -} \ No newline at end of file +} diff --git a/src/Database/Eloquent/Annotations/Annotations/Bind.php b/src/Database/Eloquent/Annotations/Annotations/Bind.php index 3b60da5..c492767 100644 --- a/src/Database/Eloquent/Annotations/Annotations/Bind.php +++ b/src/Database/Eloquent/Annotations/Annotations/Bind.php @@ -2,17 +2,33 @@ namespace Collective\Annotations\Database\Eloquent\Annotations\Annotations; +use Collective\Annotations\Database\BindInterface; + /** * @Annotation */ -class Bind +class Bind implements BindInterface { /** * The binding the annotation binds the model to. * * @var array */ - public $binding; + protected $binding; + + /** + * Whether to use a digits pattern. + * + * @var bool + */ + protected $digits; + + /** + * The pattern to use. + * + * @var string + */ + protected $pattern; /** * Create a new annotation instance. @@ -24,5 +40,25 @@ class Bind public function __construct(array $values = []) { $this->binding = $values['value']; + $this->digits = $values['digits'] ?? false; + $this->pattern = $values['pattern'] ?? null; + } + + /** + * @return string + */ + public function getKey() { + return $this->binding; + } + + /** + * @return string + */ + public function getPattern() { + if ($this->digits) { + return '\d+'; + } + + return $this->pattern; } } diff --git a/src/Database/Eloquent/Attributes/AttributeStrategy.php b/src/Database/Eloquent/Attributes/AttributeStrategy.php index f249d3d..54707e4 100644 --- a/src/Database/Eloquent/Attributes/AttributeStrategy.php +++ b/src/Database/Eloquent/Attributes/AttributeStrategy.php @@ -22,7 +22,7 @@ public function support(ReflectionClass $class): bool public function getBindings(ReflectionClass $class): array { return array_map( - fn (ReflectionAttribute $attribute) => $attribute->newInstance()->binding, + fn (ReflectionAttribute $attribute) => $attribute->newInstance(), $class->getAttributes() ); } diff --git a/src/Database/Eloquent/Attributes/Attributes/Bind.php b/src/Database/Eloquent/Attributes/Attributes/Bind.php index ddebd26..7686d77 100644 --- a/src/Database/Eloquent/Attributes/Attributes/Bind.php +++ b/src/Database/Eloquent/Attributes/Attributes/Bind.php @@ -3,10 +3,32 @@ namespace Collective\Annotations\Database\Eloquent\Attributes\Attributes; use Attribute; +use Collective\Annotations\Database\BindInterface; #[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] -class Bind +class Bind implements BindInterface { - public function __construct(public string $binding) - {} + public function __construct( + protected string $binding, + protected bool $digits = false, + protected ?string $pattern = null, + ) {} + + /** + * @return string + */ + public function getKey() { + return $this->binding; + } + + /** + * @return string + */ + public function getPattern() { + if ($this->digits) { + return '\d+'; + } + + return $this->pattern; + } } diff --git a/src/Database/ScanStrategyInterface.php b/src/Database/ScanStrategyInterface.php index 910706e..bc4aaa0 100644 --- a/src/Database/ScanStrategyInterface.php +++ b/src/Database/ScanStrategyInterface.php @@ -18,7 +18,7 @@ public function support(ReflectionClass $class): bool; * Get information for Bindings of class * * @param ReflectionClass $class - * @return string[] + * @return BindInterface */ public function getBindings(ReflectionClass $class): array; -} \ No newline at end of file +} diff --git a/src/Database/Scanner.php b/src/Database/Scanner.php index cf3a090..be99c90 100644 --- a/src/Database/Scanner.php +++ b/src/Database/Scanner.php @@ -65,13 +65,17 @@ protected function extendsEloquent(ReflectionClass $class): bool /** * Build the event listener for the class and method. * - * @param string $binding + * @param BindInterface $binding * @param string $class * * @return string */ protected function buildBinding($binding, $class): string { - return sprintf('$router->model(\'%s\', \'%s\');', $binding, $class).PHP_EOL; + $code = sprintf('$router->model(\'%s\', \'%s\');', $binding->getKey(), $class).PHP_EOL; + if ($binding->getPattern()) { + $code .= sprintf("\$router->pattern('%s', '%s');", $binding->getKey(), $binding->getPattern()).PHP_EOL; + } + return $code; } }