Skip to content

Commit

Permalink
Merge pull request #18 from fre5h/3.0
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
fre5h committed Nov 23, 2014
2 parents fac5960 + 4ae5005 commit 67cd890
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 150 deletions.
19 changes: 11 additions & 8 deletions DBAL/Types/AbstractEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\DBAL\Types;
namespace Fresh\DoctrineEnumBundle\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
Expand All @@ -30,7 +30,7 @@ abstract class AbstractEnumType extends Type
protected $name = '';

/**
* @var array Array of ENUM Values, where enum values are keys and their readable versions are values
* @var array Array of ENUM Values, where ENUM values are keys and their readable versions are values
* @static
*/
protected static $choices = [];
Expand All @@ -56,12 +56,15 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
*/
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$values = implode(', ', array_map(
function ($value) {
return "'{$value}'";
},
$this->getValues()
));
$values = implode(
', ',
array_map(
function ($value) {
return "'{$value}'";
},
$this->getValues()
)
);

if ($platform instanceof SqlitePlatform) {
return sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
Expand Down
18 changes: 4 additions & 14 deletions DependencyInjection/FreshDoctrineEnumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\DependencyInjection;
namespace Fresh\DoctrineEnumBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand All @@ -30,17 +30,7 @@ class FreshDoctrineEnumExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
}

/**
* Get alias
*
* @return string
*/
public function getAlias()
{
return 'fresh_doctrine_enum';
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
}
2 changes: 1 addition & 1 deletion Exception/EnumTypeIsNotRegisteredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\Exception;
namespace Fresh\DoctrineEnumBundle\Exception;

/**
* EnumTypeIsNotRegisteredException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\Exception;
namespace Fresh\DoctrineEnumBundle\Exception;

/**
* EnumTypeIsRegisteredButClassDoesNotExistException
Expand Down
2 changes: 1 addition & 1 deletion Exception/NoRegisteredEnumTypesException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\Exception;
namespace Fresh\DoctrineEnumBundle\Exception;

/**
* NoRegisteredEnumTypesException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\Exception;
namespace Fresh\DoctrineEnumBundle\Exception;

/**
* ValueIsFoundInFewRegisteredEnumTypesException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\Exception;
namespace Fresh\DoctrineEnumBundle\Exception;

/**
* ValueIsNotFoundInAnyRegisteredEnumTypeException
Expand Down
4 changes: 2 additions & 2 deletions Fixtures/DBAL/Types/BasketballPositionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\Fixtures\DBAL\Types;
namespace Fresh\DoctrineEnumBundle\Fixtures\DBAL\Types;

use Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;

/**
* Basketball position type
Expand Down
4 changes: 2 additions & 2 deletions Fixtures/DBAL/Types/MapLocationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\Fixtures\DBAL\Types;
namespace Fresh\DoctrineEnumBundle\Fixtures\DBAL\Types;

use Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;

/**
* Map location type
Expand Down
8 changes: 4 additions & 4 deletions Form/EnumTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle\Form;
namespace Fresh\DoctrineEnumBundle\Form;

use Doctrine\Common\Persistence\ManagerRegistry;
use Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
use Fresh\Bundle\DoctrineEnumBundle\Exception\EnumTypeIsRegisteredButClassDoesNotExistException;
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
use Fresh\DoctrineEnumBundle\Exception\EnumTypeIsRegisteredButClassDoesNotExistException;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function guessType($class, $property)

$enumTypeFullClassName = $this->registeredEnumTypes[$fieldType];

$abstractEnumTypeFullClassName = 'Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType';
$abstractEnumTypeFullClassName = 'Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType';

if (get_parent_class($enumTypeFullClassName) !== $abstractEnumTypeFullClassName) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion FreshDoctrineEnumBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/

namespace Fresh\Bundle\DoctrineEnumBundle;
namespace Fresh\DoctrineEnumBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand Down
52 changes: 21 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
# DoctrineEnumBundle

Provides support of **ENUM type** for Doctrine in Symfony applications
Provides support of **ENUM type** for Doctrine in Symfony applications.

[![License](https://poser.pugx.org/fresh/doctrine-enum-bundle/license.png)](https://packagist.org/packages/fresh/doctrine-enum-bundle)
[![Build Status](https://secure.travis-ci.org/fre5h/DoctrineEnumBundle.png?branch=master)](https://travis-ci.org/fre5h/DoctrineEnumBundle)
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/fre5h/DoctrineEnumBundle/badges/quality-score.png?s=be81f9b30a3996e7786cff5b4e0c0d972a64a37b)](https://scrutinizer-ci.com/g/fre5h/DoctrineEnumBundle/)
* Scrutinizer [![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/fre5h/DoctrineEnumBundle/badges/quality-score.png?s=be81f9b30a3996e7786cff5b4e0c0d972a64a37b)](https://scrutinizer-ci.com/g/fre5h/DoctrineEnumBundle/)
[![Build Status](https://scrutinizer-ci.com/g/fre5h/delivery-auto-api-php-client/badges/build.png?b=master)](https://scrutinizer-ci.com/g/fre5h/delivery-auto-api-php-client/build-status/master)
* Travis CI [![Build Status](https://secure.travis-ci.org/fre5h/DoctrineEnumBundle.png?branch=master)](https://travis-ci.org/fre5h/DoctrineEnumBundle)
* Packagist.org [![License](https://poser.pugx.org/fresh/doctrine-enum-bundle/license.png)](https://packagist.org/packages/fresh/doctrine-enum-bundle)
[![Latest Stable Version](https://poser.pugx.org/fresh/doctrine-enum-bundle/v/stable.png)](https://packagist.org/packages/fresh/doctrine-enum-bundle)
[![Total Downloads](https://poser.pugx.org/fresh/doctrine-enum-bundle/downloads.png)](https://packagist.org/packages/fresh/doctrine-enum-bundle)
[![Dependency Status](https://www.versioneye.com/php/fresh:doctrine-enum-bundle/badge.svg)](https://www.versioneye.com/php/fresh:doctrine-enum-bundle/5.3.x-dev)
* VersionEye [![Dependency Status](https://www.versioneye.com/user/projects/52ea10b1ec13755c2f000045/badge.svg)](https://www.versioneye.com/user/projects/52ea10b1ec13755c2f000045)
[![Reference Status](https://www.versioneye.com/php/fresh:doctrine-enum-bundle/reference_badge.svg)](https://www.versioneye.com/php/fresh:doctrine-enum-bundle/references)

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/0cff4816-374a-474e-a1d5-9d5db34562e3/big.png)](https://insight.sensiolabs.com/projects/0cff4816-374a-474e-a1d5-9d5db34562e3)

[![knpbundles.com](http://knpbundles.com/fre5h/DoctrineEnumBundle/badge-short)](http://knpbundles.com/fre5h/DoctrineEnumBundle)

## Supported platforms:
## Supported platforms

* MySQL
* SQLite

## Requirements

* Symfony 2.1 *and later*
* PHP 5.4 *and later*
* Symfony 2.5 *and later*
* Doctrine 2.2 *and later*

## Installation

### Install via Composer

Add the following lines into your `composer.json` file:

```json
{
"require": {
"fresh/doctrine-enum-bundle": "v2.5"
}
}
```
And then run `php composer.phar update`
```php composer.phar require fresh/doctrine-enum-bundle='v3.0'```

### Register the bundle

Expand All @@ -49,7 +41,7 @@ public function registerBundles()
{
$bundles = [
// Other bundles...
new Fresh\Bundle\DoctrineEnumBundle\FreshDoctrineEnumBundle(),
new Fresh\DoctrineEnumBundle\FreshDoctrineEnumBundle(),
];
}
```
Expand All @@ -62,7 +54,6 @@ Add the following lines for doctrine configuration in `config.yml` file:
# Doctrine Configuration
doctrine:
dbal:
# Other options...
mapping_types:
enum: string
```
Expand All @@ -83,10 +74,10 @@ Create class for new ENUM type `BasketballPositionType`:

```php
<?php
namespace Application\Bundle\DefaultBundle\DBAL\Types;
namespace Acme\AppBundle\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
/**
* Basketball position type
Expand Down Expand Up @@ -119,20 +110,19 @@ Register `BasketballPositionType` for Doctrine in config.yml:
# Doctrine Configuration
doctrine:
dbal:
# Other options...
types:
BasketballPositionType: Application\Bundle\DefaultBundle\DBAL\Types\BasketballPositionType
BasketballPositionType: Acme\AppBundle\DBAL\Types\BasketballPositionType
```

Create `Player` entity that has `position` field:

```php
<?php
namespace Application\Bundle\DefaultBundle\Entity;
namespace Acme\AppBundle\Entity;
use Application\Bundle\DefaultBundle\DBAL\Types\BasketballPositionType;
use Acme\AppBundle\DBAL\Types\BasketballPositionType;
use Doctrine\ORM\Mapping as ORM;
use Fresh\Bundle\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
/**
* Player Entity
Expand All @@ -143,7 +133,7 @@ use Fresh\Bundle\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
class Player
{
/**
* @var int $id
* @var int $id ID
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
Expand All @@ -152,15 +142,15 @@ class Player
protected $id;
/**
* @var string $position
* @var string $position Position
*
* @DoctrineAssert\Enum(entity="Application\Bundle\DefaultBundle\DBAL\Types\BasketballPositionType")
* @DoctrineAssert\Enum(entity="Acme\AppBundle\DBAL\Types\BasketballPositionType")
* @ORM\Column(name="position", type="BasketballPositionType", nullable=false)
*/
protected $position;
/**
* Get id
* Get ID
*
* @return int ID
*/
Expand Down Expand Up @@ -200,7 +190,7 @@ $player->setPosition(BasketballPositionType::POINT_GUARD);
But don't forget to define `BasketballPositionType` in the *use* section:

```php
use Application\Bundle\DefaultBundle\DBAL\Types\BasketballPositionType;
use Acme\AppBundle\DBAL\Types\BasketballPositionType;
```

`NULL` values are also supported by ENUM field.
Expand Down
19 changes: 0 additions & 19 deletions Resources/config/services.xml

This file was deleted.

16 changes: 16 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
parameters:
twig.extension.readable_enum_value.class: Fresh\DoctrineEnumBundle\Twig\Extension\ReadableEnumValueExtension
enum_type_guesser.class: Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser

services:
twig.extension.readable_enum_value:
class: %twig.extension.readable_enum_value.class%
arguments: [%doctrine.dbal.connection_factory.types%]
tags:
- { name: twig.extension }

enum_type_guesser:
class: %enum_type_guesser.class%
arguments: ["@doctrine", %doctrine.dbal.connection_factory.types%]
tags:
- { name: form.type_guesser }
Loading

0 comments on commit 67cd890

Please sign in to comment.