Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
c0ntax committed Jan 27, 2018
2 parents 530548a + 39ee681 commit 01192bd
Show file tree
Hide file tree
Showing 42 changed files with 698 additions and 77 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ For reasons that I can't quite imagine, you might only want to just add client s
TextType::class,
[
'parsleys' => [
new \C0ntax\ParsleyBundle\Directive\Field\Constraint\MinLength(2, 'You need more than %s chars'),
new \C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint\MinLength(2, 'You need more than %s chars'),
],
]
)
Expand Down Expand Up @@ -146,7 +146,7 @@ Let's assume that the Entity in the example above is out of your control. It's c
TextType::class,
[
'parsleys' => [
new \C0ntax\ParsleyBundle\Directive\Field\ConstraintErrorMessage(\C0ntax\ParsleyBundle\Directive\Field\Constraint\MinLength::class, 'You need more than %s chars'),
new \C0ntax\ParsleyBundle\Parsleys\Directive\Field\ConstraintErrorMessage(\C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint\MinLength::class, 'You need more than %s chars'),
],
]
)
Expand All @@ -156,6 +156,23 @@ Let's assume that the Entity in the example above is out of your control. It's c

*NOTE* The class passed to identify where to attache the error message is the ParsleyBundle one and not the Symfony one!

## Removals

There may be occassions where you want the bridge between Symfony and Parsley enabled, but specific validations 'removed' from a form element. For example, in the case of [Group Sequences](https://symfony.com/doc/current/validation/sequence_provider.html) where there is no equivalent in Parsley. With removals, you can 'turn off' the Symfony Constraint and manually add your own custom Parsely validation. For example, say we wanted to have a Regex symfony validation on the server, but not on the client side:

```php
$builder->add(
'field',
TextType::class,
[
'constraints' => [new Regex(['pattern' => '/bla/]),
'parsleys' => [new RemoveSymfonyConstraint(Regex::class)],]
]
);
```

There is also the ``RemoveParsleyConstraint()`` class that can be used to remove specific Parsley constrains. This is handy if you want to remove something that was auto-generated from a Symfony Constraint.

## Rolling your own

You can add your own parsley directives by simply implementing the ``DirectiveInterface``. The only requirement is that it passes back an array of attributes that will be injected into your form HTML.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "c0ntax/parsley-bundle",
"version": "0.4.0",
"version": "0.5.0",
"type": "symfony-bundle",
"description": "A bridge between Symfony and Parsley.js",
"license": "Apache-2.0",
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/ConstraintInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace C0ntax\ParsleyBundle\Contracts;

use C0ntax\ParsleyBundle\Directive\Field\ConstraintErrorMessage;
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\ConstraintErrorMessage;

/**
* Interface ConstraintInterface
*
* @package C0ntax\ParsleyBundle\Directive\Field\Constraint
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint
*/
interface ConstraintInterface extends DirectiveInterface
{
Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/DirectiveInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types = 1);

namespace C0ntax\ParsleyBundle\Contracts;

Expand All @@ -7,7 +8,7 @@
*
* @package C0ntax\ParsleyBundle\Contracts
*/
interface DirectiveInterface
interface DirectiveInterface extends ParsleyInterface
{
/**
* @return array
Expand Down
16 changes: 16 additions & 0 deletions src/Contracts/ParsleyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types = 1);

namespace C0ntax\ParsleyBundle\Contracts;

/**
* Interface ParsleyInterface
*
* This interface doesn't actually mandate anything. It's just to let us know that things is a parsley command
*
* @package C0ntax\ParsleyBundle\Contracts
*/
interface ParsleyInterface
{

}
19 changes: 19 additions & 0 deletions src/Contracts/RemoveInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types = 1);

namespace C0ntax\ParsleyBundle\Contracts;

/**
* Interface RemoveInterface
*
* @package C0ntax\ParsleyBundle\Contracts
*/
interface RemoveInterface extends ParsleyInterface
{
/**
* Get the fully qualified classname that is to be removed from the view
*
* @return string
*/
public function getClassName(): string;
}
4 changes: 2 additions & 2 deletions src/Factory/ConstraintFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace C0ntax\ParsleyBundle\Factory;

use C0ntax\ParsleyBundle\Contracts\ConstraintInterface;
use C0ntax\ParsleyBundle\Directive\Field\Constraint as ParsleyConstraint;
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint as ParsleyConstraint;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
Expand All @@ -16,7 +16,7 @@
/**
* Class ConstraintFactory
*
* @package C0ntax\ParsleyBundle\Directive\Field\Constraint
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint
*/
class ConstraintFactory
{
Expand Down
110 changes: 99 additions & 11 deletions src/Form/Extension/ParsleyTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

use C0ntax\ParsleyBundle\Contracts\ConstraintInterface;
use C0ntax\ParsleyBundle\Contracts\DirectiveInterface;
use C0ntax\ParsleyBundle\Directive\Field\Generic;
use C0ntax\ParsleyBundle\Contracts\ParsleyInterface;
use C0ntax\ParsleyBundle\Contracts\RemoveInterface;
use C0ntax\ParsleyBundle\Factory\ConstraintFactory;
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\Generic;
use C0ntax\ParsleyBundle\Parsleys\RemoveParsleyConstraint;
use C0ntax\ParsleyBundle\Parsleys\RemoveSymfonyConstraint;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
Expand Down Expand Up @@ -52,14 +56,22 @@ public function buildView(FormView $view, FormInterface $form, array $options):
return;
}

$constraints = array_merge(
$this->getAnnotatedConstraintsFromForm($form),
$this->getConstraintsFromForm($form)
$parsleys = $options[self::OPTION_NAME];

$symfonyConstraints = $this->removeFromConstraints(
array_merge(
$this->getAnnotatedConstraintsFromForm($form),
$this->getConstraintsFromForm($form)
),
$this->getRemoveSymfonyConstraintsFromParsleys($parsleys)
);

$parsleyConstraints = array_merge(
$this->createParsleyConstraintsFromValidationConstraints($constraints, $form),
$options[self::OPTION_NAME]
$parsleyConstraints = $this->removeFromConstraints(
array_merge(
$this->createParsleyConstraintsFromValidationConstraints($symfonyConstraints, $form),
$this->getDirectivesFromParsleys($parsleys)
),
$this->getRemoveParsleyConstraintsFromParsleys($parsleys)
);

$this->addParsleyToView($view, $parsleyConstraints);
Expand All @@ -76,13 +88,89 @@ public function getExtendedType(): string
/**
* @param OptionsResolver $resolver
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
self::OPTION_NAME => [],
]
$resolver
->setDefaults(
[
self::OPTION_NAME => [],
]
)
->addAllowedTypes(self::OPTION_NAME, 'array');
}

/**
* @param ParsleyInterface[] $parsleys
* @return DirectiveInterface[]
*/
private function getDirectivesFromParsleys(array $parsleys): array
{
$dir = array_values(
array_filter(
$parsleys,
function (ParsleyInterface $parsley) {
return $parsley instanceof DirectiveInterface;
}
)
);

return $dir;
}

/**
* @param ParsleyInterface[] $parsleys
* @return RemoveParsleyConstraint[]
*/
private function getRemoveParsleyConstraintsFromParsleys(array $parsleys): array
{
return array_values(
array_filter(
$parsleys,
function (ParsleyInterface $parsley) {
return $parsley instanceof RemoveParsleyConstraint;
}
)
);
}

/**
* @param ParsleyInterface[] $parsleys
* @return RemoveSymfonyConstraint[]
*/
private function getRemoveSymfonyConstraintsFromParsleys(array $parsleys): array
{
return array_values(
array_filter(
$parsleys,
function (ParsleyInterface $parsley) {
return $parsley instanceof RemoveSymfonyConstraint;
}
)
);
}

/**
* @param \Symfony\Component\Validator\Constraint[]|ConstraintInterface[] $constraints
* @param RemoveInterface[] $removals
* @return \Symfony\Component\Validator\Constraint[]|ConstraintInterface[]
*/
private function removeFromConstraints(array $constraints, array $removals): array
{
return array_values(
array_filter(
$constraints,
function ($constraint) use ($removals) {
foreach ($removals as $removal) {
if ($removal->getClassName() === get_class($constraint)) {
return false;
}
}

return true;
}
)
);
}

Expand Down
57 changes: 57 additions & 0 deletions src/Parsleys/AbstractRemove.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace C0ntax\ParsleyBundle\Parsleys;

use C0ntax\ParsleyBundle\Contracts\RemoveInterface;

/**
* Class AbstractRemove
*
* @package C0ntax\ParsleyBundle\Parsleys
*/
abstract class AbstractRemove implements RemoveInterface
{
/** @var string */
private $className;

/**
* AbstractRemove constructor.
*
* @param string $className
* @param string $instanceOf
* @throws \InvalidArgumentException
*/
public function __construct(string $className, string $instanceOf)
{
if (!class_exists($className)) {
throw new \InvalidArgumentException('Class '.$className.' does not exist');
}

$instancesOf = array_merge(class_implements($className), class_parents($className));

if (!in_array($instanceOf, $instancesOf, true)) {
throw new \InvalidArgumentException('Class '.$className.' does not implement '.$instanceOf);
}

$this->setClassName($className);
}

/**
* @return string
*/
public function getClassName(): string
{
return $this->className;
}

/**
* @param string $className
* @return AbstractRemove
*/
private function setClassName(string $className): AbstractRemove
{
$this->className = $className;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
declare(strict_types = 1);

namespace C0ntax\ParsleyBundle\Directive\Field\Constraint;
namespace C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint;

/**
* Class AbstractComparison
*
* @package C0ntax\ParsleyBundle\Directive\Field\Constraint
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint
*/
abstract class AbstractComparison extends AbstractConstraint
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
declare(strict_types = 1);

namespace C0ntax\ParsleyBundle\Directive\Field\Constraint;
namespace C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint;

use C0ntax\ParsleyBundle\Contracts\ConstraintInterface;
use C0ntax\ParsleyBundle\Directive\Field\ConstraintErrorMessage;
use C0ntax\ParsleyBundle\Parsleys\Directive\Field\ConstraintErrorMessage;

/**
* Class AbstractConstraint
*
* @package C0ntax\ParsleyBundle\Directive\Field\Constraint
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint
*/
abstract class AbstractConstraint implements ConstraintInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
declare(strict_types = 1);

namespace C0ntax\ParsleyBundle\Directive\Field\Constraint;
namespace C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint;

/**
* Class AbstractLength
*
* @package C0ntax\ParsleyBundle\Directive\Field\Constraint
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint
*/
abstract class AbstractLength extends AbstractConstraint
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
declare(strict_types = 1);

namespace C0ntax\ParsleyBundle\Directive\Field\Constraint;
namespace C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint;

/**
* Class Email
*
* @package C0ntax\ParsleyBundle\Directive\Field\Constraint
* @package C0ntax\ParsleyBundle\Parsleys\Directive\Field\Constraint
*/
class Email extends AbstractConstraint
{
Expand Down
Loading

0 comments on commit 01192bd

Please sign in to comment.