Skip to content

Instantly share code, notes, and snippets.

@cristianoc72
Last active January 14, 2022 22:07

Revisions

  1. cristianoc72 revised this gist Jun 23, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion violations_to_array.php
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ function violations_to_array(ConstraintViolationList $violationsList, $propertyP

    if (null !== $propertyPath) {
    if (array_key_exists($propertyPath, $output)) {
    $output = array_intersect_key($output, array($propertyPath => 0));
    $output = array($propertyPath => $output[$propertyPath]);
    } else {
    return array();
    }
  2. cristianoc72 revised this gist Jun 23, 2013. 1 changed file with 0 additions and 52 deletions.
    52 changes: 0 additions & 52 deletions violationsToArrayFunctionTest.php
    Original file line number Diff line number Diff line change
    @@ -1,52 +0,0 @@
    <?php

    require 'violations_to_array.php';

    use Symfony\Component\Validator\ConstraintViolation;
    use Symfony\Component\Validator\ConstraintViolationList;

    class ConstraintViolationListTest extends \PHPUnit_Framework_TestCase
    {
    protected $list;

    protected function setUp()
    {
    $this->list = new ConstraintViolationList();
    }

    protected function tearDown()
    {
    $this->list = null;
    }

    public function testToArray()
    {
    $this->list = new ConstraintViolationList(array(
    $this->getViolation('Error 1', 'Root', 'foo'),
    $this->getViolation('Error 2', 'Root', 'foo'),
    $this->getViolation('Error 3', 'Root', 'bar'),
    $this->getViolation('Error 4', 'Root 2', 'foo'),
    $this->getViolation('Error 5', 'Root 3', 'baz'),
    ));

    $expectedNoFilters = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4'),
    'bar' => array('Error 3'),
    'baz' => array('Error 5')
    );
    $this->assertEquals($expectedNoFilters, violations_to_array($this->list));

    $expectedFilterPropertyPath = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4')
    );
    $this->assertEquals($expectedFilterPropertyPath, violations_to_array($this->list, 'foo'));

    //Wrong value causes to return empty array
    $this->assertEquals(array(), violations_to_array($this->list, 'wrong'));
    }

    protected function getViolation($message, $root = null, $propertyPath = null)
    {
    return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
    }
    }
  3. cristianoc72 revised this gist Jun 23, 2013. 1 changed file with 52 additions and 0 deletions.
    52 changes: 52 additions & 0 deletions violationsToArrayFunctionTest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php

    require 'violations_to_array.php';

    use Symfony\Component\Validator\ConstraintViolation;
    use Symfony\Component\Validator\ConstraintViolationList;

    class ConstraintViolationListTest extends \PHPUnit_Framework_TestCase
    {
    protected $list;

    protected function setUp()
    {
    $this->list = new ConstraintViolationList();
    }

    protected function tearDown()
    {
    $this->list = null;
    }

    public function testToArray()
    {
    $this->list = new ConstraintViolationList(array(
    $this->getViolation('Error 1', 'Root', 'foo'),
    $this->getViolation('Error 2', 'Root', 'foo'),
    $this->getViolation('Error 3', 'Root', 'bar'),
    $this->getViolation('Error 4', 'Root 2', 'foo'),
    $this->getViolation('Error 5', 'Root 3', 'baz'),
    ));

    $expectedNoFilters = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4'),
    'bar' => array('Error 3'),
    'baz' => array('Error 5')
    );
    $this->assertEquals($expectedNoFilters, violations_to_array($this->list));

    $expectedFilterPropertyPath = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4')
    );
    $this->assertEquals($expectedFilterPropertyPath, violations_to_array($this->list, 'foo'));

    //Wrong value causes to return empty array
    $this->assertEquals(array(), violations_to_array($this->list, 'wrong'));
    }

    protected function getViolation($message, $root = null, $propertyPath = null)
    {
    return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
    }
    }
  4. cristianoc72 revised this gist Jun 23, 2013. 1 changed file with 0 additions and 52 deletions.
    52 changes: 0 additions & 52 deletions violationsToArrayFunctionTest.php
    Original file line number Diff line number Diff line change
    @@ -1,52 +0,0 @@
    <?php

    require 'violations_to_array.php';

    use Symfony\Component\Validator\ConstraintViolation;
    use Symfony\Component\Validator\ConstraintViolationList;

    class ConstraintViolationListTest extends \PHPUnit_Framework_TestCase
    {
    protected $list;

    protected function setUp()
    {
    $this->list = new ConstraintViolationList();
    }

    protected function tearDown()
    {
    $this->list = null;
    }

    public function testToArray()
    {
    $this->list = new ConstraintViolationList(array(
    $this->getViolation('Error 1', 'Root', 'foo'),
    $this->getViolation('Error 2', 'Root', 'foo'),
    $this->getViolation('Error 3', 'Root', 'bar'),
    $this->getViolation('Error 4', 'Root 2', 'foo'),
    $this->getViolation('Error 5', 'Root 3', 'baz'),
    ));

    $expectedNoFilters = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4'),
    'bar' => array('Error 3'),
    'baz' => array('Error 5')
    );
    $this->assertEquals($expectedNoFilters, violations_to_array($this->list));

    $expectedFilterPropertyPath = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4')
    );
    $this->assertEquals($expectedFilterPropertyPath, violations_to_array($this->list, 'foo'));

    //Wrong value causes to return empty array
    $this->assertEquals(array(), violations_to_array($this->list, 'wrong'));
    }

    protected function getViolation($message, $root = null, $propertyPath = null)
    {
    return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
    }
    }
  5. cristianoc72 revised this gist Jun 23, 2013. 1 changed file with 52 additions and 0 deletions.
    52 changes: 52 additions & 0 deletions violationsToArrayFunctionTest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php

    require 'violations_to_array.php';

    use Symfony\Component\Validator\ConstraintViolation;
    use Symfony\Component\Validator\ConstraintViolationList;

    class ConstraintViolationListTest extends \PHPUnit_Framework_TestCase
    {
    protected $list;

    protected function setUp()
    {
    $this->list = new ConstraintViolationList();
    }

    protected function tearDown()
    {
    $this->list = null;
    }

    public function testToArray()
    {
    $this->list = new ConstraintViolationList(array(
    $this->getViolation('Error 1', 'Root', 'foo'),
    $this->getViolation('Error 2', 'Root', 'foo'),
    $this->getViolation('Error 3', 'Root', 'bar'),
    $this->getViolation('Error 4', 'Root 2', 'foo'),
    $this->getViolation('Error 5', 'Root 3', 'baz'),
    ));

    $expectedNoFilters = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4'),
    'bar' => array('Error 3'),
    'baz' => array('Error 5')
    );
    $this->assertEquals($expectedNoFilters, violations_to_array($this->list));

    $expectedFilterPropertyPath = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4')
    );
    $this->assertEquals($expectedFilterPropertyPath, violations_to_array($this->list, 'foo'));

    //Wrong value causes to return empty array
    $this->assertEquals(array(), violations_to_array($this->list, 'wrong'));
    }

    protected function getViolation($message, $root = null, $propertyPath = null)
    {
    return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
    }
    }
  6. cristianoc72 revised this gist Jun 23, 2013. 1 changed file with 0 additions and 52 deletions.
    52 changes: 0 additions & 52 deletions violationsToArrayFunctionTest.php
    Original file line number Diff line number Diff line change
    @@ -1,52 +0,0 @@
    <?php

    require 'violations_to_array.php';

    use Symfony\Component\Validator\ConstraintViolation;
    use Symfony\Component\Validator\ConstraintViolationList;

    class ViolationsToArrayFunctionTest extends \PHPUnit_Framework_TestCase
    {
    protected $list;

    protected function setUp()
    {
    $this->list = new ConstraintViolationList();
    }

    protected function tearDown()
    {
    $this->list = null;
    }

    public function testToArray()
    {
    $this->list = new ConstraintViolationList(array(
    $this->getViolation('Error 1', 'Root', 'foo'),
    $this->getViolation('Error 2', 'Root', 'foo'),
    $this->getViolation('Error 3', 'Root', 'bar'),
    $this->getViolation('Error 4', 'Root 2', 'foo'),
    $this->getViolation('Error 5', 'Root 3', 'baz'),
    ));

    $expectedNoFilters = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4'),
    'bar' => array('Error 3'),
    'baz' => array('Error 5')
    );
    $this->assertEquals($expectedNoFilters, violations_to_array($this->list));

    $expectedFilterPropertyPath = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4')
    );
    $this->assertEquals($expectedFilterPropertyPath, violations_to_array($this->list, 'foo'));

    //Wrong value causes to return empty array
    $this->assertEquals(array(), violations_to_array($this->list, 'wrong'));
    }

    protected function getViolation($message, $root = null, $propertyPath = null)
    {
    return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
    }
    }
  7. cristianoc72 revised this gist Jun 23, 2013. 1 changed file with 52 additions and 0 deletions.
    52 changes: 52 additions & 0 deletions violationsToArrayFunctionTest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php

    require 'violations_to_array.php';

    use Symfony\Component\Validator\ConstraintViolation;
    use Symfony\Component\Validator\ConstraintViolationList;

    class ViolationsToArrayFunctionTest extends \PHPUnit_Framework_TestCase
    {
    protected $list;

    protected function setUp()
    {
    $this->list = new ConstraintViolationList();
    }

    protected function tearDown()
    {
    $this->list = null;
    }

    public function testToArray()
    {
    $this->list = new ConstraintViolationList(array(
    $this->getViolation('Error 1', 'Root', 'foo'),
    $this->getViolation('Error 2', 'Root', 'foo'),
    $this->getViolation('Error 3', 'Root', 'bar'),
    $this->getViolation('Error 4', 'Root 2', 'foo'),
    $this->getViolation('Error 5', 'Root 3', 'baz'),
    ));

    $expectedNoFilters = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4'),
    'bar' => array('Error 3'),
    'baz' => array('Error 5')
    );
    $this->assertEquals($expectedNoFilters, violations_to_array($this->list));

    $expectedFilterPropertyPath = array(
    'foo' => array('Error 1', 'Error 2', 'Error 4')
    );
    $this->assertEquals($expectedFilterPropertyPath, violations_to_array($this->list, 'foo'));

    //Wrong value causes to return empty array
    $this->assertEquals(array(), violations_to_array($this->list, 'wrong'));
    }

    protected function getViolation($message, $root = null, $propertyPath = null)
    {
    return new ConstraintViolation($message, $message, array(), $root, $propertyPath, null);
    }
    }
  8. cristianoc72 created this gist Jun 23, 2013.
    45 changes: 45 additions & 0 deletions violations_to_array.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php
    /**
    * This function was originally created to be used inside a Propel2 project.
    * Anyway, it can be used in every project following the PSR-0 and using the Symfony Validator Component.
    *
    * @see http://symfony.com/doc/current/book/validation.html
    * @see https://github.com/propelorm/Propel2/blob/master/documentation/behaviors/validate.markdown
    */

    use Symfony\Component\Validator\ConstraintViolationList;

    /**
    * Extract, from a ConstraintViolationList object, an associative array as follow:
    * <code>
    * array(
    * 'property1.1' => array('message1.1', 'message1.2'),
    * 'property1.2' => array('message2')
    * ........
    * )
    * </code>
    * with the possibility to filter a given property path.
    *
    * @param $violationsList ConstraintViolationList object
    * @param string $propertyPath The name of the property to filter
    *
    * @return array
    */
    function violations_to_array(ConstraintViolationList $violationsList, $propertyPath = null)
    {
    $output = array();

    foreach ($violationsList as $violation) {
    $output[$violation->getPropertyPath()][] = $violation->getMessage();
    }

    if (null !== $propertyPath) {
    if (array_key_exists($propertyPath, $output)) {
    $output = array_intersect_key($output, array($propertyPath => 0));
    } else {
    return array();
    }
    }

    return $output;
    }