Skip to content

Commit

Permalink
Merge branch 'refs/heads/5.x' into 5.x-lambda-has-many-through
Browse files Browse the repository at this point in the history
  • Loading branch information
tychokamphuis-aqqo committed Sep 19, 2024
2 parents 9d92e69 + 5785ee1 commit 29d3379
Show file tree
Hide file tree
Showing 161 changed files with 363 additions and 173 deletions.
21 changes: 15 additions & 6 deletions src/Attributes/LodataEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class LodataEnum extends LodataProperty
{
protected string $enum;

public function __construct(string $name, string $enum, ?string $source = null)
{
parent::__construct($name, $source);
/** @var string */
protected $enum;

public function __construct(
string $name,
string $enum,
?string $description = null,
?string $source = null,
?bool $nullable = true,
?bool $immutable = false
) {
parent::__construct($name, $description, $source);
$this->nullable = $nullable;
$this->immutable = $immutable;
$this->enum = $enum;
}

Expand All @@ -33,4 +42,4 @@ public function getType(): Type

return Lodata::getEnumerationType($this->enum);
}
}
}
23 changes: 15 additions & 8 deletions src/ComplexType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use ArrayAccess;
use Flat3\Lodata\Annotation\Core\V1\Computed;
use Flat3\Lodata\Annotation\Core\V1\ComputedDefaultValue;
use Flat3\Lodata\Annotation\Core\V1\Immutable;
use Flat3\Lodata\Controller\Transaction;
use Flat3\Lodata\Exception\Protocol\NotAcceptableException;
use Flat3\Lodata\Helper\Constants;
Expand Down Expand Up @@ -243,9 +245,11 @@ public function getOpenAPISchema(): array
return [
'type' => Constants::oapiObject,
'title' => $this->getName(),
'properties' => (object) $this->getDeclaredProperties()->map(function (DeclaredProperty $property) {
return $property->getOpenAPISchema();
})
'properties' => (object) $this->getProperties()
->sliceByClass([DeclaredProperty::class, GeneratedProperty::class])
->map(function (Property $property) {
return $property->getOpenAPISchema();
})
];
}

Expand All @@ -259,7 +263,8 @@ public function getOpenAPICreateSchema(): array
'type' => Constants::oapiObject,
'title' => __('lodata:::name (Create schema)', ['name' => $this->getName()]),
'properties' => (object) $this->getDeclaredProperties()->filter(function (DeclaredProperty $property) {
return $property->getAnnotations()->sliceByClass([Computed::class])->isEmpty();
return $property->getAnnotations()->sliceByClass([Computed::class])->isEmpty()
|| $property->getAnnotations()->sliceByClass([ComputedDefaultValue::class])->hasEntries();
})->map(function (DeclaredProperty $property) {
return $property->getOpenAPISchema();
})
Expand All @@ -276,7 +281,7 @@ public function getOpenAPIUpdateSchema(): array
'type' => Constants::oapiObject,
'title' => __('lodata:::name (Update schema)', ['name' => $this->getName()]),
'properties' => (object) $this->getDeclaredProperties()->filter(function (DeclaredProperty $property) {
return $property->getAnnotations()->sliceByClass([Computed::class])->isEmpty();
return $property->getAnnotations()->sliceByClass([Computed::class, Immutable::class])->isEmpty();
})->map(function (DeclaredProperty $property) {
return $property->getOpenAPISchema();
})
Expand Down Expand Up @@ -322,7 +327,7 @@ public function assertPropertyValues(PropertyValues $propertyValues): PropertyVa
public function assertUpdateProperties(PropertyValues $propertyValues): PropertyValues
{
return $this->assertPropertyValues($propertyValues)->filter(function (PropertyValue $propertyValue) {
return !$propertyValue->getProperty()->isImmutable();
return !($propertyValue->getProperty()->isImmutable() || $propertyValue->getProperty()->isComputed());
});
}

Expand Down Expand Up @@ -353,7 +358,7 @@ public function assertCreateProperties(
continue;
}

if ($declaredProperty->isNullable() || $declaredProperty->isComputed()) {
if ($declaredProperty->isNullable() || $declaredProperty->isComputed() || $declaredProperty->isComputedDefault()) {
continue;
}

Expand All @@ -371,6 +376,8 @@ public function assertCreateProperties(
$declaredProperty->assertAllowsValue(null);
}

return $propertyValues;
return $propertyValues->filter(function (PropertyValue $propertyValue) {
return !$propertyValue->getProperty()->isComputed();
});
}
}
1 change: 1 addition & 0 deletions src/Controller/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function __construct(IlluminateRequest $request)
{
$this->method = $request->getRealMethod();
$this->headers = $request->headers;
$this->cookies = $request->cookies;
$this->query = $request->query;
$this->content = $request->content;
$this->server = $request->server;
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public function getAcceptedContentTypes(): MediaTypes
{
$formatQueryOption = $this->getFormat()->getValue();

if (Str::startsWith($formatQueryOption, ['json', 'xml'])) {
if ($formatQueryOption && Str::startsWith($formatQueryOption, ['json', 'xml'])) {
if (!in_array($formatQueryOption, ['json', 'xml'])) {
throw new BadRequestException(
'invalid_short_format',
Expand Down
6 changes: 3 additions & 3 deletions src/Drivers/CSVEntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Flat3\Lodata\Drivers;

use Flat3\Lodata\Annotation\Core\V1\ComputedDefaultValue;
use Flat3\Lodata\Annotation\Core\V1\Computed;
use Flat3\Lodata\DeclaredProperty;
use Flat3\Lodata\EntityType;
use Flat3\Lodata\Type;
Expand All @@ -18,6 +18,6 @@ class CSVEntityType extends EntityType
public function __construct($identifier)
{
parent::__construct($identifier);
$this->setKey((new DeclaredProperty('offset', Type::int64()))->addAnnotation(new ComputedDefaultValue));
$this->setKey((new DeclaredProperty('offset', Type::int64()))->addAnnotation(new Computed));
}
}
}
18 changes: 8 additions & 10 deletions src/Drivers/EloquentEntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Doctrine\DBAL\Schema\Column;
use Exception;
use Flat3\Lodata\Annotation\Capabilities\V1\DeepInsertSupport;
use Flat3\Lodata\Annotation\Core\V1\ComputedDefaultValue;
use Flat3\Lodata\Annotation\Core\V1\Computed;
use Flat3\Lodata\Annotation\Core\V1\Description;
use Flat3\Lodata\Attributes\LodataIdentifier;
use Flat3\Lodata\Attributes\LodataProperty;
Expand Down Expand Up @@ -188,14 +188,12 @@ public function read(PropertyValue $key): Entity
*/
protected function setModelAttributes(Model $model, PropertyValues $propertyValues): Model
{
foreach ($propertyValues->getDeclaredPropertyValues() as $propertyValue) {
$model->setAttribute(
$this->getPropertySourceName($propertyValue->getProperty()),
$propertyValue->getPrimitive()->toMixed()
return $propertyValues->getDeclaredPropertyValues()->reduce(function (Model $model, PropertyValue $value) {
return $model->setAttribute(
$this->getPropertySourceName($value->getProperty()),
$value->getPrimitive()->toMixed(),
);
}

return $model;
}, $model);
}

/**
Expand Down Expand Up @@ -230,7 +228,7 @@ public function create(PropertyValues $propertyValues): Entity
foreach ($navigationProperty->getConstraints() as $constraint) {
$referencedProperty = $constraint->getReferencedProperty();
$model->setAttribute(
$referencedProperty->getName(),
$this->getPropertySourceName($referencedProperty),
$this->navigationSource->getParent()->getEntityId()->getPrimitive()->toMixed()
);
}
Expand Down Expand Up @@ -685,7 +683,7 @@ public function columnToDeclaredProperty(Column $column): ?DeclaredProperty

$defaultValue = $model->getAttributeValue($column->getName());
if ($defaultValue) {
$property->addAnnotation(new ComputedDefaultValue);
$property->addAnnotation(new Computed);
$property->setDefaultValue($defaultValue);
}

Expand Down
4 changes: 3 additions & 1 deletion src/EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Flat3\Lodata;

use Flat3\Lodata\Annotation\Core\V1\Computed;
use Flat3\Lodata\Annotation\Core\V1\Immutable;
use Flat3\Lodata\Controller\Transaction;
use Flat3\Lodata\Exception\Internal\PathNotHandledException;
use Flat3\Lodata\Facades\Lodata;
Expand Down Expand Up @@ -98,7 +99,8 @@ public function getOpenAPIUpdateSchema(): array
'type' => Constants::oapiObject,
'title' => __('lodata:::name (Update schema)', ['name' => $this->getName()]),
'properties' => (object) $this->getDeclaredProperties()->filter(function (DeclaredProperty $property) {
return $property->getAnnotations()->sliceByClass([Computed::class])->isEmpty() && $property !== $this->getKey();
return $property->getAnnotations()->sliceByClass([Computed::class, Immutable::class])->isEmpty()
&& $property !== $this->getKey();
})->map(function (DeclaredProperty $property) {
return $property->getOpenAPISchema();
})
Expand Down
5 changes: 5 additions & 0 deletions src/GeneratedProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
abstract class GeneratedProperty extends Property
{
public function __construct($name, ?Type $type = null)
{
parent::__construct($name, $type !== null ? $type : Type::untyped());
}

/**
* Generate the property value for this property on the provided entity
* @param ComplexValue $value Entity this property is generated on
Expand Down
14 changes: 14 additions & 0 deletions src/Helper/ObjectArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ public function map(callable $callback)
return array_map($callback, $this->array);
}

/**
* Reduce the objects in the array
* @param callable(mixed $initial, mixed $value, mixed $key): mixed $callback
* @param $initial
* @return mixed|null
*/
public function reduce(callable $callback, $initial = null)
{
foreach ($this->array as $key => $value) {
$initial = $callback($initial, $value, $key);
}
return $initial;
}

/**
* Sort the objects in the array
* @param callable $callback
Expand Down
2 changes: 1 addition & 1 deletion src/PathSegment/OpenAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ public static function applyProperty(?Property $property = null, array $schema =
$schema['nullable'] = $property->isNullable();

if ($property->hasStaticDefaultValue()) {
$schema['default'] = $property->computeDefaultValue();
$schema['default'] = $property->computeDefaultValue()->toJson();
}

if ($property->hasMaxLength()) {
Expand Down
13 changes: 12 additions & 1 deletion src/Primitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Flat3\Lodata\Interfaces\ResponseInterface;
use Flat3\Lodata\Interfaces\SerializeInterface;
use Flat3\Lodata\Traits\HasIdentifier;
use Stringable;

/**
* Primitive
Expand Down Expand Up @@ -58,6 +59,16 @@ public function __construct($value = null)
*/
abstract public function set($value);

/**
* Test the provided value is formatted correctly for this type
* @param $value
* @return bool
*/
public function allows($value): bool
{
return true;
}

/**
* Get the internal representation of the value
* @return mixed
Expand Down Expand Up @@ -181,7 +192,7 @@ public function matches($value): bool
$value = $value->get();
}

if (is_scalar($value) || is_null($value) || $value instanceof \Stringable) {
if (is_scalar($value) || is_null($value) || $value instanceof Stringable) {
return (string) $this->get() === (string) $value;
}

Expand Down
12 changes: 11 additions & 1 deletion src/PrimitiveType.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,14 @@ public function getOpenAPISchema(): array
{
return $this->instance()->getOpenAPISchema();
}
}

/**
* Test the provided value is correctly formatted for this type
* @param $value
* @return bool
*/
public function allowsValue($value): bool
{
return $this->instance()->allows($value);
}
}
25 changes: 22 additions & 3 deletions src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ public function isSearchable(): bool
*/
public function assertAllowsValue($value)
{
if (!$this->isNullable() && $value === null) {
if ($value === null) {
if ($this->isNullable()) {
return;
}

throw new BadRequestException(
'property_not_nullable',
sprintf("The property '%s' cannot be set to null", $this->getName())
Expand All @@ -383,6 +387,13 @@ public function assertAllowsValue($value)
sprintf("The value property '%s' exceeds the maximum length", $this->getName())
);
}

if (!$this->type->allowsValue($value)) {
throw new BadRequestException(
'property_value_invalid',
sprintf("The value property '%s' is not valid", $this->getName()),
);
}
}

/**
Expand All @@ -409,8 +420,16 @@ public function getOpenAPISchema(): array
public function isComputed(): bool
{
return $this instanceof ComputedProperty ||
$this->hasAnnotation(new Computed, Boolean::true()) ||
$this->hasAnnotation(new ComputedDefaultValue, Boolean::true());
$this->hasAnnotation(new Computed, Boolean::true());
}

/**
* Determine whether this property is computed on create operations
* @return bool
*/
public function isComputedDefault(): bool
{
return $this->hasAnnotation(new ComputedDefaultValue, Boolean::true());
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,14 @@ public function is(string $class): bool
{
return is_a($this->factory, $class, true);
}

/**
* Test the provided value is correctly formatted for this type
* @param $value
* @return bool
*/
public function allowsValue($value): bool
{
return true;
}
}
8 changes: 7 additions & 1 deletion src/Type/Guid.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Flat3\Lodata\PathSegment\OpenAPI;
use Flat3\Lodata\Primitive;
use Flat3\Lodata\Property;
use Stringable;

/**
* Guid
Expand All @@ -27,7 +28,7 @@ public function matches($value): bool
$value = $value->get();
}

if (is_scalar($value) || is_null($value) || $value instanceof \Stringable) {
if (is_scalar($value) || is_null($value) || $value instanceof Stringable) {
return (string) $this->get() === strtoupper((string) $value);
}

Expand Down Expand Up @@ -81,4 +82,9 @@ public function getOpenAPISchema(?Property $property = null): array
'pattern' => '^'.Lexer::guid.'$',
]);
}

public function allows($value): bool
{
return Lexer::patternCheck(Lexer::guid, (string) $value);
}
}
Loading

0 comments on commit 29d3379

Please sign in to comment.