-
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.
Introduced a specific type for field triggering
- Loading branch information
Showing
3 changed files
with
104 additions
and
2 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,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,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'], | ||
], | ||
]; | ||
} | ||
} |