Skip to content

Commit

Permalink
Update PHPdoc & allow immutable annotation via Attributes on Eloque…
Browse files Browse the repository at this point in the history
…nt Model (#836)
  • Loading branch information
yvo-niedrich authored Jul 17, 2024
1 parent a388df1 commit be527e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
17 changes: 15 additions & 2 deletions src/Attributes/LodataProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Flat3\Lodata\Annotation\Core\V1\Computed;
use Flat3\Lodata\Annotation\Core\V1\Description;
use Flat3\Lodata\Annotation\Core\V1\Immutable;
use Flat3\Lodata\DeclaredProperty;
use Flat3\Lodata\EntitySet;
use Flat3\Lodata\Property;
Expand All @@ -18,6 +19,7 @@ abstract class LodataProperty
protected ?string $source = null;
protected bool $key = false;
protected bool $computed = false;
protected bool $immutable = false;
protected bool $nullable = true;
protected ?int $maxLength = null;
protected ?int $precision = null;
Expand All @@ -38,13 +40,15 @@ public function __construct(
$scale = null,
$alternativeKey = false,
$searchable = false,
$filterable = true
$filterable = true,
?bool $immutable = false
) {
$this->name = $name;
$this->description = $description;
$this->source = $source;
$this->key = $key;
$this->computed = $computed;
$this->immutable = $immutable;
$this->nullable = $nullable;
$this->maxLength = $maxLength;
$this->precision = $precision;
Expand Down Expand Up @@ -84,6 +88,11 @@ public function isComputed(): bool
return $this->computed;
}

public function isImmutable(): bool
{
return $this->immutable;
}

public function isNullable(): bool
{
return $this->nullable && !$this->key;
Expand Down Expand Up @@ -158,6 +167,10 @@ public function addProperty(EntitySet $entitySet): Property
$property->addAnnotation(new Computed);
}

if ($this->isImmutable()) {
$property->addAnnotation(new Immutable);
}

if ($this->hasMaxLength()) {
$property->setMaxLength($this->getMaxLength());
}
Expand All @@ -178,4 +191,4 @@ public function addProperty(EntitySet $entitySet): Property
}

abstract public function getType(): Type;
}
}
10 changes: 6 additions & 4 deletions src/Drivers/EloquentEntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ public function getModelByKey(PropertyValue $key): ?Model
*/
public function getTable(): string
{
$model = $this->getModel();

return $model->getTable();
return $this->getModel()->getTable();
}

/**
Expand All @@ -168,7 +166,7 @@ public function getConnection(): ConnectionInterface
/**
* Read an Eloquent model
* @param PropertyValue $key Model key
* @return Entity|null Entity
* @return Entity Entity
*/
public function read(PropertyValue $key): Entity
{
Expand Down Expand Up @@ -258,6 +256,10 @@ public function delete(PropertyValue $key): void
{
$model = $this->getModelByKey($key);

if (null === $model) {
throw new NotFoundException('entity_not_found', 'Entity not found');
}

try {
$model->delete();
} catch (Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/GeneratedProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class GeneratedProperty extends Property
/**
* Generate the property value for this property on the provided entity
* @param ComplexValue $value Entity this property is generated on
* @return PropertyValue
* @return Primitive|scalar|null
*/
abstract public function invoke(ComplexValue $value);

Expand Down

0 comments on commit be527e5

Please sign in to comment.