Created
July 10, 2012 21:07
-
-
Save l3l0/3086241 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace Acme\DemoBundle\Entity; | |
class IP | |
{ | |
/** | |
* @var integer $id | |
*/ | |
private $id; | |
/** | |
* @var integer $ip | |
*/ | |
private $ip; | |
/** | |
* @var string $high | |
*/ | |
private $high; | |
/** | |
* @var string $hoster | |
*/ | |
private $hoster; | |
/** | |
* @var date $scandate | |
*/ | |
private $scandate; | |
... |
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
<?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')); | |
} | |
} |
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
<?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'; | |
} | |
} |
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
<?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); | |
} | |
} |
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
<?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'; | |
} | |
} |
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
<?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