Skip to content

Commit

Permalink
List form filters for MongoODM
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Oct 22, 2018
1 parent a4b7768 commit 7a33466
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion TODO_mongo_odm.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- EmbeddedListViewConfigPass
- ExcludeFieldsConfigPass
- ListFormFiltersConfigPass
- ShortFormTypeConfigPass: ON PROGRESS
- ShortFormTypeConfigPass: OK (use PropertyPath to include [list][form_filters])
- ShowViewConfigPass

# POST LIST MONGO ODM QUERY BUILDER
Expand Down
43 changes: 27 additions & 16 deletions src/Configuration/ListFormFiltersConfigPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,34 @@ public function __construct(ManagerRegistry $doctrine)
*/
public function process(array $backendConfig): array
{
if (!isset($backendConfig['entities'])) {
return $backendConfig;
if (isset($backendConfig['entities']) && is_array($backendConfig['entities'])) {
$this->processObjectListFormFilters('entity', $backendConfig['entities']);
}

foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
if (!isset($entityConfig['list']['form_filters'])) {
if (isset($backendConfig['documents']) && is_array($backendConfig['documents'])) {
$this->processObjectListFormFilters('document', $backendConfig['documents']);
}

return $backendConfig;
}

private function processObjectListFormFilters(string $objectType, array &$objectConfigs)
{
foreach ($objectConfigs as $objectName => $objectConfig) {
if (!isset($objectConfig['list']['form_filters'])) {
continue;
}

$formFilters = array();

foreach ($entityConfig['list']['form_filters'] as $i => $formFilter) {
foreach ($objectConfig['list']['form_filters'] as $i => $formFilter) {
// Detects invalid config node
if (!is_string($formFilter) && !is_array($formFilter)) {
throw new \RuntimeException(
sprintf(
'The values of the "form_filters" option for the list view of the "%s" entity can only be strings or arrays.',
$entityConfig['class']
'The values of the "form_filters" option for the list view of the "%s" object of type "%s" can only be strings or arrays.',
$objectConfig['class'],
$objectType
)
);
}
Expand All @@ -57,16 +67,19 @@ public function process(array $backendConfig): array
if (!array_key_exists('property', $formFilter)) {
throw new \RuntimeException(
sprintf(
'One of the values of the "form_filters" option for the "list" view of the "%s" entity does not define the mandatory option "property".',
$entityConfig['class']
'One of the values of the "form_filters" option for the "list" view of the "%s" object of type "%s" does not define the mandatory option "property".',
$objectConfig['class'],
$objectType
)
);
}

$filterConfig = $formFilter;
}

$this->configureFilter($entityConfig['class'], $filterConfig);
if ('entity' === $objectType) {
$this->configureEntityFormFilter($objectConfig['class'], $filterConfig);
}

// If type is not configured at this steps => not guessable
if (!isset($filterConfig['type'])) {
Expand All @@ -77,13 +90,11 @@ public function process(array $backendConfig): array
}

// set form filters config and form !
$backendConfig['entities'][$entityName]['list']['form_filters'] = $formFilters;
$objectConfigs[$objectName]['list']['form_filters'] = $formFilters;
}

return $backendConfig;
}

private function configureFilter(string $entityClass, array &$filterConfig)
private function configureEntityFormFilter(string $entityClass, array &$filterConfig)
{
// No need to guess type
if (isset($filterConfig['type'])) {
Expand All @@ -102,7 +113,7 @@ private function configureFilter(string $entityClass, array &$filterConfig)
}

if ($entityMetadata->hasField($filterConfig['property'])) {
$this->configureFieldFilter(
$this->configureEntityPropertyFilter(
$entityClass, $entityMetadata->getFieldMapping($filterConfig['property']), $filterConfig
);
} elseif ($entityMetadata->hasAssociation($filterConfig['property'])) {
Expand All @@ -112,7 +123,7 @@ private function configureFilter(string $entityClass, array &$filterConfig)
}
}

private function configureFieldFilter(string $entityClass, array $fieldMapping, array &$filterConfig)
private function configureEntityPropertyFilter(string $entityClass, array $fieldMapping, array &$filterConfig)
{
switch ($fieldMapping['type']) {
case 'boolean':
Expand Down

0 comments on commit 7a33466

Please sign in to comment.