Expose match rules as api resources
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\ApiResource\Matchfunding;
|
||||
|
||||
use ApiPlatform\Metadata as API;
|
||||
use App\State\Matchfunding\RuleStateProvider;
|
||||
|
||||
/**
|
||||
* A MatchRule is a predefined code implementation for validating
|
||||
* that a match making condition is met when there is a Charge to be matched.
|
||||
*/
|
||||
#[API\ApiResource(
|
||||
shortName: 'MatchRule',
|
||||
provider: RuleStateProvider::class
|
||||
)]
|
||||
#[API\GetCollection()]
|
||||
#[API\Get()]
|
||||
class RuleApiResource
|
||||
{
|
||||
#[API\ApiProperty(identifier: true, writable: false)]
|
||||
public string $name;
|
||||
|
||||
/**
|
||||
* A plain-text description about what the rules validates for.
|
||||
*/
|
||||
#[API\ApiProperty(writable: false)]
|
||||
public string $description;
|
||||
}
|
||||
@@ -7,6 +7,11 @@ use App\Entity\Project\Project;
|
||||
|
||||
interface RuleInterface
|
||||
{
|
||||
/**
|
||||
* A plain-text description about what the rules validates for.
|
||||
*/
|
||||
public static function getDescription(): string;
|
||||
|
||||
/**
|
||||
* Do necessary checks to determine if Charge and Project comply with this rule.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\State\Matchfunding;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\Matchfunding\RuleApiResource;
|
||||
use App\Matchfunding\Rule\RuleInterface;
|
||||
use App\Matchfunding\Rule\RuleLocator;
|
||||
use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
|
||||
|
||||
class RuleStateProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private RuleLocator $ruleLocator,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||
{
|
||||
if (\array_key_exists('name', $uriVariables)) {
|
||||
try {
|
||||
$rule = $this->ruleLocator->getOne(\sprintf(
|
||||
'App\\Matchfunding\\Rule\\%s',
|
||||
$uriVariables['name']
|
||||
));
|
||||
|
||||
return $this->getApiResource($rule);
|
||||
} catch (ClassNotFoundException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$strategies = $this->ruleLocator->getAll();
|
||||
|
||||
$resources = [];
|
||||
foreach ($strategies as $rule) {
|
||||
$resources[] = $this->getApiResource($rule);
|
||||
}
|
||||
|
||||
return $resources;
|
||||
}
|
||||
|
||||
private function getApiResource(RuleInterface $rule): RuleApiResource
|
||||
{
|
||||
$resource = new RuleApiResource();
|
||||
$resource->name = self::getName($rule::class);
|
||||
$resource->description = $rule::getDescription();
|
||||
|
||||
return $resource;
|
||||
}
|
||||
|
||||
private static function getName(string $class): string
|
||||
{
|
||||
return \array_slice(\explode('\\', $class), -1, 1)[0];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user