- ArrayOf errors now catched in EntityTrait (event soon to be attached to it)
- Fixed some non-nullable defaulted to null vars in functions - Added a new diff method for entities which skips nullable values if they are not defined.
This commit is contained in:
parent
709ed63323
commit
13b7cf06f1
@ -25,22 +25,26 @@ class ArrayOf implements \Ulmus\Entity\EntityValueModifier
|
|||||||
sprintf("Given item '%s' is not a valid enum for type %s ; try one of theses instead : %s", $item, $this->type, implode("', '", array_map(fn($e) => $e->value, $this->type::cases())))
|
sprintf("Given item '%s' is not a valid enum for type %s ; try one of theses instead : %s", $item, $this->type, implode("', '", array_map(fn($e) => $e->value, $this->type::cases())))
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$value = $this->type::from($item);
|
$obj = $this->type::from($item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
elseif (class_exists($this->type)) {
|
elseif (class_exists($this->type)) {
|
||||||
if (is_subclass_of($this->type, JsonUnserializable::class)) {
|
if (is_subclass_of($this->type, JsonUnserializable::class)) {
|
||||||
$value = (new \ReflectionClass($this->type))->newInstanceWithoutConstructor();
|
$obj = (new \ReflectionClass($this->type))->newInstanceWithoutConstructor();
|
||||||
$value->jsonUnserialize($item);
|
$obj->jsonUnserialize($item);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$value = new $this->type($item);
|
$obj = new $this->type($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new \InvalidArgumentException("Given type for ArrayOf is incompatible with it's use. An object should be provided");
|
||||||
|
}
|
||||||
|
|
||||||
|
$return[$index] = $obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$return[$index] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (! $property->type->builtIn) {
|
if (! $property->type->builtIn) {
|
||||||
$cls = $property->type->type;
|
$cls = $property->type->type;
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,6 @@ use Ulmus\EntityCollection;
|
|||||||
|
|
||||||
class ArrayCollection extends EntityCollection implements JsonUnserializable
|
class ArrayCollection extends EntityCollection implements JsonUnserializable
|
||||||
{
|
{
|
||||||
|
|
||||||
public function jsonUnserialize(array $json): void
|
public function jsonUnserialize(array $json): void
|
||||||
{
|
{
|
||||||
$this->fromArray($json);
|
$this->fromArray($json);
|
||||||
|
|||||||
@ -17,7 +17,7 @@ class DatasetHandler
|
|||||||
protected bool $entityStrictFieldsDeclaration = false,
|
protected bool $entityStrictFieldsDeclaration = false,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function pull(object $entity) : Generator
|
public function pull(object $entity, bool $nullable = true) : Generator
|
||||||
{
|
{
|
||||||
foreach($this->entityResolver->fieldList(EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) {
|
foreach($this->entityResolver->fieldList(EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) {
|
||||||
$attribute = $this->entityResolver->searchFieldAnnotation($key,[ Field::class ]);
|
$attribute = $this->entityResolver->searchFieldAnnotation($key,[ Field::class ]);
|
||||||
@ -25,7 +25,7 @@ class DatasetHandler
|
|||||||
if ( $entity->__isset($key) ) {
|
if ( $entity->__isset($key) ) {
|
||||||
yield $attribute->name ?? $key => $entity->$key;
|
yield $attribute->name ?? $key => $entity->$key;
|
||||||
}
|
}
|
||||||
elseif ( $field->allowsNull() ) {
|
elseif ( $nullable && $field->allowsNull() ) {
|
||||||
yield $attribute->name ?? $key => null;
|
yield $attribute->name ?? $key => null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ interface EntityInterface extends SearchableInterface /* extends \JsonSerializab
|
|||||||
public function isLoaded() : bool;
|
public function isLoaded() : bool;
|
||||||
public function jsonSerialize() : mixed;
|
public function jsonSerialize() : mixed;
|
||||||
public static function resolveEntity() : EntityResolver;
|
public static function resolveEntity() : EntityResolver;
|
||||||
public static function repository(string $alias = Repository::DEFAULT_ALIAS, ConnectionAdapter $adapter = null) : Repository;
|
public static function repository(string $alias = Repository::DEFAULT_ALIAS, ?ConnectionAdapter $adapter = null) : Repository;
|
||||||
public static function entityCollection(...$arguments) : EntityCollection;
|
public static function entityCollection(...$arguments) : EntityCollection;
|
||||||
public static function queryBuilder() : QueryBuilderInterface;
|
public static function queryBuilder() : QueryBuilderInterface;
|
||||||
public static function field($name, null|string|false $alias = Repository::DEFAULT_ALIAS) : EntityField;
|
public static function field($name, null|string|false $alias = Repository::DEFAULT_ALIAS) : EntityField;
|
||||||
|
|||||||
@ -28,7 +28,7 @@ class Datetime extends \DateTime implements EntityObjectInterface, \JsonSerializ
|
|||||||
return $obj;
|
return $obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTime($hour, $minute, $second = 0, $microsecond = 0) : \DateTimeInterface
|
public function setTime($hour, $minute, $second = 0, $microsecond = 0) : \DateTime
|
||||||
{
|
{
|
||||||
$fromParent = parent::setTime($hour, $minute, $second, $microsecond);
|
$fromParent = parent::setTime($hour, $minute, $second, $microsecond);
|
||||||
|
|
||||||
@ -45,14 +45,15 @@ class Datetime extends \DateTime implements EntityObjectInterface, \JsonSerializ
|
|||||||
return $this->format($this->format);
|
return $this->format($this->format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[\Deprecated]
|
||||||
public function formatLocale(string $format) : string
|
public function formatLocale(string $format) : string
|
||||||
{
|
{
|
||||||
return strftime($format, $this->getTimestamp());
|
return strftime($format, $this->getTimestamp());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function formatLocaleIntl(int $dateFormatter = \IntlDateFormatter::LONG, int $timeFormatter = \IntlDateFormatter::NONE) : string
|
public function formatLocaleIntl(int $dateFormatter = \IntlDateFormatter::LONG, int $timeFormatter = \IntlDateFormatter::NONE, ? string $pattern = null) : string
|
||||||
{
|
{
|
||||||
$formatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormatter, $timeFormatter, \Locale::getRegion(""));
|
$formatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormatter, $timeFormatter, \date_default_timezone_get(), \IntlDateFormatter::GREGORIAN, $pattern);
|
||||||
|
|
||||||
return $formatter->format($this->getTimestamp());
|
return $formatter->format($this->getTimestamp());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,20 +4,15 @@ namespace Ulmus;
|
|||||||
|
|
||||||
use Notes\Attribute\Ignore;
|
use Notes\Attribute\Ignore;
|
||||||
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
|
||||||
|
|
||||||
use Ulmus\{Attribute\Obj\JsonSerialize,
|
use Ulmus\{Attribute\Obj\JsonSerialize,
|
||||||
Attribute\Property\Join,
|
|
||||||
Attribute\Property\Relation,
|
|
||||||
Attribute\Property\ResettablePropertyInterface,
|
Attribute\Property\ResettablePropertyInterface,
|
||||||
Attribute\Property\Virtual,
|
|
||||||
Common\EntityResolver,
|
Common\EntityResolver,
|
||||||
Common\EntityField,
|
Common\EntityField,
|
||||||
Entity\DatasetHandler,
|
Entity\DatasetHandler,
|
||||||
Entity\EntityInterface,
|
Entity\EntityInterface,
|
||||||
QueryBuilder\QueryBuilderInterface};
|
QueryBuilder\QueryBuilderInterface};
|
||||||
|
|
||||||
use Ulmus\{Attribute\RegisterEntityEventInterface, Entity\EntityValueModifier, SearchRequest, Event};
|
use Ulmus\{Attribute\RegisterEntityEventInterface, Entity\EntityValueModifier, };
|
||||||
|
|
||||||
#[JsonSerialize(includeRelations: true)]
|
#[JsonSerialize(includeRelations: true)]
|
||||||
trait EntityTrait {
|
trait EntityTrait {
|
||||||
@ -33,7 +28,7 @@ trait EntityTrait {
|
|||||||
protected array $entityDatasetUnmatchedFields = [];
|
protected array $entityDatasetUnmatchedFields = [];
|
||||||
|
|
||||||
#[Ignore]
|
#[Ignore]
|
||||||
protected DatasetHandler $datasetHandler;
|
public DatasetHandler $datasetHandler;
|
||||||
|
|
||||||
#[Ignore]
|
#[Ignore]
|
||||||
public string $loadedFromAdapter;
|
public string $loadedFromAdapter;
|
||||||
@ -82,8 +77,17 @@ trait EntityTrait {
|
|||||||
foreach($generator as $field => $value) {
|
foreach($generator as $field => $value) {
|
||||||
foreach($properties[$field]->attributes as $tag) {
|
foreach($properties[$field]->attributes as $tag) {
|
||||||
if ( $tag->object instanceof EntityValueModifier ) {
|
if ( $tag->object instanceof EntityValueModifier ) {
|
||||||
|
try {
|
||||||
$value = $tag->object->push($value, $properties[$field]);
|
$value = $tag->object->push($value, $properties[$field]);
|
||||||
}
|
}
|
||||||
|
catch(\Throwable $ex) {
|
||||||
|
throw new \LogicException(
|
||||||
|
message: sprintf("An error occured trying to push an EntityValueModifier results : %s", $ex->getMessage()),
|
||||||
|
code: $ex->getCode(),
|
||||||
|
previous: $ex,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->$field = $value;
|
$this->$field = $value;
|
||||||
@ -128,6 +132,18 @@ trait EntityTrait {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Ignore]
|
||||||
|
public function entityGetDiffDataset() : array
|
||||||
|
{
|
||||||
|
$return = [];
|
||||||
|
|
||||||
|
foreach($this->datasetHandler->pull($this, false) as $field => $value) {
|
||||||
|
$return[$field] = static::repository()->adapter->adapter()->writableValue($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
#[Ignore]
|
#[Ignore]
|
||||||
public function resetVirtualProperties() : self
|
public function resetVirtualProperties() : self
|
||||||
{
|
{
|
||||||
@ -217,7 +233,12 @@ trait EntityTrait {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return isset($this->$name);
|
if (isset($this->$name)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Handling undefined 'nullable'
|
||||||
|
return new \ReflectionProperty($this, $name)->isInitialized($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Ignore]
|
#[Ignore]
|
||||||
@ -289,7 +310,7 @@ trait EntityTrait {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[Ignore]
|
#[Ignore]
|
||||||
public static function repository(string $alias = Repository::DEFAULT_ALIAS, ConnectionAdapter $adapter = null) : Repository
|
public static function repository(string $alias = Repository::DEFAULT_ALIAS, ?ConnectionAdapter $adapter = null) : Repository
|
||||||
{
|
{
|
||||||
return Ulmus::repository(static::class, $alias, $adapter);
|
return Ulmus::repository(static::class, $alias, $adapter);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -491,7 +491,7 @@ class MysqlQueryBuilder extends SqlQueryBuilder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFragments(QueryFragmentInterface|array|\Stringable|string $fragment = null) : array
|
public function getFragments(QueryFragmentInterface|array|\Stringable|string|null $fragment = null) : array
|
||||||
{
|
{
|
||||||
return $fragment !== null ? array_filter($this->queryStack, fn($e) => is_a($e, $fragment, true) ) : $this->queryStack;
|
return $fragment !== null ? array_filter($this->queryStack, fn($e) => is_a($e, $fragment, true) ) : $this->queryStack;
|
||||||
}
|
}
|
||||||
@ -501,7 +501,7 @@ class MysqlQueryBuilder extends SqlQueryBuilder
|
|||||||
return $this->render();
|
return $this->render();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addParameter(mixed $value, string $key = null) : string
|
public function addParameter(mixed $value, ?string $key = null) : string
|
||||||
{
|
{
|
||||||
if ( $this->parent ?? false ) {
|
if ( $this->parent ?? false ) {
|
||||||
return $this->parent->addParameter($value, $key);
|
return $this->parent->addParameter($value, $key);
|
||||||
@ -540,4 +540,5 @@ class MysqlQueryBuilder extends SqlQueryBuilder
|
|||||||
|
|
||||||
return $next + 1;
|
return $next + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -337,7 +337,7 @@ class Repository implements RepositoryInterface
|
|||||||
|
|
||||||
public function generateDatasetDiff(object $entity, bool $oldValues = false) : array
|
public function generateDatasetDiff(object $entity, bool $oldValues = false) : array
|
||||||
{
|
{
|
||||||
$array = array_change_key_case($entity->toArray());
|
$array = array_change_key_case($entity->entityGetDiffDataset());
|
||||||
|
|
||||||
$dataset = array_change_key_case($entity->entityGetDataset(false, true));
|
$dataset = array_change_key_case($entity->entityGetDataset(false, true));
|
||||||
|
|
||||||
|
|||||||
@ -108,7 +108,7 @@ abstract class Ulmus
|
|||||||
return static::$resolved[$entityClass] ?? static::$resolved[$entityClass] = new Common\EntityResolver($entityClass, static::$cache);
|
return static::$resolved[$entityClass] ?? static::$resolved[$entityClass] = new Common\EntityResolver($entityClass, static::$cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function repository(string $entityClass, string $alias = Repository::DEFAULT_ALIAS, ConnectionAdapter $adapter = null) : Repository
|
public static function repository(string $entityClass, string $alias = Repository::DEFAULT_ALIAS, ?ConnectionAdapter $adapter = null) : Repository
|
||||||
{
|
{
|
||||||
$adapter ??= $entityClass::resolveEntity()->sqlAdapter();
|
$adapter ??= $entityClass::resolveEntity()->sqlAdapter();
|
||||||
$cls = $adapter->adapter()->repositoryClass();
|
$cls = $adapter->adapter()->repositoryClass();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user