-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
95 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\DataView\YiiRouter; | ||
|
||
use LogicException; | ||
use Yiisoft\Router\CurrentRoute; | ||
use Yiisoft\Router\UrlGeneratorInterface; | ||
use Yiisoft\Yii\DataView\Column\ActionColumn; | ||
use Yiisoft\Yii\DataView\Column\Base\DataContext; | ||
|
||
use function is_object; | ||
|
||
final class ActionColumnUrlCreator | ||
{ | ||
public function __construct( | ||
private readonly UrlGeneratorInterface $urlGenerator, | ||
private readonly CurrentRoute $currentRoute, | ||
private readonly string $defaultPrimaryKey = 'id', | ||
private readonly bool $defaultPrimaryKeyPlace = UrlConfig::ARGUMENTS, | ||
) { | ||
} | ||
|
||
public function __invoke(string $action, DataContext $context): string | ||
{ | ||
/** @var ActionColumn $column */ | ||
$column = $context->column; | ||
|
||
$config = $column->urlConfig ?? new UrlConfig(); | ||
if (!$config instanceof UrlConfig) { | ||
throw new LogicException(ActionColumnUrlCreator::class . ' supports ' . UrlConfig::class . ' only.'); | ||
} | ||
|
||
$primaryKey = $column->primaryKey ?? $this->defaultPrimaryKey; | ||
$primaryKeyPlace = $config->primaryKeyPlace ?? $this->defaultPrimaryKeyPlace; | ||
|
||
$primaryKeyValue = is_object($context->data) | ||
? $context->data->$primaryKey | ||
: $context->data[$primaryKey]; | ||
|
||
/** @psalm-suppress PossiblyNullOperand We guess that current route is matched. */ | ||
$route = ($config->baseRouteName ?? $this->currentRoute->getName()) . '/' . $action; | ||
|
||
$arguments = $config->arguments; | ||
$queryParameters = $config->queryParameters; | ||
if ($primaryKeyPlace === UrlConfig::ARGUMENTS) { | ||
$arguments = array_merge($arguments, [$primaryKey => (string)$primaryKeyValue]); | ||
} else { | ||
$queryParameters = array_merge($queryParameters, [$primaryKey => (string)$primaryKeyValue]); | ||
} | ||
|
||
return $this->urlGenerator->generate($route, $arguments, $queryParameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\DataView\YiiRouter; | ||
|
||
use Stringable; | ||
|
||
final class UrlConfig | ||
{ | ||
public const ARGUMENTS = true; | ||
public const QUERY_PARAMETERS = false; | ||
|
||
/** | ||
* @psalm-param array<string,scalar|Stringable|null> $arguments | ||
*/ | ||
public function __construct( | ||
public readonly ?string $baseRouteName = null, | ||
public readonly array $arguments = [], | ||
public readonly array $queryParameters = [], | ||
public readonly ?bool $primaryKeyPlace = null, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters