Skip to content

Commit

Permalink
Max cutoff level configurable per route.
Browse files Browse the repository at this point in the history
  • Loading branch information
daedeloth committed May 3, 2022
1 parent ed0571c commit 0de2cfd
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 16 deletions.
17 changes: 12 additions & 5 deletions src/Collections/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RouteCollection extends RouteProperties implements \ArrayAccess
const OPTIONS_PARENT_IDENTIFIER_NAME = 'parentId';
const OPTIONS_IDENTIFIER_TRANSFORMER = 'identifier_transformer';
const OPTIONS_ONLY_INCLUDE_METHODS = 'only';
const OPTIONS_MAX_EXPAND_DEPTH = 'maxExpandDepth';

// 'index', 'view', 'store', 'edit', 'destroy'
const OPTIONS_METHOD_INDEX = 'index';
Expand Down Expand Up @@ -210,6 +211,7 @@ public function resource($resourceDefinition, $path, $controller, $options)
];

$id = $options[self::OPTIONS_IDENTIFIER_NAME] ?? 'id';
$maxExpandDepth = $options[self::OPTIONS_MAX_EXPAND_DEPTH] ?? 2;

$group = $this->group([]);

Expand All @@ -219,7 +221,8 @@ public function resource($resourceDefinition, $path, $controller, $options)
$entityName = $resourceDefinitionFactory->getDefault()->getEntityName(true);
return 'Returns all ' . $entityName;
})
->returns()->statusCode(200)->many($resourceDefinitionFactory->getDefault());
->returns()->statusCode(200)->many($resourceDefinitionFactory->getDefault())
->maxExpandDepth($maxExpandDepth);
}

if (in_array(self::OPTIONS_METHOD_VIEW, $only)) {
Expand All @@ -229,7 +232,8 @@ public function resource($resourceDefinition, $path, $controller, $options)

return 'View a single ' . $entityName;
})
->returns()->statusCode(200)->one($resourceDefinitionFactory->getDefault());
->returns()->statusCode(200)->one($resourceDefinitionFactory->getDefault())
->maxExpandDepth($maxExpandDepth);

$this->addIdParameterToRoutePath($viewRoute, $id, $options);
}
Expand All @@ -242,7 +246,8 @@ public function resource($resourceDefinition, $path, $controller, $options)
return 'Create a new ' . $entityName;
})
->parameters()->resource($resourceDefinitionFactory->getDefault())->required()
->returns()->statusCode(200)->one($resourceDefinitionFactory->getDefault());
->returns()->statusCode(200)->one($resourceDefinitionFactory->getDefault())
->maxExpandDepth($maxExpandDepth);
}

if (in_array(self::OPTIONS_METHOD_EDIT, $only)) {
Expand All @@ -253,7 +258,8 @@ public function resource($resourceDefinition, $path, $controller, $options)
return 'Update an existing ' . $entityName;
})
->parameters()->resource($resourceDefinitionFactory->getDefault())->required()
->returns()->statusCode(200)->one($resourceDefinitionFactory->getDefault());
->returns()->statusCode(200)->one($resourceDefinitionFactory->getDefault())
->maxExpandDepth($maxExpandDepth);

$this->addIdParameterToRoutePath($editRoute, $id, $options);
}
Expand All @@ -266,7 +272,8 @@ public function resource($resourceDefinition, $path, $controller, $options)
return 'Patch an existing ' . $entityName;
})
->parameters()->resource($resourceDefinitionFactory->getDefault())->required()
->returns()->statusCode(200)->one($resourceDefinitionFactory->getDefault());
->returns()->statusCode(200)->one($resourceDefinitionFactory->getDefault())
->maxExpandDepth($maxExpandDepth);

$this->addIdParameterToRoutePath($patchRoute, $id, $options);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Interfaces/RouteMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public function consumes(string $mimetype) : RouteMutator;
* @return mixed
*/
public function defaultOrder(string $order, $direction = OrderParameter::ASC) : RouteMutator;

/**
* @param int $maxDepth
* @return RouteMutator
*/
public function maxExpandDepth(int $maxDepth): RouteMutator;
}
9 changes: 9 additions & 0 deletions src/Models/Routing/Parameters/Base/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ public function defaultOrder(string $order, $direction = OrderParameter::ASC) :
return $this->route->defaultOrder($order, $direction);
}

/**
* @param int $maxDepth
* @return RouteMutator
*/
public function maxExpandDepth(int $maxDepth) : RouteMutator
{
return $this->route->maxExpandDepth($maxDepth);
}

/**
* Merge properties
* @param Parameter $parameter
Expand Down
9 changes: 9 additions & 0 deletions src/Models/Routing/ReturnValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,13 @@ public function getDescription()
{
return $this->description;
}

/**
* @param int $maxDepth
* @return RouteMutator
*/
public function maxExpandDepth(int $maxDepth) : RouteMutator
{
return $this->parent->maxExpandDepth($maxDepth);
}
}
35 changes: 24 additions & 11 deletions src/Models/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CatLab\Charon\Models\Routing;

use CatLab\Charon\Collections\RouteCollection;
use CatLab\Charon\Enums\Action;
use CatLab\Charon\Enums\Method;
use CatLab\Charon\Interfaces\ResourceTransformer;
use CatLab\Charon\Interfaces\RouteMutator;
Expand All @@ -18,8 +19,6 @@
*/
class Route extends RouteProperties implements RouteMutator
{
const MAX_EXPAND_DEPTH = 3;

/**
* @var string
*/
Expand Down Expand Up @@ -263,7 +262,7 @@ public function getExtraParameters($hasCardinalityMany)
* @param array $visibleValues
* @param array $expandValues
* @param string $prefix
* @param int $currentDepth
* @param array $path
* @return void
* @throws \CatLab\Charon\Exceptions\InvalidResourceDefinition
*/
Expand All @@ -272,15 +271,18 @@ protected function addExpandableValues(
string $context,
array &$visibleValues,
array &$expandValues,
$prefix = '',
$currentDepth = 0
string $prefix = '',
array $path = []
) {
$currentDepth ++;
if ($currentDepth > self::MAX_EXPAND_DEPTH) {
if (count($path) >= $this->getMaxExpandDepth()) {
return;
}

if (!$field->isViewable($context) || !$field->isExpandable()) {
if (
!$field->isViewable($context) ||
!$field->isExpandable() ||
$field->getExpandAction() === Action::IDENTIFIER
) {
return;
}

Expand All @@ -291,18 +293,29 @@ protected function addExpandableValues(
$related = $field->getChildResource();

foreach ($related->getFields() as $relatedField) {
if ($relatedField->isVisible()) {
$visibleValues[] = $prefix . $field->getDisplayName() . '.' . $relatedField->getDisplayName();
/** @var Field $relatedField */
$alreadySelected = in_array($field->getDisplayName(), $path);
if ($alreadySelected) {
continue;
}

// @todo do something differently here
if (!$relatedField->isViewable($field->getExpandAction())) {
continue;
}

$visibleValues[] = $prefix . $field->getDisplayName() . '.' . $relatedField->getDisplayName();

if ($relatedField instanceof RelationshipField) {
$newPath = array_merge($path, [ $field->getDisplayName() ]);

$this->addExpandableValues(
$relatedField,
$context,
$visibleValues,
$expandValues,
$prefix . $field->getDisplayName() . '.',
$currentDepth + 1
$newPath
);
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/Models/Routing/RouteProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ abstract class RouteProperties implements RouteMutator
*/
private $defaultOrder;


/**
* @var int
*/
private $maxExpandDepth = 2;

/**
* RouteCollection constructor.
* @param array $options
Expand Down Expand Up @@ -250,6 +256,24 @@ public function getDefaultOrder()
return $this->defaultOrder;
}

/**
* @param int $maxExpandDepth
* @return $this
*/
public function maxExpandDepth(int $maxExpandDepth): RouteMutator
{
$this->maxExpandDepth = $maxExpandDepth;
return $this;
}

/**
* @return int
*/
public function getMaxExpandDepth()
{
return $this->maxExpandDepth;
}

/**
* @internal
* @param string
Expand Down

0 comments on commit 0de2cfd

Please sign in to comment.