-
Notifications
You must be signed in to change notification settings - Fork 49
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
1 parent
e079cfb
commit 6a1c438
Showing
5 changed files
with
251 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\GridBundle\Builder\Field; | ||
|
||
final class CallbackField | ||
{ | ||
public static function create(string $name, callable $callback, bool $htmlspecialchars = true): FieldInterface | ||
{ | ||
return Field::create($name, 'callback') | ||
->setOption('callback', $callback) | ||
->setOption('htmlspecialchars', $htmlspecialchars) | ||
; | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Component\Grid\FieldTypes; | ||
|
||
use Sylius\Component\Grid\DataExtractor\DataExtractorInterface; | ||
use Sylius\Component\Grid\Definition\Field; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
final class CallbackFieldType implements FieldTypeInterface | ||
{ | ||
private DataExtractorInterface $dataExtractor; | ||
|
||
public function __construct(DataExtractorInterface $dataExtractor) | ||
{ | ||
$this->dataExtractor = $dataExtractor; | ||
} | ||
|
||
public function render(Field $field, $data, array $options): string | ||
{ | ||
$value = $this->dataExtractor->get($field, $data); | ||
$value = call_user_func($options['callback'], $value); | ||
|
||
if ($options['htmlspecialchars'] !== true) { | ||
return (string) $value; | ||
} | ||
|
||
return htmlspecialchars((string) $value); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setRequired('callback'); | ||
$resolver->setAllowedTypes('callback', 'callable'); | ||
|
||
$resolver->setDefault('htmlspecialchars', true); | ||
$resolver->setAllowedTypes('htmlspecialchars', 'bool'); | ||
} | ||
} |
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,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Component\Grid\FieldTypes; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Grid\DataExtractor\DataExtractorInterface; | ||
use Sylius\Component\Grid\Definition\Field; | ||
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface; | ||
|
||
final class CallbackFieldTypeSpec extends ObjectBehavior | ||
{ | ||
function let(DataExtractorInterface $dataExtractor): void | ||
{ | ||
$this->beConstructedWith($dataExtractor); | ||
} | ||
|
||
function it_is_a_grid_field_type(): void | ||
{ | ||
$this->shouldImplement(FieldTypeInterface::class); | ||
} | ||
|
||
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_callback_with_htmlspecialchars( | ||
DataExtractorInterface $dataExtractor, | ||
Field $field, | ||
): void { | ||
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar'); | ||
|
||
$this->render($field, ['foo' => 'bar'], [ | ||
'callback' => fn (string $value): string => "<strong>$value</strong>", | ||
'htmlspecialchars' => true, | ||
])->shouldReturn('<strong>bar</strong>'); | ||
} | ||
|
||
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_callback_without_htmlspecialchars( | ||
DataExtractorInterface $dataExtractor, | ||
Field $field, | ||
): void { | ||
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar'); | ||
|
||
$this->render($field, ['foo' => 'bar'], [ | ||
'callback' => fn (string $value): string => "<strong>$value</strong>", | ||
'htmlspecialchars' => false, | ||
])->shouldReturn('<strong>bar</strong>'); | ||
} | ||
|
||
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_function_callback( | ||
DataExtractorInterface $dataExtractor, | ||
Field $field, | ||
): void { | ||
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('bar'); | ||
|
||
$this->render($field, ['foo' => 'bar'], [ | ||
'callback' => 'strtoupper', | ||
'htmlspecialchars' => true, | ||
])->shouldReturn('BAR'); | ||
} | ||
|
||
function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_static_callback( | ||
DataExtractorInterface $dataExtractor, | ||
Field $field, | ||
): void { | ||
$dataExtractor->get($field, ['foo' => 'bar'])->willReturn('BAR'); | ||
|
||
$this->render($field, ['foo' => 'bar'], [ | ||
'callback' => [self::class, 'callable'], | ||
'htmlspecialchars' => true, | ||
])->shouldReturn('bar'); | ||
} | ||
|
||
static function callable($value) | ||
{ | ||
return strtolower($value); | ||
} | ||
} |