- WIP on locale formatting since strftime is deprecated

- Added Limit and Offset attributes in RelationBuilder
This commit is contained in:
Dave M. 2025-07-14 16:54:45 +00:00
parent 2f482324b2
commit b33c67b594
4 changed files with 50 additions and 4 deletions

View File

@ -0,0 +1,10 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Limit {
public function __construct(
public int $value,
) {}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Offset {
public function __construct(
public int $value,
) {}
}

View File

@ -43,6 +43,13 @@ class Datetime extends \DateTime implements EntityObjectInterface {
return strftime($format, $this->getTimestamp());
}
public function formatLocaleIntl(int $dateFormatter = \IntlDateFormatter::LONG, int $timeFormatter = \IntlDateFormatter::NONE) : string
{
$formatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormatter, $timeFormatter, \Locale::getRegion());
return $formatter->format($this->getTimestamp());
}
public function ageAt(null|\DateTime $dateTime) : int
{
return (int) ( (int) ($dateTime ?: new DateTime())->format('Ymd') - (int) $this->format('Ymd') ) / 10000;

View File

@ -6,9 +6,7 @@ use Ulmus\{
Ulmus, Query, Common\EntityResolver, Repository, Event, Common\Sql
};
use Ulmus\Attribute\Property\{
Filter, Join, OrderBy, Relation, Virtual, Where, WithJoin,
};
use Ulmus\Attribute\Property\{Filter, Join, Limit, Offset, OrderBy, Relation, Virtual, Where, WithJoin};
class RelationBuilder
{
@ -30,6 +28,10 @@ class RelationBuilder
protected array $joins;
protected ? Limit $limit;
protected ? Offset $offset;
public function __construct(string|object $entity, ? Repository $repository = null)
{
$this->entity = $entity;
@ -93,6 +95,8 @@ class RelationBuilder
{
if ( null !== ( $relation = $this->resolver->searchFieldAnnotation($name, [ Relation::class ] ) ) ) {
$this->orders = $this->resolver->searchFieldAnnotationList($name, [ OrderBy::class ] );
$this->limit = $this->resolver->searchFieldAnnotation($name, [ Limit::class ] );
$this->offset = $this->resolver->searchFieldAnnotation($name, [ Offset::class ] );
$this->wheres = $this->resolver->searchFieldAnnotationList($name, [ Where::class ] );
$this->filters = $this->resolver->searchFieldAnnotationList($name, [ Filter::class ] );
$this->joins = $this->resolver->searchFieldAnnotationList($name, [ WithJoin::class ] );
@ -184,6 +188,17 @@ class RelationBuilder
}
}
protected function applyLimits() : void
{
if ($this->limit) {
$this->repository->limit($this->limit->value);
}
if ($this->offset) {
$this->repository->offset($this->offset->value);
}
}
protected function instanciateEmptyEntity(string $name, Relation $relation) : object
{
$class = $relation->entity ?? $this->resolver->reflectedClass->getProperties()[$name]->getTypes()[0]->type;
@ -264,7 +279,7 @@ class RelationBuilder
public function oneToOne(string $name, Relation $relation) : Repository
{
return $this->oneToMany($name, $relation)->limit(1);
return $this->oneToMany($name, $relation)->limit($this->limit->value ?? 1);
}
public function oneToMany(string $name, Relation $relation) : Repository
@ -279,6 +294,8 @@ class RelationBuilder
$this->applyWithJoin();
$this->applyLimits();
$field = $relation->key;
if ($relation->foreignKey) {
@ -324,6 +341,8 @@ class RelationBuilder
$this->applyWithJoin();
$this->applyLimits();
if ($selectBridgeField && $relation->bridgeField) {
$this->repository->selectEntity($relation->bridge, $bridgeAlias, $bridgeAlias);
}