Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model binding pattern #123

Open
wants to merge 1 commit into
base: 8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Database/BindInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Collective\Annotations\Database;

interface BindInterface
{
/**
* @return string
*/
public function getKey();

/**
* @return string
*/
public function getPattern();

}
9 changes: 2 additions & 7 deletions src/Database/Eloquent/Annotations/AnnotationStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ public function support(ReflectionClass $class): bool
*/
public function getBindings(ReflectionClass $class): array
{
return array_map(
function (Bind $annotation) {
return $annotation->binding;
},
$this->getReader()->getClassAnnotations($class)
);
return $this->getReader()->getClassAnnotations($class);
}

}
}
40 changes: 38 additions & 2 deletions src/Database/Eloquent/Annotations/Annotations/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Database/Eloquent/Attributes/AttributeStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
28 changes: 25 additions & 3 deletions src/Database/Eloquent/Attributes/Attributes/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/Database/ScanStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 6 additions & 2 deletions src/Database/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}