-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
203 additions
and
8 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
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,68 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace C0ntax\ParsleyBundle\Parsleys\Directive\Field; | ||
|
||
use C0ntax\ParsleyBundle\Contracts\DirectiveInterface; | ||
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint\AbstractConstraint; | ||
|
||
/** | ||
* Class Trigger | ||
* | ||
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field | ||
*/ | ||
class Trigger implements DirectiveInterface | ||
{ | ||
/** @var string */ | ||
private $key = 'trigger'; | ||
|
||
/** @var string */ | ||
private $value; | ||
|
||
/** | ||
* Trigger constructor. | ||
* | ||
* @param string $value | ||
*/ | ||
public function __construct(string $value) | ||
{ | ||
$this->setValue($value); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getViewAttr(): array | ||
{ | ||
return [ | ||
implode('-', [AbstractConstraint::DATA_ATTRIBUTE_PREFIX, $this->getKey()]) => $this->getValue(), | ||
]; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function getKey(): string | ||
{ | ||
return $this->key; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function getValue(): string | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* @return Trigger | ||
*/ | ||
private function setValue(string $value): Trigger | ||
{ | ||
$this->value = $value; | ||
|
||
return $this; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace C0ntax\ParsleyBundle\Tests\Fixtures\Entity; | ||
|
||
class TriggerEntity | ||
{ | ||
private $check = false; | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isCheck(): bool | ||
{ | ||
return $this->check; | ||
} | ||
|
||
/** | ||
* @param bool $check | ||
* @return TriggerEntity | ||
*/ | ||
public function setCheck(bool $check) | ||
{ | ||
$this->check = $check; | ||
|
||
return $this; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace C0ntax\ParsleyBundle\Tests\Fixtures\Form\Type; | ||
|
||
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint\Required; | ||
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Trigger; | ||
use C0ntax\ParsleyBundle\Tests\Fixtures\Entity\TriggerEntity; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class TestTriggerType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add( | ||
'check', | ||
CheckboxType::class, | ||
[ | ||
'parsleys' => [ | ||
new Required(), | ||
new Trigger('click') | ||
], | ||
] | ||
) | ||
; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(['data_class' => TriggerEntity::class]); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace C0ntax\ParsleyBundle\Tests\Parsleys\Directive\Field; | ||
|
||
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Trigger; | ||
|
||
class TriggerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider createGetViewAttrTestData | ||
*/ | ||
public function testGetViewAttr(string $value, array $expected) | ||
{ | ||
$trigger = new Trigger($value); | ||
self::assertEquals($expected, $trigger->getViewAttr()); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function createGetViewAttrTestData(): array | ||
{ | ||
return [ | ||
[ | ||
'value' => 'click', | ||
'expected' => ['data-parsley-trigger' => 'click'], | ||
], | ||
[ | ||
'value' => 'change focus', | ||
'expected' => ['data-parsley-trigger' => 'change focus'], | ||
], | ||
]; | ||
} | ||
} |