Skip to content

Commit dfbe8ea

Browse files
committed
refactor callback into callable
1 parent f8f212f commit dfbe8ea

File tree

12 files changed

+62
-62
lines changed

12 files changed

+62
-62
lines changed

docs/field_types.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,13 @@ $field->setOptions([
320320
]);
321321
```
322322

323-
Callback
323+
Callable
324324
--------
325325

326-
The Callback column aims to offer almost as much flexibility as the Twig column, but without requiring the creation of a template.
327-
You simply need to specify a callback, which allows you to transform the 'data' variable on the fly.
326+
The Callable column aims to offer almost as much flexibility as the Twig column, but without requiring the creation of a template.
327+
You simply need to specify a callable, which allows you to transform the 'data' variable on the fly.
328328

329-
When defining callbacks in YAML, only string representations of callables are supported.
329+
When defining callables in YAML, only string representations of callables are supported.
330330
When configuring grids using PHP (as opposed to service grid configuration), both string and array callables are supported. However, closures cannot be used due to restrictions in Symfony's configuration (values of type "Closure" are not permitted in service configuration files).
331331
By contrast, when configuring grids with service definitions, you can use both callables and closures.
332332

@@ -342,14 +342,14 @@ sylius_grid:
342342
app_user:
343343
fields:
344344
id:
345-
type: callback
345+
type: callable
346346
options:
347-
callback: "callback:App\\Helper\\GridHelper::addHashPrefix"
347+
callable: "callable:App\\Helper\\GridHelper::addHashPrefix"
348348
label: app.ui.id
349349
name:
350-
type: callback
350+
type: callable
351351
options:
352-
callback: "callback:strtoupper"
352+
callable: "callable:strtoupper"
353353
label: app.ui.name
354354
```
355355
@@ -361,24 +361,24 @@ sylius_grid:
361361
<?php
362362
// config/packages/sylius_grid.php
363363

364-
use Sylius\Bundle\GridBundle\Builder\Field\CallbackField;
364+
use Sylius\Bundle\GridBundle\Builder\Field\CallableField;
365365
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
366366
use Sylius\Bundle\GridBundle\Config\GridConfig;
367367

368368
return static function (GridConfig $grid): void {
369369
$grid->addGrid(GridBuilder::create('app_user', '%app.model.user.class%')
370370
->addField(
371-
CallbackField::create('id', 'App\\Helper\\GridHelper::addHashPrefix')
371+
CallableField::create('id', 'App\\Helper\\GridHelper::addHashPrefix')
372372
->setLabel('app.ui.id')
373373
)
374374
// or
375375
->addField(
376-
CallbackField::create('id', ['App\\Helper\\GridHelper', 'addHashPrefix'])
376+
CallableField::create('id', ['App\\Helper\\GridHelper', 'addHashPrefix'])
377377
->setLabel('app.ui.id')
378378
)
379379

380380
->addField(
381-
CallbackField::create('name', 'strtoupper')
381+
CallableField::create('name', 'strtoupper')
382382
->setLabel('app.ui.name')
383383
)
384384
)
@@ -396,7 +396,7 @@ declare(strict_types=1);
396396
namespace App\Grid;
397397

398398
use App\Entity\User;
399-
use Sylius\Bundle\GridBundle\Builder\Field\CallbackField;
399+
use Sylius\Bundle\GridBundle\Builder\Field\CallableField;
400400
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
401401
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
402402
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
@@ -412,15 +412,15 @@ final class UserGrid extends AbstractGrid implements ResourceAwareGridInterface
412412
{
413413
$gridBuilder
414414
->addField(
415-
CallbackField::create('id', GridHelper::addHashPrefix(...))
415+
CallableField::create('id', GridHelper::addHashPrefix(...))
416416
->setLabel('app.ui.id')
417417
)
418418
->addField(
419-
CallbackField::create('name', 'strtoupper')
419+
CallableField::create('name', 'strtoupper')
420420
->setLabel('app.ui.name')
421421
)
422422
->addField(
423-
CallbackField::create('roles' fn (array $roles): string => implode(', ', $roles))
423+
CallableField::create('roles' fn (array $roles): string => implode(', ', $roles))
424424
->setLabel('app.ui.roles')
425425
)
426426
;

src/Bundle/Builder/Field/CallbackField.php renamed to src/Bundle/Builder/Field/CallableField.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
namespace Sylius\Bundle\GridBundle\Builder\Field;
1515

16-
final class CallbackField
16+
final class CallableField
1717
{
18-
public static function create(string $name, callable $callback, bool $htmlspecialchars = true): FieldInterface
18+
public static function create(string $name, callable $callable, bool $htmlspecialchars = true): FieldInterface
1919
{
20-
return Field::create($name, 'callback')
21-
->setOption('callback', $callback)
20+
return Field::create($name, 'callable')
21+
->setOption('callable', $callable)
2222
->setOption('htmlspecialchars', $htmlspecialchars)
2323
;
2424
}

src/Bundle/Parser/OptionsParser.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ private function parseOption($parameter)
4545
return $parameter;
4646
}
4747

48-
if (0 === strpos($parameter, 'callback:')) {
49-
return $this->parseOptionCallback(substr($parameter, 9));
48+
if (0 === strpos($parameter, 'callable:')) {
49+
return $this->parseOptionCallable(substr($parameter, 9));
5050
}
5151

5252
return $parameter;
5353
}
5454

55-
private function parseOptionCallback(string $callback): \Closure
55+
private function parseOptionCallable(string $callable): \Closure
5656
{
57-
if (!is_callable($callback)) {
58-
throw new \RuntimeException(\sprintf('%s is not a callable.', $callback));
57+
if (!is_callable($callable)) {
58+
throw new \RuntimeException(\sprintf('%s is not a callable.', $callable));
5959
}
6060

61-
return $callback(...);
61+
return $callable(...);
6262
}
6363
}

src/Bundle/Resources/config/services/field_types.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<services>
1616
<defaults public="true" />
1717

18-
<service id="sylius.grid_field.callback" class="Sylius\Component\Grid\FieldTypes\CallbackFieldType">
18+
<service id="sylius.grid_field.callable" class="Sylius\Component\Grid\FieldTypes\CallableFieldType">
1919
<argument type="service" id="sylius.grid.data_extractor" />
20-
<tag name="sylius.grid_field" type="callback" />
20+
<tag name="sylius.grid_field" type="callable" />
2121
</service>
22-
<service id="Sylius\Component\Grid\FieldTypes\CallbackFieldType" alias="sylius.grid_field.callback" />
22+
<service id="Sylius\Component\Grid\FieldTypes\CallableFieldType" alias="sylius.grid_field.callable" />
2323

2424
<service id="sylius.grid_field.datetime" class="Sylius\Component\Grid\FieldTypes\DatetimeFieldType">
2525
<argument type="service" id="sylius.grid.data_extractor" />

src/Bundle/spec/Parser/OptionsParserSpec.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ function it_is_an_options_parser(): void
2323
$this->shouldImplement(OptionsParserInterface::class);
2424
}
2525

26-
function it_parses_options_with_callback(): void
26+
function it_parses_options_with_callable(): void
2727
{
2828
$this
2929
->parseOptions([
30-
'type' => 'callback',
30+
'type' => 'callable',
3131
'option' => [
32-
'callback' => 'callback:App\\Helper\\GridHelper::addHashPrefix',
32+
'callable' => 'callable:App\\Helper\\GridHelper::addHashPrefix',
3333
],
3434
'label' => 'app.ui.id',
3535
])
3636
->shouldBeAValidConfig([
37-
'type' => 'callback',
37+
'type' => 'callable',
3838
'option' => [],
3939
'label' => 'app.ui.id',
4040
])
@@ -49,19 +49,19 @@ public function getMatchers(): array
4949
return false;
5050
}
5151

52-
return is_callable($subject['option']['callback'] ?? null);
52+
return is_callable($subject['option']['callable'] ?? null);
5353
},
5454
];
5555
}
5656

57-
function it_fails_while_parsing_options_with_invalid_callback(): void
57+
function it_fails_while_parsing_options_with_invalid_callable(): void
5858
{
5959
$this
6060
->shouldThrow(\RuntimeException::class)
6161
->during('parseOptions', [[
62-
'type' => 'callback',
62+
'type' => 'callable',
6363
'option' => [
64-
'callback' => 'callback:foobar',
64+
'callable' => 'callable:foobar',
6565
],
6666
'label' => 'app.ui.id',
6767
]])

src/Component/FieldTypes/CallbackFieldType.php renamed to src/Component/FieldTypes/CallableFieldType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Sylius\Component\Grid\Definition\Field;
1818
use Symfony\Component\OptionsResolver\OptionsResolver;
1919

20-
final class CallbackFieldType implements FieldTypeInterface
20+
final class CallableFieldType implements FieldTypeInterface
2121
{
2222
public function __construct(private DataExtractorInterface $dataExtractor)
2323
{
@@ -26,7 +26,7 @@ public function __construct(private DataExtractorInterface $dataExtractor)
2626
public function render(Field $field, $data, array $options): string
2727
{
2828
$value = $this->dataExtractor->get($field, $data);
29-
$value = (string) call_user_func($options['callback'], $value);
29+
$value = (string) call_user_func($options['callable'], $value);
3030

3131
if ($options['htmlspecialchars']) {
3232
$value = htmlspecialchars($value);
@@ -37,8 +37,8 @@ public function render(Field $field, $data, array $options): string
3737

3838
public function configureOptions(OptionsResolver $resolver): void
3939
{
40-
$resolver->setRequired('callback');
41-
$resolver->setAllowedTypes('callback', 'callable');
40+
$resolver->setRequired('callable');
41+
$resolver->setAllowedTypes('callable', 'callable');
4242

4343
$resolver->setDefault('htmlspecialchars', true);
4444
$resolver->setAllowedTypes('htmlspecialchars', 'bool');

src/Component/spec/FieldTypes/CallbackFieldTypeSpec.php renamed to src/Component/spec/FieldTypes/CallableFieldTypeSpec.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Sylius\Component\Grid\Definition\Field;
1919
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface;
2020

21-
final class CallbackFieldTypeSpec extends ObjectBehavior
21+
final class CallableFieldTypeSpec extends ObjectBehavior
2222
{
2323
function let(DataExtractorInterface $dataExtractor): void
2424
{
@@ -30,50 +30,50 @@ function it_is_a_grid_field_type(): void
3030
$this->shouldImplement(FieldTypeInterface::class);
3131
}
3232

33-
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_callback_with_htmlspecialchars(
33+
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_callable_with_htmlspecialchars(
3434
DataExtractorInterface $dataExtractor,
3535
Field $field,
3636
): void {
3737
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar');
3838

3939
$this->render($field, ['foo' => 'bar'], [
40-
'callback' => fn (string $value): string => "<strong>$value</strong>",
40+
'callable' => fn (string $value): string => "<strong>$value</strong>",
4141
'htmlspecialchars' => true,
4242
])->shouldReturn('&lt;strong&gt;bar&lt;/strong&gt;');
4343
}
4444

45-
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_callback_without_htmlspecialchars(
45+
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_callable_without_htmlspecialchars(
4646
DataExtractorInterface $dataExtractor,
4747
Field $field,
4848
): void {
4949
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar');
5050

5151
$this->render($field, ['foo' => 'bar'], [
52-
'callback' => fn (string $value): string => "<strong>$value</strong>",
52+
'callable' => fn (string $value): string => "<strong>$value</strong>",
5353
'htmlspecialchars' => false,
5454
])->shouldReturn('<strong>bar</strong>');
5555
}
5656

57-
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_function_callback(
57+
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_function_callable(
5858
DataExtractorInterface $dataExtractor,
5959
Field $field,
6060
): void {
6161
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar');
6262

6363
$this->render($field, ['foo' => 'bar'], [
64-
'callback' => 'strtoupper',
64+
'callable' => 'strtoupper',
6565
'htmlspecialchars' => true,
6666
])->shouldReturn('BAR');
6767
}
6868

69-
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_static_callback(
69+
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_static_callable(
7070
DataExtractorInterface $dataExtractor,
7171
Field $field,
7272
): void {
7373
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('BAR');
7474

7575
$this->render($field, ['foo' => 'bar'], [
76-
'callback' => [self::class, 'callable'],
76+
'callable' => [self::class, 'callable'],
7777
'htmlspecialchars' => true,
7878
])->shouldReturn('bar');
7979
}

tests/Application/config/sylius/grids.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ sylius_grid:
3333
title: asc
3434
fields:
3535
title:
36-
type: callback
36+
type: callable
3737
options:
38-
callback: "callback:strtoupper"
38+
callable: "callable:strtoupper"
3939
label: Title
4040
sortable: ~
4141
author:
@@ -62,9 +62,9 @@ sylius_grid:
6262
name: asc
6363
fields:
6464
id:
65-
type: callback
65+
type: callable
6666
options:
67-
callback: "callback:App\\Helper\\GridHelper::addHashPrefix"
67+
callable: "callable:App\\Helper\\GridHelper::addHashPrefix"
6868
label: ID
6969
sortable: ~
7070
name:

tests/Application/config/sylius/grids/author.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
use Sylius\Bundle\GridBundle\Builder\Field\CallbackField;
14+
use Sylius\Bundle\GridBundle\Builder\Field\CallableField;
1515
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
1616
use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter;
1717
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
@@ -23,7 +23,7 @@
2323
->addFilter(StringFilter::create('name'))
2424
->orderBy('name', 'asc')
2525
->addField(
26-
CallbackField::create('id', ['App\\Helper\\GridHelper', 'addHashPrefix'])
26+
CallableField::create('id', ['App\\Helper\\GridHelper', 'addHashPrefix'])
2727
->setSortable(true),
2828
)
2929
->addField(

tests/Application/config/sylius/grids/book.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use App\Entity\Author;
1515
use App\Entity\Book;
1616
use App\Grid\Builder\NationalityFilter;
17-
use Sylius\Bundle\GridBundle\Builder\Field\CallbackField;
17+
use Sylius\Bundle\GridBundle\Builder\Field\CallableField;
1818
use Sylius\Bundle\GridBundle\Builder\Field\StringField;
1919
use Sylius\Bundle\GridBundle\Builder\Filter\EntityFilter;
2020
use Sylius\Bundle\GridBundle\Builder\Filter\SelectFilter;
@@ -49,7 +49,7 @@
4949
)
5050
->orderBy('title', 'asc')
5151
->addField(
52-
CallbackField::create('title', 'strtoupper')
52+
CallableField::create('title', 'strtoupper')
5353
->setLabel('Title')
5454
->setSortable(true),
5555
)

0 commit comments

Comments
 (0)