Skip to content

Commit

Permalink
improve url creating
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 30, 2023
1 parent 8206767 commit 1dda340
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 115 deletions.
2 changes: 1 addition & 1 deletion config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Translator\Message\Php\MessageSource;
use Yiisoft\Translator\SimpleMessageFormatter;
use Yiisoft\Yii\DataView\Column\ActionColumnRenderer;
use Yiisoft\Yii\DataView\Column\ActionColumnUrlCreator;
use Yiisoft\Yii\DataView\YiiRouter\ActionColumnUrlCreator;

/** @var array $params */

Expand Down
5 changes: 1 addition & 4 deletions src/Column/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ final class ActionColumn implements ColumnInterface
public function __construct(
public readonly ?string $primaryKey = null,
public readonly ?string $template = null,
public readonly ?string $routeName = null,
public readonly array $urlParamsConfig = [],
public readonly ?array $urlArguments = null,
public readonly array $urlQueryParameters = [],
public readonly mixed $urlConfig = null,
?callable $urlCreator = null,
public readonly ?string $header = null,
public readonly ?string $footer = null,
Expand Down
59 changes: 0 additions & 59 deletions src/Column/ActionColumnUrlCreator.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Column/Base/DataContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class DataContext
public function __construct(
public readonly ColumnInterface $column,
public readonly array|object $data,
public readonly mixed $key,
public readonly int|string $key,
public readonly int $index,
) {
}
Expand Down
5 changes: 1 addition & 4 deletions src/Column/CheckboxColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con
}

if (!array_key_exists('value', $inputAttributes)) {
$key = $context->key;
$value = is_array($key)
? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
: (string)$key;
$value = $context->key;
}

$input = Html::checkbox($name, $value, $inputAttributes);
Expand Down
5 changes: 1 addition & 4 deletions src/Column/RadioColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con
}

if (!array_key_exists('value', $inputAttributes)) {
$key = $context->key;
$value = is_array($key)
? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
: (string)$key;
$value = $context->key;
}

$input = Html::radio($name, $value, $inputAttributes);
Expand Down
55 changes: 55 additions & 0 deletions src/YiiRouter/ActionColumnUrlCreator.php
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.');

Check warning on line 32 in src/YiiRouter/ActionColumnUrlCreator.php

View check run for this annotation

Codecov / codecov/patch

src/YiiRouter/ActionColumnUrlCreator.php#L32

Added line #L32 was not covered by tests
}

$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);
}
}
24 changes: 24 additions & 0 deletions src/YiiRouter/UrlConfig.php
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,
) {
}
}
51 changes: 10 additions & 41 deletions tests/Column/ActionColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Yiisoft\Yii\DataView\GridView;
use Yiisoft\Yii\DataView\Tests\Support\Assert;
use Yiisoft\Yii\DataView\Tests\Support\TestTrait;
use Yiisoft\Yii\DataView\YiiRouter\UrlConfig;

final class ActionColumnTest extends TestCase
{
Expand Down Expand Up @@ -537,7 +538,7 @@ public function testUrlArguments(): void
</div>
HTML,
GridView::widget()
->columns(new ActionColumn(urlArguments: ['test-arguments' => 'test.arguments']))
->columns(new ActionColumn(urlConfig: new UrlConfig(arguments: ['test-arguments' => 'test.arguments'])))
->id('w1-grid')
->dataReader($this->createOffsetPaginator($this->data, 10))
->render()
Expand Down Expand Up @@ -628,46 +629,14 @@ public function testUrlQueryParameters(): void
</div>
HTML,
GridView::widget()
->columns(new ActionColumn(urlQueryParameters: ['test-param' => 'test.param']))
->id('w1-grid')
->dataReader($this->createOffsetPaginator($this->data, 10))
->render()
);
}

public function testUrlParamsConfig(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table>
<thead>
<tr>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<a name="view" href="/admin/manage/view?test-param=test.param&amp;id=1" title="View" role="button" style="text-decoration: none!important;"><span>πŸ”Ž</span></a>
<a name="update" href="/admin/manage/update?test-param=test.param&amp;id=1" title="Update" role="button" style="text-decoration: none!important;"><span>✎</span></a>
<a name="delete" href="/admin/manage/delete?test-param=test.param&amp;id=1" title="Delete" role="button" style="text-decoration: none!important;"><span>❌</span></a>
</td>
</tr>
<tr>
<td>
<a name="view" href="/admin/manage/view?test-param=test.param&amp;id=2" title="View" role="button" style="text-decoration: none!important;"><span>πŸ”Ž</span></a>
<a name="update" href="/admin/manage/update?test-param=test.param&amp;id=2" title="Update" role="button" style="text-decoration: none!important;"><span>✎</span></a>
<a name="delete" href="/admin/manage/delete?test-param=test.param&amp;id=2" title="Delete" role="button" style="text-decoration: none!important;"><span>❌</span></a>
</td>
</tr>
</tbody>
</table>
<div>Page <b>1</b> of <b>1</b></div>
</div>
HTML,
GridView::widget()
->columns(new ActionColumn(urlParamsConfig: ['test-param' => 'test.param']))
->columns(
new ActionColumn(
urlConfig: new UrlConfig(
queryParameters: ['test-param' => 'test.param'],
primaryKeyPlace: UrlConfig::QUERY_PARAMETERS,
),
),
)
->id('w1-grid')
->dataReader($this->createOffsetPaginator($this->data, 10))
->render()
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Widget\WidgetFactory;
use Yiisoft\Yii\DataView\Column\ActionColumnRenderer;
use Yiisoft\Yii\DataView\Column\ActionColumnUrlCreator;
use Yiisoft\Yii\DataView\YiiRouter\ActionColumnUrlCreator;

trait TestTrait
{
Expand Down

0 comments on commit 1dda340

Please sign in to comment.