-
-
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.
Improve action column (extract url creating, move default values to r…
…enderer) (#142)
- Loading branch information
Showing
10 changed files
with
204 additions
and
141 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 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
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(self::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); | ||
} | ||
} |
Oops, something went wrong.