-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pavel.Batanov
committed
Jul 12, 2018
1 parent
1c1e4e2
commit 6d8c361
Showing
25 changed files
with
891 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
vendor/ | ||
build/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
$finder = PhpCsFixer\Finder::create() | ||
->exclude('vendor') | ||
->exclude('build') | ||
->in(__DIR__); | ||
|
||
return PhpCsFixer\Config::create() | ||
->setRules([ | ||
'@Symfony' => true, | ||
'concat_space' => ['spacing' => 'one'], | ||
'phpdoc_align' => false, | ||
'phpdoc_to_comment' => false, | ||
'header_comment' => false, | ||
]) | ||
->setFinder($finder); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
checks: | ||
php: | ||
code_rating: true | ||
duplication: true | ||
|
||
build: | ||
nodes: | ||
analysis: | ||
tests: | ||
override: | ||
- php-scrutinizer-run | ||
tests: | ||
override: | ||
- | ||
command: vendor/bin/phpunit --coverage-clover=build/clover.xml | ||
coverage: | ||
file: build/clover.xml | ||
format: clover | ||
|
||
filter: | ||
excluded_paths: | ||
- "./tests" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
language: php | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
- nightly | ||
|
||
sudo: false | ||
|
||
matrix: | ||
fast_finish: true | ||
allow_failures: | ||
- php: nightly | ||
include: | ||
- php: 7.1 | ||
env: PACKAGES='symfony/symfony=3.4.*' | ||
- php: 7.1 | ||
env: PACKAGES='symfony/symfony=4.0.*' | ||
- php: 7.1 | ||
env: PACKAGES='symfony/symfony=4.1.*' | ||
|
||
before_install: | ||
- travis_retry composer self-update | ||
|
||
install: | ||
- composer require --no-update ${PACKAGES} | ||
- composer --prefer-source install |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,45 @@ | ||
# php-enum-bundle | ||
# Lamoda PHP Enum bundle | ||
Utility wrapper for https://github.com/paillechat/php-enum | ||
|
||
## Installation | ||
|
||
Usage is as simple as | ||
|
||
```bash | ||
composer require lamoda/enum-bundle:^1.0 | ||
``` | ||
|
||
```php | ||
// Kernel | ||
|
||
public function registerBundles() | ||
{ | ||
// ... | ||
$bundles[] = new \Lamoda\EnumBundle\LamodaEnumBundle(); | ||
// ... | ||
} | ||
``` | ||
|
||
```yaml | ||
# config.yml | ||
lamoda_enum: | ||
dbal_types: | ||
# short example | ||
my_domain_enum: My\Domain\Enum | ||
# full example | ||
my_domain_enum_2: | ||
class: My\Domain\Enum | ||
# identical strategy saves enum as its name as is, no conversion | ||
# lowercase strategy converts enum name to lowercase and vice versa on fetch | ||
strategy: identical | ||
``` | ||
```php | ||
class MyEntity | ||
{ | ||
/** @ORM\Column(type="my_domain_enum") */ | ||
private $value; | ||
} | ||
``` | ||
|
||
This will enable conversion of value field from your enum |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "lamoda/enum-bundle", | ||
"type": "symfony-bundle", | ||
"description": "Utility wrapper around PHP-enum", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Pavel Batanov", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.1", | ||
"paillechat/php-enum": "^1.2 || ^2.1", | ||
"symfony/http-kernel": "^3.4 || ^4.0", | ||
"symfony/dependency-injection": "^3.4 || ^4.0", | ||
"symfony/config": "^3.4 || ^4.0", | ||
"doctrine/dbal": "^2.4" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.0", | ||
"symfony/yaml": "^4.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Lamoda\\EnumBundle\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Lamoda\\EnumBundle\\Tests\\": "tests/" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit bootstrap="./vendor/autoload.php"> | ||
|
||
<php> | ||
<ini name="error_reporting" value="-1" /> | ||
</php> | ||
<testsuites> | ||
<testsuite name="unit tests"> | ||
<directory>./tests/Unit</directory> | ||
</testsuite> | ||
<testsuite name="functional tests"> | ||
<directory>./tests/Functional</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./src/</directory> | ||
<exclude> | ||
<directory>./build/</directory> | ||
<directory>./vendor/</directory> | ||
<directory>./tests/</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Lamoda\EnumBundle\DBAL; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\Type; | ||
use Lamoda\EnumBundle\Naming\IdenticalNamingStrategy; | ||
use Lamoda\EnumBundle\Naming\NamingStrategyInterface; | ||
use Paillechat\Enum\Enum; | ||
use Paillechat\Enum\Exception\EnumException; | ||
|
||
final class EnumType extends Type | ||
{ | ||
/** @var string */ | ||
private $fqcn; | ||
/** @var string */ | ||
private $name; | ||
/** @var NamingStrategyInterface */ | ||
private $strategy; | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function setFqcn(string $fqcn): void | ||
{ | ||
$this->fqcn = $fqcn; | ||
} | ||
|
||
public function setStrategy(NamingStrategyInterface $strategy): void | ||
{ | ||
$this->strategy = $strategy; | ||
} | ||
|
||
private function getStrategy(): NamingStrategyInterface | ||
{ | ||
if (!$this->strategy) { | ||
$this->strategy = new IdenticalNamingStrategy(); | ||
} | ||
|
||
return $this->strategy; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform) | ||
{ | ||
/** @var Enum $fqcn */ | ||
$fqcn = $this->fqcn; | ||
|
||
try { | ||
return $fqcn::createByName($this->getStrategy()->toEnumName($value)); | ||
} catch (EnumException $e) { | ||
throw ConversionException::conversionFailed($value, $this->getName()); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform) | ||
{ | ||
if (!is_a($value, $this->fqcn)) { | ||
throw ConversionException::conversionFailed($value, $this->getName()); | ||
} | ||
|
||
return $this->getStrategy()->fromEnumName((string) $value); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) | ||
{ | ||
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Lamoda\EnumBundle\DBAL; | ||
|
||
use Doctrine\DBAL\Types\Type; | ||
use Lamoda\EnumBundle\Naming\NamingStrategyInterface; | ||
|
||
final class EnumTypeInitializer | ||
{ | ||
/** | ||
* @param string $type | ||
* @param string $fqcn | ||
* @param NamingStrategyInterface|null $strategy | ||
* | ||
* @throws \Doctrine\DBAL\DBALException | ||
*/ | ||
public function initialize(string $type, string $fqcn, NamingStrategyInterface $strategy = null): void | ||
{ | ||
if (Type::hasType($type)) { | ||
return; | ||
} | ||
|
||
Type::addType($type, EnumType::class); | ||
/** @var EnumType $typeInstance */ | ||
$typeInstance = Type::getType($type); | ||
$typeInstance->setFqcn($fqcn); | ||
$typeInstance->setName($type); | ||
if ($strategy) { | ||
$typeInstance->setStrategy($strategy); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Lamoda\EnumBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
final class Configuration implements ConfigurationInterface | ||
{ | ||
/** {@inheritdoc} */ | ||
public function getConfigTreeBuilder(): TreeBuilder | ||
{ | ||
$builder = new TreeBuilder(); | ||
|
||
$root = $builder->root('lamoda_enum'); | ||
|
||
$this->configureEnumNodes($root); | ||
|
||
return $builder; | ||
} | ||
|
||
private function configureEnumNodes(ArrayNodeDefinition $parent): void | ||
{ | ||
$types = $parent->children()->arrayNode('dbal_types'); | ||
|
||
$dbalTypeProto = $types->prototype('array'); | ||
$dbalTypeProto->addDefaultsIfNotSet(); | ||
$dbalTypeProto | ||
->beforeNormalization() | ||
->ifString() | ||
->then( | ||
function (string $v) { | ||
return ['class' => $v]; | ||
} | ||
) | ||
->end(); | ||
$dbalTypeProto | ||
->children()->scalarNode('class') | ||
->isRequired() | ||
->example('My\Enum'); | ||
$dbalTypeProto | ||
->children()->enumNode('strategy') | ||
->values( | ||
[ | ||
'lowercase', | ||
'identical' | ||
] | ||
) | ||
->defaultValue('identical') | ||
; | ||
} | ||
} |
Oops, something went wrong.