- WIP on attributes ; on testing phase

This commit is contained in:
mcndave committed 2023-01-26 13:25:59 +00:00
1 parent 5295a335b3
commit e6d0279c1a
44 files changed
+763 -142

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Field {
public function __construct(
public ? string $name = null,
public ? string $type = null,
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Bigint extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "bigint",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Blob extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "blob",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
@@ -0,0 +1,17 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class CreatedAt extends \Ulmus\Attribute\Property\Field {
public function __construct(
public ? string $name = null,
public ? string $type = "timestamp",
public array $attributes = [
'default' => "CURRENT_TIMESTAMP",
],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Date extends \Ulmus\Attribute\Property\Field {
public function __construct(
public ? string $name = null,
public ? string $type = "date",
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Datetime extends \Ulmus\Attribute\Property\Field {
public function __construct(
public ? string $name = null,
public ? string $type = "datetime",
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
@@ -0,0 +1,24 @@
<?php
namespace Ulmus\Attribute\Property\Field;
/**
* Since we need consistancy between the declaration of our ID and FK fields, it
* was decided to extend the PK class instead of Field for this case.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ForeignKey extends PrimaryKey {
public function __construct(
public ? string $name = null,
public ? string $type = 'bigint',
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [
'primary_key' => false,
'auto_increment' => false
],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Id extends \Ulmus\Attribute\Property\Field {
public function __construct(
public ? string $name = null,
public ? string $type = 'bigint',
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [
'unsigned' => true,
'auto_increment' => true,
'primary_key' => true,
],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Longblob extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "longblob",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Longtext extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "longtext",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Mediumblob extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "mediumblob",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Mediumtext extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "mediumtext",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
@@ -0,0 +1,20 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class PrimaryKey extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = 'bigint',
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [
'primary_key' => true,
],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Text extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "text",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Time extends \Ulmus\Attribute\Property\Field
{
public function __construct(? string $type = "time", ? int $length = null)
{
parent::__construct($type, $length);
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Blob extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "tinyblob",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Tinyint extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "tinyint",
public ? int $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class UpdatedAt extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "timestamp",
public array $attributes = [
'update' => "CURRENT_TIMESTAMP",
'default' => null,
],
public bool $nullable = true,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class Filter {
public function __construct(
public string $method = ""
)
{}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class FilterJoin {
public function __construct(
public string $method = ""
) {}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class GroupBy {
public array $fields = [];
public function __construct(...$field)
{
if ( $field ) {
$this->fields = $field;
}
}
}
+6
View File
@@ -0,0 +1,6 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class Having extends Where {}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
#[\Attribute]
class Join {
public function __construct(
public string $type,
public null|string|\Stringable $key = null,
public null|string|\Stringable $foreignKey = null,
public null|string $entity = null,
public null|string $alias = null,
) {
$this->key = Attribute::handleArrayField($this->key);
$this->foreignKey = Attribute::handleArrayField($this->foreignKey);
}
}
+6
View File
@@ -0,0 +1,6 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class On extends Where {}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
use Ulmus\Query;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class OrWhere extends Where {
public function __construct(
public string|\Stringable|array $field,
public mixed $value = null,
public null|string $operator = null,
public null|string $condition = Query\Where::CONDITION_OR,
) {
$this->key = Attribute::handleArrayField($this->key);
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class OrderBy {
public function __construct(
public string|\Stringable|array $field,
public string $order = "ASC",
) {
$this->field = Attribute::handleArrayField($this->field);
}
}
+87
View File
@@ -0,0 +1,87 @@
<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Relation {
public function __construct(
public string $type,
public \Stringable|string|array $key = "",
public null|\Closure $generateKey = null,
public null|\Stringable|string|array $foreignKey = null,
public null|\Stringable|string|array $foreignField = null,
public array $foreignKeys = [],
public null|string $bridge = null,
public null|\Stringable|string|array $bridgeKey = null,
public null|\Stringable|string|array $bridgeField = null,
public null|\Stringable|string|array $bridgeForeignKey = null,
public null|\Stringable|string|array $field = null,
public null|string $entity = null,
public null|string $join = null,
public string $function = "loadAll",
) {
$this->key = Attribute::handleArrayField($this->key);
$this->foreignKey = Attribute::handleArrayField($this->foreignKey);
$this->foreignField = Attribute::handleArrayField($this->foreignField);
$this->bridgeKey = Attribute::handleArrayField($this->bridgeKey);
$this->bridgeField = Attribute::handleArrayField($this->bridgeField);
$this->bridgeForeignKey = Attribute::handleArrayField($this->bridgeForeignKey);
$this->field = Attribute::handleArrayField($this->field);
}
public function entity() {
try {
$e = $this->entity;
} catch (\Throwable $ex) {
throw new \Exception("Your @Relation annotation seems to be missing an `entity` entry.");
}
return new $e();
}
public function bridge() {
$e = $this->bridge;
return new $e();
}
public function normalizeType() : string
{
return strtolower(str_replace(['-', '_', ' '], '', $this->type));
}
public function isOneToOne() : bool
{
return $this->normalizeType() === 'onetoone';
}
public function isOneToMany() : bool
{
return $this->normalizeType() === 'onetomany';
}
public function isManyToMany() : bool
{
return $this->normalizeType() === 'manytomany';
}
public function function() : string
{
if ($this->function) {
return $this->function;
}
elseif ($this->isOneToOne()) {
return 'load';
}
return 'loadAll';
}
public function hasBridge() : bool
{
return (bool) $this->bridge;
}
}
@@ -0,0 +1,6 @@
<?php
namespace Ulmus\Attribute\Property\Relation;
#[\Attribute]
class Ignore {}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Virtual extends Field {
public bool $readonly = true;
public function __construct(
public ? string $method = null,
) {}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
use Ulmus\Query;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class Where {
public function __construct(
public string|\Stringable|array $field,
public mixed $value = null,
public string $operator = Query\Where::OPERATOR_EQUAL,
public string $condition = Query\Where::CONDITION_AND,
) {
$this->field = Attribute::handleArrayField($field);
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class WithJoin {
public function __construct(
array $joins = []
) {}
}