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
Showing
5 changed files
with
210 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
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,104 @@ | ||
<?php | ||
|
||
namespace AlterPHP\EasyAdminExtensionBundle\Configuration; | ||
|
||
use AlterPHP\EasyAdminExtensionBundle\Exception\ConflictingConfigurationException; | ||
use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigPassInterface; | ||
use ReflectionClass; | ||
use ReflectionProperty; | ||
|
||
/** | ||
* Adds "exclude_fields" option to forms | ||
* | ||
* ```yaml | ||
* form: | ||
* fields: ['name', 'perex', 'description', 'duration', 'capacity'] | ||
* ↓ | ||
* exclude_fields: ['trainingReferences'] | ||
* ``` | ||
*/ | ||
class ExcludeFieldsConfigPass implements ConfigPassInterface | ||
{ | ||
/** | ||
* @param mixed[] $backendConfig | ||
* | ||
* @return mixed[] | ||
*/ | ||
public function process(array $backendConfig): array | ||
{ | ||
if (!isset($backendConfig['entities'])) { | ||
return $backendConfig; | ||
} | ||
|
||
foreach ($backendConfig['entities'] as $entityName => $entityConfig) { | ||
if (!isset($entityConfig['form']['exclude_fields'])) { | ||
continue; | ||
} | ||
|
||
$this->ensureFieldConfigurationIsValid($entityConfig, $entityName); | ||
|
||
$propertyNames = $this->getPropertyNamesForEntity($entityConfig, $entityName); | ||
|
||
// filter fields to be displayed | ||
$fields = []; | ||
foreach ($propertyNames as $propertyName) { | ||
if ($this->shouldSkipField($propertyName, $entityConfig['form']['exclude_fields'])) { | ||
continue; | ||
} | ||
|
||
$fields[] = $propertyName; | ||
} | ||
|
||
// set it! | ||
$backendConfig['entities'][$entityName]['form']['fields'] = $fields; | ||
} | ||
|
||
return $backendConfig; | ||
} | ||
|
||
/** | ||
* @param string[] $excludedFields | ||
*/ | ||
private function shouldSkipField(string $propertyName, array $excludedFields): bool | ||
{ | ||
if ('id' === $propertyName) { | ||
return true; | ||
} | ||
|
||
return in_array($propertyName, $excludedFields, true); | ||
} | ||
|
||
/** | ||
* Explicit "fields" option and "exclude_fields" won't work together | ||
* | ||
* @param mixed[] $entityConfig | ||
*/ | ||
private function ensureFieldConfigurationIsValid(array $entityConfig, string $entityName) | ||
{ | ||
if (!isset($entityConfig['form']['fields']) || !count($entityConfig['form']['fields'])) { | ||
return; | ||
} | ||
|
||
throw new ConflictingConfigurationException(sprintf( | ||
'"%s" and "%s" are mutually conflicting. Pick just one of them in %s YAML configuration', | ||
'exclude_fields', | ||
'fields', | ||
sprintf('easy_admin_bundle > entities > %s > form', $entityName) | ||
)); | ||
} | ||
|
||
/** | ||
* @param mixed[] $entityConfig | ||
* | ||
* @return string[] | ||
*/ | ||
private function getPropertyNamesForEntity(array $entityConfig, string $entityName): array | ||
{ | ||
$entityClass = $entityConfig['class'] ?: $entityName; | ||
$entityReflectionClass = new ReflectionClass($entityClass); | ||
|
||
return array_map(function (ReflectionProperty $reflectionProperty) { | ||
return $reflectionProperty->getName(); | ||
}, $entityReflectionClass->getProperties()); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlterPHP\EasyAdminExtensionBundle\Exception; | ||
|
||
use Exception; | ||
|
||
final class ConflictingConfigurationException extends Exception | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Configuration/ExcludeFieldsConfigPassSource/DummyEntity.php
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,10 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace AlterPHP\EasyAdminExtensionBundle\Tests\Configuration\ExcludeFieldsConfigPassSource; | ||
|
||
final class DummyEntity | ||
{ | ||
public $name; | ||
|
||
public $exclude; | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace AlterPHP\EasyAdminExtensionBundle\Tests\Configuration; | ||
|
||
use AlterPHP\EasyAdminExtensionBundle\Configuration\ExcludeFieldsConfigPass; | ||
use AlterPHP\EasyAdminExtensionBundle\Tests\Configuration\ExcludeFieldsConfigPassSource\DummyEntity; | ||
|
||
class ExcludeFieldsConfigPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ExcludeFieldsConfigPass | ||
*/ | ||
private $excludeFieldsConfigPass; | ||
|
||
protected function setUp() | ||
{ | ||
$this->excludeFieldsConfigPass = new ExcludeFieldsConfigPass(); | ||
} | ||
|
||
public function test() | ||
{ | ||
$backendConfig = array( | ||
'entities' => array( | ||
'TestEntity' => array( | ||
'class' => DummyEntity::class, | ||
'form' => array( | ||
'exclude_fields' => array('exclude') | ||
), | ||
), | ||
), | ||
); | ||
|
||
$processedBackendConfig = $this->excludeFieldsConfigPass->process($backendConfig); | ||
|
||
$expectedBackendConfig = array( | ||
'entities' => array( | ||
'TestEntity' => array( | ||
'class' => DummyEntity::class, | ||
'form' => array( | ||
'exclude_fields' => array('exclude'), | ||
'fields' => array('name') | ||
), | ||
), | ||
), | ||
); | ||
|
||
$this->assertSame($processedBackendConfig, $expectedBackendConfig); | ||
} | ||
} |