Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Mar 29, 2021
1 parent c19ebe6 commit e57da56
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 90 deletions.
5 changes: 3 additions & 2 deletions src/Controller/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Flat3\Lodata\Drivers\ManualEntitySet;
use Flat3\Lodata\Entity;
use Flat3\Lodata\EntitySet;
use Flat3\Lodata\EntityType;
use Flat3\Lodata\Exception\Internal\PathNotHandledException;
use Flat3\Lodata\Exception\Protocol\AcceptedException;
use Flat3\Lodata\Exception\Protocol\BadRequestException;
Expand Down Expand Up @@ -212,11 +213,11 @@ class Transaction implements ArgumentInterface
* @var PipeInterface[] $handlers
*/
protected $handlers = [
Entity::class,
EntitySet::class,
EntityType::class,
PathSegment\Batch::class,
PathSegment\Metadata::class,
PathSegment\Entity::class,
PathSegment\Type::class,
PathSegment\Value::class,
PathSegment\Count::class,
PathSegment\Filter::class,
Expand Down
23 changes: 22 additions & 1 deletion src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayAccess;
use Flat3\Lodata\Controller\Response;
use Flat3\Lodata\Controller\Transaction;
use Flat3\Lodata\Exception\Internal\PathNotHandledException;
use Flat3\Lodata\Exception\Protocol\BadRequestException;
use Flat3\Lodata\Exception\Protocol\InternalServerErrorException;
use Flat3\Lodata\Exception\Protocol\MethodNotAllowedException;
Expand All @@ -30,6 +31,7 @@
use Flat3\Lodata\Transaction\MetadataContainer;
use Flat3\Lodata\Transaction\NavigationRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

/**
* Entity
Expand Down Expand Up @@ -389,7 +391,26 @@ public static function pipe(
?string $nextSegment,
?PipeInterface $argument
): ?PipeInterface {
return $argument;
if ($currentSegment !== '$entity') {
throw new PathNotHandledException();
}

if ($argument) {
throw new BadRequestException('entity_argument', 'Entity cannot have a preceding path segment');
}

$id = $transaction->getIdOption();

if (!$id->hasValue()) {
throw new BadRequestException('missing_id', 'The entity id system query option must be provided');
}

$entityId = $id->getValue();
if (Str::startsWith($entityId, ServiceProvider::endpoint())) {
$entityId = Str::substr($entityId, strlen(ServiceProvider::endpoint()));
}

return EntitySet::pipe($transaction, $entityId);
}

/**
Expand Down
30 changes: 29 additions & 1 deletion src/EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace Flat3\Lodata;

use Flat3\Lodata\Controller\Transaction;
use Flat3\Lodata\Exception\Internal\PathNotHandledException;
use Flat3\Lodata\Exception\Protocol\NotFoundException;
use Flat3\Lodata\Facades\Lodata;
use Flat3\Lodata\Helper\Identifier;
use Flat3\Lodata\Interfaces\PipeInterface;

/**
* Entity Type
* @link https://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/odata-csdl-xml-v4.01.html#_Toc38530349
* @package Flat3\Lodata
*/
class EntityType extends ComplexType
class EntityType extends ComplexType implements PipeInterface
{
/**
* Primary key property
Expand Down Expand Up @@ -57,4 +62,27 @@ public function setKey(DeclaredProperty $key): self

return $this;
}

public static function pipe(
Transaction $transaction,
string $currentSegment,
?string $nextSegment,
?PipeInterface $argument
): ?PipeInterface {
$entityType = Lodata::getEntityType($currentSegment);

if (!$entityType) {
throw new PathNotHandledException();
}

if (!$argument instanceof Entity) {
throw new PathNotHandledException();
}

if ($argument->getType()->getIdentifier() !== $entityType->getIdentifier()) {
throw new NotFoundException('invalid_entity_type', 'The provided type did not match the entity type');
}

return $argument;
}
}
46 changes: 0 additions & 46 deletions src/PathSegment/Entity.php

This file was deleted.

40 changes: 0 additions & 40 deletions src/PathSegment/Type.php

This file was deleted.

0 comments on commit e57da56

Please sign in to comment.