Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
janatjak committed Dec 1, 2023
1 parent 1599cf7 commit 399c750
Show file tree
Hide file tree
Showing 39 changed files with 477 additions and 716 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Expand Up @@ -12,14 +12,14 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.3
coverage: pcov

- name: Composer install
run: composer install --dev --no-progress

- name: Check coding standard
run: vendor/bin/phpcs --standard=PSR12 ./src ./tests
run: vendor/bin/ecs

- name: Check phpstan
run: vendor/bin/phpstan
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Expand Up @@ -7,4 +7,7 @@ phpstan:
php vendor/bin/phpstan

cs:
php vendor/bin/phpcs --standard=PSR12 ./src ./tests
php vendor/bin/ecs

cs-fix:
php vendor/bin/ecs --fix
140 changes: 70 additions & 70 deletions README.md
Expand Up @@ -34,80 +34,80 @@ Documentation
### Usage

```php
use FreezyBee\DoctrineFormMapper\DoctrineFormMapper;
use FreezyBee\DoctrineFormMapper\IComponentMapper;
use FreezyBee\DoctrineFormMapper\DoctrineFormMapper;
use FreezyBee\DoctrineFormMapper\IComponentMapper;

class XPresenter extends Presenter
{
/** @var DoctrineFormMapper @inject */
public $mapper;

/** @var EntityRepository @inject */
public $articlesRepository;

class XPresenter extends Presenter
protected function createComponentForm()
{
/** @var DoctrineFormMapper @inject */
public $mapper;

/** @var EntityRepository @inject */
public $articlesRepository;
$form = new Form;

// Column
$form->addText('name');

// ManyToOne
$form->addSelect('author')
// order items
->setOption(IComponentMapper::ITEMS_ORDER, ['age' => 'ASC'])
// filter items
->setOption(IComponentMapper::ITEMS_FILTER, ['age' => 0])
// filter items by callback
->setOption(IComponentMapper::ITEMS_FILTER, function(QueryBuilder $qb) {
$qb->andWhere('entity.age != 0')
})
// custom select label renderer
->setOption(IComponentMapper::ITEMS_TITLE, function (Author $author) {
return $author->getName() . ' - ' . $author->getAge();
});

// ManyToOne
$form->addRadioList('tags')
->setOption(IComponentMapper::ITEMS_TITLE, 'name');


// ManyToMany
$form->addMultiSelect('users')
->setOption(IComponentMapper::ITEMS_TITLE, 'username');

// ManyToMany
// btw you can define items and then ITEMS_TITLE is not required
$form->addCheckboxList('countries', 'Countries', [1 => 'CZ', 2 => 'SK']);


// A) create new entity
$article = new Article;

// B) load entity from db
$article = $this->articlesRepository->find(1);

protected function createComponentForm()
{
$form = new Form;

// Column
$form->addText('name');

// ManyToOne
$form->addSelect('author')
// order items
->setOption(IComponentMapper::ITEMS_ORDER, ['age' => 'ASC'])
// filter items
->setOption(IComponentMapper::ITEMS_FILTER, ['age' => 0])
// filter items by callback
->setOption(IComponentMapper::ITEMS_FILTER, function(QueryBuilder $qb) {
$qb->andWhere('entity.age != 0')
})
// custom select label renderer
->setOption(IComponentMapper::ITEMS_TITLE, function (Author $author) {
return $author->getName() . ' - ' . $author->getAge();
});

// ManyToOne
$form->addRadioList('tags')
->setOption(IComponentMapper::ITEMS_TITLE, 'name');


// ManyToMany
$form->addMultiSelect('users')
->setOption(IComponentMapper::ITEMS_TITLE, 'username');

// ManyToMany
// btw you can define items and then ITEMS_TITLE is not required
$form->addCheckboxList('countries', 'Countries', [1 => 'CZ', 2 => 'SK']);


// A) create new entity
$article = new Article;

// B) load entity from db
$article = $this->articlesRepository->find(1);
// C) create new entity by class name - see point INFO below
$article = Article::class;

// load data from entity to form
$this->mapper->load($article, $form);

$form->onSuccess[] = function (Form $form) use ($article) {

// C) create new entity by class name - see point INFO below
$article = Article::class;
// save (map) data from form to entity - without flush!!!
$articleEntity = $this->mapper->save($article, $form);

// load data from entity to form
$this->mapper->load($article, $form);

$form->onSuccess[] = function (Form $form) use ($article) {

// save (map) data from form to entity - without flush!!!
$articleEntity = $this->mapper->save($article, $form);

// INFO - if article was classname, mapper create new instance
// $articleEntity is instanceof Article

// flush data...
$em = $this->mapper->getEntityManager();
$em->persist($articleEntity)
$em->flush();
};

return $form;
}
// INFO - if article was classname, mapper create new instance
// $articleEntity is instanceof Article

// flush data...
$em = $this->mapper->getEntityManager();
$em->persist($articleEntity)
$em->flush();
};

return $form;
}
}
```
7 changes: 3 additions & 4 deletions composer.json
Expand Up @@ -22,17 +22,16 @@
"nette/di": "~2.4 || ^3.0",
"nette/forms": "~2.4 || ^3.0",
"doctrine/orm": "~2.5",
"symfony/property-access": "~3.0 || ^4.0 || ^5.0 || ^6.0"
"symfony/property-access": "~3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"require-dev": {
"doctrine/annotations": "^1.0",
"latte/latte": "~2.5 || ^3.0",
"nette/application": "~2.4 || ^3.0",
"nette/bootstrap": "~2.4 || ^3.0",
"nette/tester": "~2.0",
"phpstan/phpstan": "^1.0",
"ramsey/uuid-doctrine": "^1.5",
"squizlabs/php_codesniffer": "^3.0",
"ramsey/uuid-doctrine": "^2.0",
"symplify/easy-coding-standard": "^12.0",
"tracy/tracy": "^2.5"
},
"autoload": {
Expand Down
27 changes: 27 additions & 0 deletions ecs.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);
$ecsConfig->fileExtensions(['php', 'phpt']);

$ecsConfig->sets([
// run and fix, one by one
SetList::PSR_12,
SetList::ARRAY,
SetList::CLEAN_CODE,
SetList::STRICT,
SetList::NAMESPACES,
SetList::DOCBLOCK,
SetList::SPACES,
]);

$ecsConfig->skip([
NotOperatorWithSuccessorSpaceFixer::class,
]);
};
7 changes: 5 additions & 2 deletions phpstan.neon
@@ -1,12 +1,15 @@
parameters:
level: 8
fileExtensions:
- php
- phpt
paths:
- src
- tests
ignoreErrors:
- '#Method class@anonymous/tests/Mock/EntityManagerTrait.php:42::getRepository\(\) return type with generic class Doctrine\\ORM\\EntityRepository does not specify its types: T#'

checkUninitializedProperties: true
ignoreErrors:
- '#Trait FreezyBee\\DoctrineFormMapper\\Tests\\Mock\\EntityManagerTrait is used zero times and is not analysed.#'

includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
9 changes: 4 additions & 5 deletions src/DI/FormMapperExtension.php
Expand Up @@ -25,7 +25,9 @@
*/
class FormMapperExtension extends CompilerExtension
{
/** @var mixed[] */
/**
* @var mixed[]
*/
private array $defaults = [
'mappers' => [
Construct::class,
Expand All @@ -35,12 +37,9 @@ class FormMapperExtension extends CompilerExtension
ManyToOne::class,
ManyToMany::class,
],
'entityManager' => null
'entityManager' => null,
];

/**
*
*/
public function loadConfiguration(): void
{
$config = $this->validateConfig($this->defaults);
Expand Down
36 changes: 10 additions & 26 deletions src/DoctrineFormMapper.php
Expand Up @@ -16,6 +16,7 @@
use Nette\SmartObject;
use ReflectionClass;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

/**
* @author Jakub Janata <jakubjanata@gmail.com>
Expand All @@ -25,54 +26,38 @@ class DoctrineFormMapper
{
use SmartObject;

/** @var EntityManagerInterface */
protected $em;

/** @var IComponentMapper[] */
protected $componentMappers = [];

/** @var PropertyAccessor */
protected $accessor;
protected EntityManagerInterface $em;

/**
* @param EntityManagerInterface $entityManager
* @var IComponentMapper[]
*/
public function __construct(EntityManagerInterface $entityManager)
protected array $componentMappers = [];

protected PropertyAccessorInterface $accessor;

public function __construct(EntityManagerInterface $entityManager, ?PropertyAccessorInterface $accessor = null)
{
$this->em = $entityManager;
$this->accessor = $accessor ?? new PropertyAccessor();
}

/**
* @param IComponentMapper $componentMapper
*/
public function addMapper(IComponentMapper $componentMapper): void
{
$this->componentMappers[] = $componentMapper;
}

/**
* @return PropertyAccessor
*/
public function getAccessor(): PropertyAccessor
public function getAccessor(): PropertyAccessorInterface
{
if ($this->accessor === null) {
$this->accessor = new PropertyAccessor();
}

return $this->accessor;
}

/**
* @return EntityManagerInterface
*/
public function getEntityManager(): EntityManagerInterface
{
return $this->em;
}

/**
* @param mixed|string $entity
* @param Container $formElement
*/
public function load($entity, Container $formElement): void
{
Expand All @@ -94,7 +79,6 @@ public function load($entity, Container $formElement): void

/**
* @param mixed|string $entity
* @param Container $formElement
* @return mixed
*/
public function save($entity, Container $formElement)
Expand Down
2 changes: 2 additions & 0 deletions src/IComponentMapper.php
Expand Up @@ -21,7 +21,9 @@
interface IComponentMapper
{
public const ITEMS_TITLE = 'items.title';

public const ITEMS_FILTER = 'items.filter';

public const ITEMS_ORDER = 'items.order';

/**
Expand Down
14 changes: 2 additions & 12 deletions src/Mappers/Column.php
Expand Up @@ -18,7 +18,7 @@
use Nette\Forms\Controls\BaseControl;
use Nette\SmartObject;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

/**
* @author Jakub Janata <jakubjanata@gmail.com>
Expand All @@ -28,20 +28,13 @@ class Column implements IComponentMapper
{
use SmartObject;

/** @var PropertyAccessor */
private $accessor;
private PropertyAccessorInterface $accessor;

/**
* @param DoctrineFormMapper $mapper
*/
public function __construct(DoctrineFormMapper $mapper)
{
$this->accessor = $mapper->getAccessor();
}

/**
* {@inheritdoc}
*/
public function load(ClassMetadata $meta, IComponent $component, $entity): bool
{
if (!$component instanceof BaseControl) {
Expand All @@ -63,9 +56,6 @@ public function load(ClassMetadata $meta, IComponent $component, $entity): bool
return false;
}

/**
* {@inheritdoc}
*/
public function save(ClassMetadata $meta, IComponent $component, &$entity): bool
{
if (!$component instanceof BaseControl) {
Expand Down

0 comments on commit 399c750

Please sign in to comment.