<?php | |
namespace Acme\DemoBundle\Tests; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
use Acme\DemoBundle\Entity\IP; | |
use Acme\DemoBundle\Form\IPExampleType; | |
class IPExampleTest extends WebTestCase | |
{ | |
/** | |
* @test | |
*/ | |
public function shouldConvertValueOfIpToInt() | |
{ | |
$kernel = static::createKernel(); | |
$kernel->boot(); | |
$ip = new IP(); | |
$form = $kernel->getContainer()->get('form.factory')->create(new IPExampleType(), $ip); | |
$form->bind(array('ip' => '80.0.0.1')); | |
$this->assertSame(1342177281, $form->getData()->getIp()); | |
} | |
/** | |
* @test | |
*/ | |
public function shouldShowIpInNormalFormat() | |
{ | |
$kernel = static::createKernel(); | |
$kernel->boot(); | |
$ip = new IP(); | |
$ip->setIp(1342177281); | |
$form = $kernel->getContainer()->get('form.factory')->create(new IPExampleType(), $ip); | |
$this->assertSame('80.0.0.1', $form->createView()->getChild('ip')->get('value')); | |
} | |
} |
<?php | |
namespace Acme\DemoBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilder; | |
class IPExampleType extends AbstractType | |
{ | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$builder | |
->add('ip', 'ip') | |
->add('high') | |
->add('hoster') | |
->add('scandate') | |
; | |
} | |
public function getName() | |
{ | |
return 'ip_example'; | |
} | |
} |
<?php | |
namespace Acme\DemoBundle\Form; | |
use Symfony\Component\Form\DataTransformerInterface; | |
use Symfony\Component\Form\Exception\TransformationFailedException; | |
class IpTransformer implements DataTransformerInterface | |
{ | |
public function transform($ip) | |
{ | |
return long2ip($ip); | |
} | |
public function reverseTransform($ip) | |
{ | |
return ip2long($ip); | |
} | |
} |
<?php | |
namespace Acme\DemoBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilder; | |
class IPType extends AbstractType | |
{ | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$ipTransformer = new IPTransformer(); | |
$builder->appendClientTransformer($ipTransformer) | |
; | |
} | |
public function getName() | |
{ | |
return 'ip'; | |
} | |
public function getParent(array $options) | |
{ | |
return 'text'; | |
} | |
} |
<?xml version="1.0" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<services> | |
<service id="acme_ip.type" class="Acme\DemoBundle\Form\IPType"> | |
<tag name="form.type" alias="ip" /> | |
</service> | |
</services> | |
</container> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment