-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
> The Callable column aims to offer almost as much flexibility as the Twig column, but without requiring the creation of a template. > You simply need to specify a callable, which allows you to transform the 'data' variable on the fly. The documentation associated for this PR can be found here Sylius/Stack#228
- Loading branch information
Showing
23 changed files
with
528 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^Dead catch \\- Throwable is never thrown in the try block\\.$#" | ||
count: 1 | ||
path: src/Component/FieldTypes/CallableFieldType.php |
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 CallableField | ||
{ | ||
public static function create(string $name, callable $callable, bool $htmlspecialchars = true): FieldInterface | ||
{ | ||
return Field::create($name, 'callable') | ||
->setOption('callable', $callable) | ||
->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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\Parser; | ||
|
||
use Sylius\Component\Grid\Exception\InvalidArgumentException; | ||
|
||
final class OptionsParser implements OptionsParserInterface | ||
{ | ||
public function parseOptions(array $parameters): array | ||
{ | ||
return array_map( | ||
function (mixed $parameter): mixed { | ||
if (is_array($parameter)) { | ||
return $this->parseOptions($parameter); | ||
} | ||
|
||
return $this->parseOption($parameter); | ||
}, | ||
$parameters, | ||
); | ||
} | ||
|
||
private function parseOption(mixed $parameter): mixed | ||
{ | ||
if (!is_string($parameter)) { | ||
return $parameter; | ||
} | ||
|
||
if (str_starts_with($parameter, 'callable:')) { | ||
return $this->parseOptionCallable(substr($parameter, 9)); | ||
} | ||
|
||
return $parameter; | ||
} | ||
|
||
private function parseOptionCallable(string $callable): \Closure | ||
{ | ||
if (!is_callable($callable)) { | ||
throw new InvalidArgumentException(\sprintf('%s is not a callable.', $callable)); | ||
} | ||
|
||
return $callable(...); | ||
} | ||
} |
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,19 @@ | ||
<?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\Parser; | ||
|
||
interface OptionsParserInterface | ||
{ | ||
public function parseOptions(array $parameters): array; | ||
} |
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,57 @@ | ||
<?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\Tests\Unit\Parser; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\Bundle\GridBundle\Parser\OptionsParser; | ||
use Sylius\Bundle\GridBundle\Parser\OptionsParserInterface; | ||
use Sylius\Component\Grid\Exception\InvalidArgumentException; | ||
|
||
final class OptionsParserTest extends TestCase | ||
{ | ||
public function testItImplementsOptionsParserInterface(): void | ||
{ | ||
$this->assertInstanceOf(OptionsParserInterface::class, new OptionsParser()); | ||
} | ||
|
||
public function testItParsesOptionsWithCallable(): void | ||
{ | ||
$options = (new OptionsParser())->parseOptions([ | ||
'type' => 'callable', | ||
'option' => [ | ||
'callable' => 'callable:strtoupper', | ||
], | ||
'label' => 'app.ui.id', | ||
]); | ||
|
||
$this->assertArrayHasKey('type', $options); | ||
$this->assertArrayHasKey('option', $options); | ||
$this->assertArrayHasKey('label', $options); | ||
|
||
$this->assertIsCallable($options['option']['callable'] ?? null); | ||
} | ||
|
||
public function testItFailsWhileParsingOptionsWithInvalidCallable(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$options = (new OptionsParser())->parseOptions([ | ||
'type' => 'callable', | ||
'option' => [ | ||
'callable' => 'callable:foobar', | ||
], | ||
'label' => 'app.ui.id', | ||
]); | ||
} | ||
} |
Oops, something went wrong.