Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel.Batanov committed Jul 12, 2018
1 parent 1c1e4e2 commit 6d8c361
Show file tree
Hide file tree
Showing 25 changed files with 891 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor/
build/
16 changes: 16 additions & 0 deletions .php_cs
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);
22 changes: 22 additions & 0 deletions .scrutinizer.yml
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"
27 changes: 27 additions & 0 deletions .travis.yml
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
45 changes: 44 additions & 1 deletion README.md
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
35 changes: 35 additions & 0 deletions composer.json
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/"
}
}
}
27 changes: 27 additions & 0 deletions phpunit.xml
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>
84 changes: 84 additions & 0 deletions src/DBAL/EnumType.php
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;
}
}
32 changes: 32 additions & 0 deletions src/DBAL/EnumTypeInitializer.php
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);
}
}
}
53 changes: 53 additions & 0 deletions src/DependencyInjection/Configuration.php
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')
;
}
}
Loading

0 comments on commit 6d8c361

Please sign in to comment.