Skip to content

Commit

Permalink
Merge branch 'release/1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pfilsx committed Dec 24, 2019
2 parents b3d3cc9 + 3021df1 commit 0ebe1e8
Show file tree
Hide file tree
Showing 29 changed files with 1,186 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/README.md export-ignore
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/vendor
/composer.lock
###> phpunit/phpunit ###
/phpunit.xml
/build
/.phpunit.result.cache
/tests/app/cache
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php
php:
- 7.2

install:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev --no-interaction

script:
- mkdir -p build/logs
- php vendor/bin/phpunit -c phpunit.xml.dist

after_success:
- wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar --output-document="${HOME}/bin/coveralls"
- chmod u+x "${HOME}/bin/coveralls"
- coveralls -v
50 changes: 50 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "pfilsx/form-layer-bundle",
"description": "Provides additional functional to follow rule \"An entity should be always valid\" with forms validation for your Symfony project",
"type": "symfony-bundle",
"keywords": [
"form",
"symfony",
"symfony4",
"symfony5",
"bundle",
"entity",
"orm",
"doctrine"
],
"license": "MIT",
"authors": [
{
"name": "Pavel Filimonov",
"email": "pfilsx@gmail.com"
}
],
"minimum-stability": "stable",
"require": {
"php": ">=7.1",
"symfony/framework-bundle": "^3.4|^4.0|^5.0",
"symfony/dependency-injection": "^3.4|^4.0|^5.0",
"symfony/console": "^3.4|^4.0|^5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"phpunit/php-code-coverage": "^7.0",
"symfony/maker-bundle": "*",
"symfony/phpunit-bridge": "^5.0",
"symfony/process": "^3.4|^4.0|^5.0",
"symfony/validator": "^3.4|^4.0|^5.0",
"symfony/yaml": "^3.4|^4.0|^5.0",
"symfony/routing": "^3.4|^4.0|^5.0",
"symfony/orm-pack": "*"
},
"autoload": {
"psr-4": {
"Pfilsx\\FormLayer\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Pfilsx\\FormLayer\\Tests\\": "tests"
}
}
}
40 changes: 40 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1"/>
<env name="APP_ENV" value="test"/>
<env name="SHELL_VERBOSITY" value="-1"/>
<server name="KERNEL_DIR" value="./tests/app/"/>
<server name="KERNEL_CLASS" value="Pfilsx\FormLayer\Tests\app\AppKernel"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled" />
</php>

<testsuites>
<testsuite name="FormLayerBundle Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
<exclude>
<directory suffix="Interface.php">./src</directory>
<directory prefix="Abstract">./src</directory>
<directory>./src/DependencyInjection</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="build/report"/>
</logging>
</phpunit>
18 changes: 18 additions & 0 deletions src/DependencyInjection/FormLayerExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


namespace Pfilsx\FormLayer\DependencyInjection;

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

class FormLayerExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
}
}
11 changes: 11 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace Pfilsx\FormLayer\Exception;


use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;

class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}
17 changes: 17 additions & 0 deletions src/FormLayerBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace Pfilsx\FormLayer;

use Pfilsx\FormLayer\Layer\FormLayerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class FormLayerBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->registerForAutoconfiguration(FormLayerInterface::class)->addTag('form_layer.type');
}
}
108 changes: 108 additions & 0 deletions src/Layer/EntityFormLayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php


namespace Pfilsx\FormLayer\Layer;

use Pfilsx\FormLayer\Exception\InvalidArgumentException;

abstract class EntityFormLayer implements FormLayerInterface
{
protected $entity;

/**
* Associated entity FQN
* @return string
*/
abstract public static function getEntityClass(): string;

/**
* Returns entity identifier
* @return int|null
*/
public function getId(): ?int
{
if ($this->entity !== null) {
if (method_exists($this->entity, 'getId')) {
return $this->entity->getId();
}
if (property_exists($this->entity, 'id')) {
return $this->entity->id;
}
}
return null;
}

/**
* @inheritDoc
*/
public function load($entity)
{
if (!is_a($entity, static::getEntityClass())) {
throw new InvalidArgumentException('Expected instance of ' . static::getEntityClass() . ', got ' . get_class($entity));
}
$this->entity = $entity;
$this->loadLayerFields();
}

/**
* @inheritDoc
*/
public function create(bool $force = false)
{
if ($force || $this->entity === null) {
$className = static::getEntityClass();
$this->entity = new $className();
}
$this->update();
return $this->entity;
}

/**
* @inheritDoc
*/
public function update()
{
if ($this->entity !== null) {
$this->loadEntityFields();
}
}

/**
* Loads data from associated entity
*/
protected function loadLayerFields()
{
foreach (get_object_vars($this) as $prop => $val) {
$getter = 'get' . $prop;
$value = $val;
if (method_exists($this->entity, $getter)) {
$value = $this->entity->$getter();
} elseif (property_exists($this->entity, $prop)) {
$value = $this->entity->$prop;
}
$loadMethod = 'load' . $prop;
if (method_exists($this, $loadMethod)) {
$this->$loadMethod($value);
} else {
$this->$prop = $value;
}
}
}

/**
* Saves data into associated entity
*/
protected function loadEntityFields()
{
foreach (get_object_vars($this) as $prop => $value) {
$saveMethod = 'save' . $prop;
$value = method_exists($this, $saveMethod) ? $this->$saveMethod() : $this->$prop;
$setter = 'set' . $prop;
if (method_exists($this->entity, $setter)) {
$this->entity->$setter($value);
} elseif (property_exists($this->entity, $prop)) {
$this->entity->$prop = $value;
}
}
}
}
27 changes: 27 additions & 0 deletions src/Layer/FormLayerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace Pfilsx\FormLayer\Layer;


interface FormLayerInterface
{
/**
* Loads data from entity and create association
* @param object|mixed $entity
*/
public function load($entity);

/**
* Creates new entity object or updates associated if exists
* @param bool $force - forces creation of new entity object even if associated exists
* @return object|mixed
*/
public function create(bool $force = false);

/**
* Updates associated entity
* @return void
*/
public function update();
}

0 comments on commit 0ebe1e8

Please sign in to comment.