Skip to content

Commit

Permalink
Added more comments. Changed service defintion from yml to xml
Browse files Browse the repository at this point in the history
  • Loading branch information
tetranz committed Sep 7, 2014
1 parent d53623e commit 5990258
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 48 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ app/config/parameters.ini
app/config/parameters.yml

# Composer
composer.phar
composer.phar

#PhpStorm
.idea
5 changes: 3 additions & 2 deletions DependencyInjection/TetranzSelect2EntityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

// set parameters with these settings so they'll be available in the service definition xml
$varNames = ['minimum_input_length', 'page_limit', 'data_type'];

foreach($varNames as $varName) {
$container->setParameter("tetranz_select2_entity.$varName", $config[$varName]);
}

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}
24 changes: 18 additions & 6 deletions Form/DataTransformer/EntitiesToPropertyTransformer.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: ross
* Date: 7/19/14
* Time: 8:11 AM
*/

namespace Tetranz\Select2EntityBundle\Form\DataTransformer;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\DataTransformerInterface;

/**
* Data transformer for multiple mode (i.e., multiple = true)
*
* Class EntitiesToPropertyTransformer
* @package Tetranz\Select2EntityBundle\Form\DataTransformer
*/
class EntitiesToPropertyTransformer implements DataTransformerInterface
{
protected $em;
Expand All @@ -25,6 +25,12 @@ public function __construct(EntityManager $em, $class, $textProperty)
$this->textProperty = $textProperty;
}

/**
* Transform initial entities as json with id and text
*
* @param mixed $entities
* @return string
*/
public function transform($entities)
{
if (count($entities) == 0) {
Expand All @@ -49,6 +55,12 @@ public function transform($entities)
return htmlspecialchars(json_encode($data));
}

/**
* Transform csv list of ids to a collection of entities
*
* @param string $values as a CSV list
* @return array|ArrayCollection|mixed
*/
public function reverseTransform($values)
{
// remove the 'magic' non-blank value added in fields.html.twig
Expand Down
24 changes: 18 additions & 6 deletions Form/DataTransformer/EntityToPropertyTransformer.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
/**
* Created by PhpStorm.
* User: ross
* Date: 7/19/14
* Time: 8:11 AM
*/

namespace Tetranz\Select2EntityBundle\Form\DataTransformer;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\DataTransformerInterface;

/**
* Data transformer for single mode (i.e., multiple = false)
*
* Class EntityToPropertyTransformer
* @package Tetranz\Select2EntityBundle\Form\DataTransformer
*/
class EntityToPropertyTransformer implements DataTransformerInterface
{
protected $em;
Expand All @@ -24,6 +24,12 @@ public function __construct(EntityManager $em, $class, $textProperty)
$this->textProperty = $textProperty;
}

/**
* Transform entity to json with id and text
*
* @param mixed $entity
* @return string
*/
public function transform($entity)
{
if (null === $entity || '' === $entity) {
Expand All @@ -44,6 +50,12 @@ public function transform($entity)
return htmlspecialchars(json_encode($data));
}

/**
* Transform to single id value to an entity
*
* @param string $value
* @return mixed|null|object
*/
public function reverseTransform($value)
{
if (null === $value || '' === $value) {
Expand Down
14 changes: 8 additions & 6 deletions Form/Type/Select2EntityType.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: ross
* Date: 7/17/14
* Time: 9:40 PM
*/

namespace Tetranz\Select2EntityBundle\Form\Type;

Expand All @@ -19,6 +13,12 @@
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer;
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer;

/**
*
*
* Class Select2EntityType
* @package Tetranz\Select2EntityBundle\Form\Type
*/
class Select2EntityType extends AbstractType
{
protected $em;
Expand All @@ -38,6 +38,7 @@ public function __construct(EntityManager $em, Router $router, $minimumInputLeng

public function buildForm(FormBuilderInterface $builder, array $options)
{
// add the appropriate data transformer
$transformer = $options['multiple']
? new EntitiesToPropertyTransformer($this->em, $options['class'], $options['text_property'])
: new EntityToPropertyTransformer($this->em, $options['class'], $options['text_property']);
Expand All @@ -49,6 +50,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
{
parent::finishView($view, $form, $options);

// make variables available to the view
$view->vars['remote_path'] = $options['remote_path']
?: $this->router->generate($options['remote_route'], $options['remote_params']);

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 tetranz
Copyright (c) 2014 Ross Keatinge

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 18 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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="tetranz_select2entity.select2entity_type" class="Tetranz\Select2EntityBundle\Form\Type\Select2EntityType">
<tag name="form.type" alias="tetranz_select2entity" />
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="router" />
<argument>%tetranz_select2_entity.minimum_input_length%</argument>
<argument>%tetranz_select2_entity.page_limit%</argument>
<argument>%tetranz_select2_entity.data_type%</argument>
</service>
</services>

</container>
8 changes: 0 additions & 8 deletions Resources/config/services.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{# Add class for jQuery for find it #}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' select2entity')|trim }) %}

{# A bit of a kludge here.
{# A bit of a hack here.
It seems to work more smoothly if we put the initial value into data-value rather than the value attribute.
But ... value needs to be non-blank for initSelection in the js to fire.
We need to remove this value in EntitiesToPropertyTransformer #}
Expand Down
17 changes: 0 additions & 17 deletions Tests/Controller/DefaultControllerTest.php

This file was deleted.

0 comments on commit 5990258

Please sign in to comment.