Skip to content

Commit 0a4175a

Browse files
ISSUE-218: Implemented BooleanToYesNoSubscriber
1 parent 3303042 commit 0a4175a

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Learn more about it in its [documentation](https://github.com/darkwebdesign/symf
2424

2525
### Event Subscribers
2626

27+
* BooleanToYesNoSubscriber, rewrites boolean values to "yes" or "no", to be used with the `BooleanType`.
2728
* JsonSchemaSubscriber, rewrites the JSON Schema `$schema` keyword property.
2829

2930
## Installing via Composer

src/BooleanToYesNoSubscriber.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2021 DarkWeb Design
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace DarkWebDesign\SymfonyAddonFormTypes;
24+
25+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26+
use Symfony\Component\Form\FormEvent;
27+
use Symfony\Component\Form\FormEvents;
28+
29+
/**
30+
* Boolean to "yes" or "no" form event subscriber.
31+
*
32+
* @author Raymond Schouten
33+
*
34+
* @since 5.2.1
35+
*/
36+
class BooleanToYesNoSubscriber implements EventSubscriberInterface
37+
{
38+
/** @var string[] */
39+
private $fieldNames;
40+
41+
public function __construct(array $fieldNames)
42+
{
43+
$this->fieldNames = $fieldNames;
44+
}
45+
46+
/**
47+
* Rewrites the pure boolean values to "yes" or "no".
48+
*/
49+
public function onPreSubmit(FormEvent $event): void
50+
{
51+
$data = $event->getData();
52+
53+
if (!is_array($data)) {
54+
return;
55+
}
56+
57+
foreach ($this->fieldNames as $fieldName) {
58+
if (!array_key_exists($fieldName, $data)) {
59+
continue;
60+
}
61+
62+
$data[$fieldName] = true === $data[$fieldName] ? 'yes' : (false === $data[$fieldName] ? 'no' : null);
63+
}
64+
65+
$event->setData($data);
66+
}
67+
68+
/**
69+
* Returns an array of event names this subscriber wants to listen to.
70+
*/
71+
public static function getSubscribedEvents(): array
72+
{
73+
return [
74+
FormEvents::PRE_SUBMIT => 'onPreSubmit',
75+
];
76+
}
77+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2021 DarkWeb Design
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace DarkWebDesign\SymfonyAddonFormTypes\Tests;
24+
25+
use DarkWebDesign\SymfonyAddonFormTypes\BooleanToYesNoSubscriber;
26+
use DarkWebDesign\SymfonyAddonFormTypes\BooleanType;
27+
use Symfony\Component\Form\Test\TypeTestCase;
28+
29+
/**
30+
* @covers \DarkWebDesign\SymfonyAddonFormTypes\BooleanToYesNoSubscriber
31+
*
32+
* @uses \DarkWebDesign\SymfonyAddonFormTypes\BooleanType
33+
* @uses \DarkWebDesign\SymfonyAddonTransformers\BooleanToValueTransformer
34+
*/
35+
class BooleanToYesNoSubscriberTest extends TypeTestCase
36+
{
37+
/**
38+
* @dataProvider provider
39+
*/
40+
public function test(?bool $value): void
41+
{
42+
$form = $this->factory->createBuilder()
43+
->add('isActive', BooleanType::class)
44+
->addEventSubscriber(new BooleanToYesNoSubscriber(['isActive']))
45+
->getForm();
46+
47+
$form->submit([
48+
'isActive' => $value,
49+
]);
50+
51+
$this->assertTrue($form->isSynchronized());
52+
$this->assertSame($value, $form->get('isActive')->getData());
53+
}
54+
55+
public function testDataNoField(): void
56+
{
57+
$form = $this->factory->createBuilder()
58+
->add('isActive', BooleanType::class)
59+
->addEventSubscriber(new BooleanToYesNoSubscriber(['isActive']))
60+
->getForm();
61+
62+
$form->submit([]);
63+
64+
$this->assertTrue($form->isSynchronized());
65+
$this->assertNull($form->get('isActive')->getData());
66+
}
67+
68+
public function testDataNotArray(): void
69+
{
70+
$form = $this->factory->createBuilder()
71+
->add('isActive', BooleanType::class)
72+
->addEventSubscriber(new BooleanToYesNoSubscriber(['isActive']))
73+
->getForm();
74+
75+
$form->submit('not-array');
76+
77+
$this->assertFalse($form->isSynchronized());
78+
$this->assertNull($form->get('isActive')->getData());
79+
}
80+
81+
public function provider(): array
82+
{
83+
return [
84+
'true' => [true],
85+
'false' => [false],
86+
'null' => [null],
87+
];
88+
}
89+
}

0 commit comments

Comments
 (0)