Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Apr 6, 2021
1 parent 6b4d640 commit 3c186d4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/Controller/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public function setPath(string $path): self
return $this;
}

/**
* Set the request content
* @param mixed $content Request content
* @return $this
*/
public function setContent($content): self
{
$this->content = $content;

return $this;
}

/** @noinspection PhpMissingParentConstructorInspection */
public function __construct(IlluminateRequest $request)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public function getQueryParam(string $key): ?string

/**
* Get the request object
* @return RequestInterface Request
* @return RequestInterface|Request Request
*/
public function getRequest(): RequestInterface
{
Expand Down
54 changes: 50 additions & 4 deletions src/Helper/PropertyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use Flat3\Lodata\EntitySet;
use Flat3\Lodata\Exception\Internal\LexerException;
use Flat3\Lodata\Exception\Internal\PathNotHandledException;
use Flat3\Lodata\Exception\Protocol\BadRequestException;
use Flat3\Lodata\Exception\Protocol\MethodNotAllowedException;
use Flat3\Lodata\Exception\Protocol\NoContentException;
use Flat3\Lodata\Exception\Protocol\NotFoundException;
use Flat3\Lodata\Expression\Lexer;
use Flat3\Lodata\GeneratedProperty;
use Flat3\Lodata\Interfaces\ContextInterface;
use Flat3\Lodata\Interfaces\EntitySet\UpdateInterface;
use Flat3\Lodata\Interfaces\JsonInterface;
use Flat3\Lodata\Interfaces\PipeInterface;
use Flat3\Lodata\Interfaces\ResourceInterface;
Expand All @@ -22,6 +25,7 @@
use Flat3\Lodata\Transaction\MetadataContainer;
use Flat3\Lodata\Transaction\NavigationRequest;
use Flat3\Lodata\Type\Stream;
use Illuminate\Http\Request;

/**
* Property Value
Expand Down Expand Up @@ -284,17 +288,59 @@ public function emitJson(Transaction $transaction): void

public function response(Transaction $transaction, ?ContextInterface $context = null): Response
{
$value = $this->value;

if ($value instanceof Entity || $value instanceof EntitySet) {
return $value->response($transaction, $this);
}

switch ($transaction->getMethod()) {
case Request::METHOD_GET:
return $this->get($transaction, $context);

case Request::METHOD_DELETE:
return $this->delete($transaction, $context);
}

throw new MethodNotAllowedException();
}

public function delete(Transaction $transaction, ?ContextInterface $context = null): Response
{
$entitySet = $this->entity->getEntitySet();

if (!$entitySet instanceof UpdateInterface) {
throw new BadRequestException(
'entity_set_not_updatable',
'The entity set for this entity does not support updates'
);
}

if (!$this->getProperty()->isNullable()) {
throw new BadRequestException('property_not_nullable', 'This property cannot be set to null');
}

Gate::check(Gate::DELETE, $this, $transaction);

$transaction->getRequest()->setContent([
$this->getProperty()->getName() => null,
]);
$entitySet->update($this->entity->getEntityId());

throw new NoContentException();
}

public function get(Transaction $transaction, ?ContextInterface $context = null): Response
{
Gate::check(Gate::READ, $this, $transaction);

$value = $this->value;
$context = $context ?: $this;

if ($value instanceof Primitive && null === $value->get() || $value === null) {
throw new NoContentException('null_value');
}

if ($value instanceof Entity || $value instanceof EntitySet) {
return $value->response($transaction, $this);
}

$metadata = $transaction->createMetadataContainer();

$metadata['context'] = $context->getContextUrl($transaction);
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/Queries/EntityPrimitive/EntityPrimitiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ public function test_read_an_entity_set_primitive()
);
}

public function test_delete_an_entity_set_primitive()
{
$this->assertNoContent(
Request::factory()
->delete()
->path('/flights(1)/origin')
);

$this->assertDatabaseSnapshot();
}

public function test_null_no_content()
{
$flight = (new Flight([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
airports:
- { id: '1', name: Heathrow, code: lhr, construction_date: '1946-03-25 00:00:00', sam_datetime: '2001-11-10 14:00:00', open_time: '09:00:00', review_score: null, is_big: '1', country_id: null }
- { id: '2', name: 'Los Angeles', code: lax, construction_date: '1930-01-01 00:00:00', sam_datetime: '2000-11-10 14:00:00', open_time: '08:00:00', review_score: null, is_big: '0', country_id: null }
- { id: '3', name: 'San Francisco', code: sfo, construction_date: '1930-01-01 00:00:00', sam_datetime: '2001-11-10 14:00:01', open_time: '15:00:00', review_score: null, is_big: null, country_id: null }
- { id: '4', name: 'O''Hare', code: ohr, construction_date: '1930-01-01 00:00:00', sam_datetime: '1999-11-10 14:00:01', open_time: '15:00:00', review_score: null, is_big: '1', country_id: null }
countries: { }
flights:
- { id: '1', origin: null, destination: lax, gate: null, duration: '41100.0' }
- { id: '2', origin: sam, destination: rgr, gate: null, duration: '2384.0' }
- { id: '3', origin: sfo, destination: lax, gate: null, duration: '2133.0' }
passengers:
- { id: '1', flight_id: '1', name: 'Anne Arbor' }
- { id: '2', flight_id: '1', name: 'Bob Barry' }
- { id: '3', flight_id: '1', name: 'Charlie Carrot' }
- { id: '4', flight_id: '2', name: 'Fox Flipper' }
- { id: '5', flight_id: '3', name: 'Grace Gumbo' }
pets: { }

0 comments on commit 3c186d4

Please sign in to comment.