[Form] Implemented ArrayToPartsTransformer and ValueToDuplicatesTransformer

This commit is contained in:
Bernhard Schussek 2011-02-23 22:30:15 +01:00
parent 5a2404a1d6
commit b5671c1be0
4 changed files with 414 additions and 0 deletions

View File

@ -0,0 +1,84 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class ArrayToPartsTransformer implements ValueTransformerInterface
{
private $partMapping;
public function __construct(array $partMapping)
{
$this->partMapping = $partMapping;
}
public function transform($array)
{
if (null === $array) {
$array = array();
}
if (!is_array($array) ) {
throw new UnexpectedTypeException($array, 'array');
}
$result = array();
foreach ($this->partMapping as $partKey => $originalKeys) {
if (empty($array)) {
$result[$partKey] = null;
} else {
$result[$partKey] = array_intersect_key($array, array_flip($originalKeys));
}
}
return $result;
}
public function reverseTransform($array)
{
if (!is_array($array) ) {
throw new UnexpectedTypeException($array, 'array');
}
$result = array();
$emptyKeys = array();
foreach ($this->partMapping as $partKey => $originalKeys) {
if (!empty($array[$partKey])) {
foreach ($originalKeys as $originalKey) {
if (isset($array[$partKey][$originalKey])) {
$result[$originalKey] = $array[$partKey][$originalKey];
}
}
} else {
$emptyKeys[] = $partKey;
}
}
if (count($emptyKeys) > 0) {
if (count($emptyKeys) === count($this->partMapping)) {
// All parts empty
return null;
}
throw new TransformationFailedException(sprintf(
'The keys "%s" should not be empty', implode('", "', $emptyKeys)));
}
return $result;
}
}

View File

@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class ValueToDuplicatesTransformer implements ValueTransformerInterface
{
private $keys;
public function __construct(array $keys)
{
$this->keys = $keys;
}
public function transform($value)
{
$result = array();
foreach ($this->keys as $key) {
$result[$key] = $value;
}
return $result;
}
public function reverseTransform($array)
{
if (!is_array($array) ) {
throw new UnexpectedTypeException($array, 'array');
}
$result = current($array);
$emptyKeys = array();
foreach ($this->keys as $key) {
if (!empty($array[$key])) {
if ($array[$key] !== $result) {
throw new TransformationFailedException(
'All values in the array should be the same');
}
} else {
$emptyKeys[] = $key;
}
}
if (count($emptyKeys) > 0) {
if (count($emptyKeys) === count($this->keys)) {
// All keys empty
return null;
}
throw new TransformationFailedException(sprintf(
'The keys "%s" should not be empty', implode('", "', $emptyKeys)));
}
return $result;
}
}

View File

@ -0,0 +1,144 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form\ValueTransformer;
use Symfony\Component\Form\ValueTransformer\ArrayToPartsTransformer;
class ArrayToPartsTransformerTest extends \PHPUnit_Framework_TestCase
{
private $transformer;
protected function setUp()
{
$this->transformer = new ArrayToPartsTransformer(array(
'first' => array('a', 'b', 'c'),
'second' => array('d', 'e', 'f'),
));
}
public function testTransform()
{
$input = array(
'a' => '1',
'b' => '2',
'c' => '3',
'd' => '4',
'e' => '5',
'f' => '6',
);
$output = array(
'first' => array(
'a' => '1',
'b' => '2',
'c' => '3',
),
'second' => array(
'd' => '4',
'e' => '5',
'f' => '6',
),
);
$this->assertSame($output, $this->transformer->transform($input));
}
public function testTransform_empty()
{
$output = array(
'first' => null,
'second' => null,
);
$this->assertSame($output, $this->transformer->transform(null));
}
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function testTransformRequiresArray()
{
$this->transformer->transform('12345');
}
public function testReverseTransform()
{
$input = array(
'first' => array(
'a' => '1',
'b' => '2',
'c' => '3',
),
'second' => array(
'd' => '4',
'e' => '5',
'f' => '6',
),
);
$output = array(
'a' => '1',
'b' => '2',
'c' => '3',
'd' => '4',
'e' => '5',
'f' => '6',
);
$this->assertSame($output, $this->transformer->reverseTransform($input));
}
public function testReverseTransform_completelyEmpty()
{
$input = array(
'first' => '',
'second' => '',
);
$this->assertNull($this->transformer->reverseTransform($input));
}
public function testReverseTransform_completelyNull()
{
$input = array(
'first' => null,
'second' => null,
);
$this->assertNull($this->transformer->reverseTransform($input));
}
/**
* @expectedException Symfony\Component\Form\ValueTransformer\TransformationFailedException
*/
public function testReverseTransform_partiallyNull()
{
$input = array(
'first' => array(
'a' => '1',
'b' => '2',
'c' => '3',
),
'second' => null,
);
$this->transformer->reverseTransform($input);
}
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function testReverseTransformRequiresArray()
{
$this->transformer->reverseTransform('12345');
}
}

View File

@ -0,0 +1,115 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form\ValueTransformer;
use Symfony\Component\Form\ValueTransformer\ValueToDuplicatesTransformer;
class ValueToDuplicatesTransformerTest extends \PHPUnit_Framework_TestCase
{
private $transformer;
protected function setUp()
{
$this->transformer = new ValueToDuplicatesTransformer(array('a', 'b', 'c'));
}
public function testTransform()
{
$output = array(
'a' => 'Foo',
'b' => 'Foo',
'c' => 'Foo',
);
$this->assertSame($output, $this->transformer->transform('Foo'));
}
public function testTransform_empty()
{
$output = array(
'a' => null,
'b' => null,
'c' => null,
);
$this->assertSame($output, $this->transformer->transform(null));
}
public function testReverseTransform()
{
$input = array(
'a' => 'Foo',
'b' => 'Foo',
'c' => 'Foo',
);
$this->assertSame('Foo', $this->transformer->reverseTransform($input));
}
public function testReverseTransform_completelyEmpty()
{
$input = array(
'a' => '',
'b' => '',
'c' => '',
);
$this->assertNull($this->transformer->reverseTransform($input));
}
public function testReverseTransform_completelyNull()
{
$input = array(
'a' => null,
'b' => null,
'c' => null,
);
$this->assertNull($this->transformer->reverseTransform($input));
}
/**
* @expectedException Symfony\Component\Form\ValueTransformer\TransformationFailedException
*/
public function testReverseTransform_partiallyNull()
{
$input = array(
'a' => 'Foo',
'b' => 'Foo',
'c' => null,
);
$this->transformer->reverseTransform($input);
}
/**
* @expectedException Symfony\Component\Form\ValueTransformer\TransformationFailedException
*/
public function testReverseTransform_differences()
{
$input = array(
'a' => 'Foo',
'b' => 'Bar',
'c' => 'Foo',
);
$this->transformer->reverseTransform($input);
}
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function testReverseTransformRequiresArray()
{
$this->transformer->reverseTransform('12345');
}
}