Merge branch 'notes-2.x' of https://git.mcnd.ca/mcndave/ulmus into notes-2.x
This commit is contained in:
commit
feee26cd26
81 files changed
+460
-904
No files matched your search
@@ -2,12 +2,10 @@
|
||||
|
||||
namespace Ulmus\Common;
|
||||
|
||||
use Ulmus\Annotation\Annotation;
|
||||
use Ulmus\Attribute;
|
||||
use Ulmus\Attribute\Property\{ Field };
|
||||
use Ulmus\Migration\FieldDefinition;
|
||||
use Ulmus\Ulmus,
|
||||
Ulmus\Adapter\AdapterInterface,
|
||||
Ulmus\Annotation\Property\Field,
|
||||
Ulmus\Query\WhereRawParameter;
|
||||
|
||||
class EntityField implements WhereRawParameter
|
||||
@@ -30,7 +28,7 @@ class EntityField implements WhereRawParameter
|
||||
|
||||
public function name($useAlias = true) : string
|
||||
{
|
||||
$name = $this->entityResolver->searchFieldAnnotation($this->name, [ Attribute\Property\Field::class, Field::class ] )->name ?? $this->name;
|
||||
$name = $this->entityResolver->searchFieldAnnotation($this->name, [ Field::class ] )->name ?? $this->name;
|
||||
|
||||
$name = $this->entityResolver->databaseAdapter()->adapter()->escapeIdentifier($name, AdapterInterface::IDENTIFIER_FIELD);
|
||||
|
||||
|
||||
@@ -6,13 +6,13 @@ use Notes\Common\ReflectedClass;
|
||||
use Notes\Common\ReflectedProperty;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use Ulmus\Ulmus,
|
||||
Ulmus\Annotation\Classes\Table,
|
||||
Ulmus\Annotation\Property\Field,
|
||||
Ulmus\Annotation\Property\Virtual,
|
||||
Ulmus\Annotation\Property\Relation,
|
||||
Ulmus\Attribute;
|
||||
Ulmus\Attribute\Obj\Table,
|
||||
Ulmus\Attribute\Obj\AdapterAttributeInterface,
|
||||
Ulmus\Attribute\Property\Field,
|
||||
Ulmus\Attribute\Property\Relation,
|
||||
Ulmus\Attribute\Property\Virtual;
|
||||
|
||||
use Notes\Annotation;
|
||||
use Notes\Common\ReflectedAttribute;
|
||||
|
||||
use Notes\ObjectReflection;
|
||||
|
||||
@@ -37,7 +37,7 @@ class EntityResolver {
|
||||
$this->reflectedClass = ObjectReflection::fromClass($entityClass, $cache)->reflectClass();
|
||||
}
|
||||
|
||||
public function field($name, $fieldKey = self::KEY_ENTITY_NAME, $throwException = true) : ? ReflectedProperty
|
||||
public function field($name, $fieldKey = self::KEY_ENTITY_NAME, $throwException = true) : null|ReflectedProperty
|
||||
{
|
||||
try{
|
||||
return $this->fieldList($fieldKey)[$name] ?? null;
|
||||
@@ -51,7 +51,7 @@ class EntityResolver {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function searchField($name) : null|array
|
||||
public function searchField($name) : null|ReflectedProperty
|
||||
{
|
||||
return $this->field($name, self::KEY_ENTITY_NAME, false) ?: $this->field($name, self::KEY_COLUMN_NAME, false);
|
||||
}
|
||||
@@ -62,8 +62,8 @@ class EntityResolver {
|
||||
|
||||
foreach($this->reflectedClass->getProperties(true) as $item) {
|
||||
foreach($item->getAttributes() as $tag) {
|
||||
if ( $tag->object instanceof Attribute\Property\Field ) {
|
||||
if ( $skipVirtual && $tag->object instanceof Attribute\Property\Virtual ) {
|
||||
if ( $tag->object instanceof Field ) {
|
||||
if ( $skipVirtual && $tag->object instanceof Virtual ) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ class EntityResolver {
|
||||
try{
|
||||
if ( $property ) {
|
||||
foreach($property->getAttributes() as $tag) {
|
||||
if ( $tag->object instanceof Relation or $tag->object instanceof Attribute\Property\Relation ) {
|
||||
if ( $tag->object instanceof Relation ) {
|
||||
return $property;
|
||||
}
|
||||
}
|
||||
@@ -120,7 +120,7 @@ class EntityResolver {
|
||||
return $this->searchFieldAnnotationList($field, $annotationType, $caseSensitive)[0] ?? null;
|
||||
}
|
||||
|
||||
public function searchFieldAnnotationList(string $field, array|object|string $annotationType, bool $caseSensitive = true) : array
|
||||
public function searchFieldAnnotationList(string $field, array|object|string $attributeType, bool $caseSensitive = true) : false|array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
@@ -128,19 +128,11 @@ class EntityResolver {
|
||||
|
||||
$search = $caseSensitive ? $properties : array_change_key_case($properties, \CASE_LOWER);
|
||||
|
||||
$annotations = is_array($annotationType) ? $annotationType : [ $annotationType ];
|
||||
|
||||
if ( null !== ( $search[$field] ?? null ) ) {
|
||||
foreach($search[$field]->getAttributes() as $tag) {
|
||||
foreach($annotations as $annotation) {
|
||||
if ( $tag->object instanceof $annotation ) {
|
||||
$list[] = $tag->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_map(fn(ReflectedAttribute $e) => $e->object, $search[$field]->getAttributes((array) $attributeType));
|
||||
}
|
||||
|
||||
return $list;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function tableName($required = false) : string
|
||||
@@ -156,7 +148,7 @@ class EntityResolver {
|
||||
return $table->name ?? "";
|
||||
}
|
||||
|
||||
public function tableAnnotation($required = false) : null|Table|Attribute\Obj\Table
|
||||
public function tableAnnotation($required = false) : null|Table
|
||||
{
|
||||
if ( null === $table = $this->getTableAttribute() ) {
|
||||
if ($required) {
|
||||
@@ -167,7 +159,7 @@ class EntityResolver {
|
||||
return $table;
|
||||
}
|
||||
|
||||
public function databaseName() : ? string
|
||||
public function databaseName() : null|string
|
||||
{
|
||||
return $this->tableAnnotation(false)->database ?? $this->databaseAdapter()->adapter()->databaseName() ?? null;
|
||||
}
|
||||
@@ -196,7 +188,7 @@ class EntityResolver {
|
||||
return $this->sqlAdapter();
|
||||
}
|
||||
|
||||
public function schemaName(bool $required = false) : ? string
|
||||
public function schemaName(bool $required = false) : null|string
|
||||
{
|
||||
if ( null === $table = $this->getTableAttribute() ) {
|
||||
throw new \LogicException("Your entity {$this->entityClass} seems to be missing a @Table() annotation");
|
||||
@@ -209,10 +201,10 @@ class EntityResolver {
|
||||
return $table->schema ?? null;
|
||||
}
|
||||
|
||||
public function getPrimaryKeyField() : ? array
|
||||
public function getPrimaryKeyField() : null|array
|
||||
{
|
||||
foreach($this->fieldList() as $key => $value) {
|
||||
$field = $this->searchFieldAnnotation($key, [ Attribute\Property\Field::class, Field::class ]);
|
||||
$field = $this->searchFieldAnnotation($key, [ Field::class ]);
|
||||
if ( null !== $field ) {
|
||||
if ( false !== ( $field->attributes['primary_key'] ?? false ) ) {
|
||||
return [ $key => $field ];
|
||||
@@ -235,57 +227,17 @@ class EntityResolver {
|
||||
|
||||
protected function getAdapterInterfaceAttribute() : null|object
|
||||
{
|
||||
return $this->getAttributeImplementing(Attribute\Obj\AdapterAttributeInterface::class);
|
||||
return $this->getAttributeImplementing(AdapterAttributeInterface::class);
|
||||
}
|
||||
|
||||
protected function getTableAttribute()
|
||||
{
|
||||
return $this->getAttributeImplementing(Attribute\Obj\Table::class) ?: $this->getAnnotationFromClassname( Table::class, false );
|
||||
return $this->getAttributeImplementing(Table::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an annotation into it's object's counterpart
|
||||
*/
|
||||
public function getAnnotationFromClassname(string $className, bool $throwError = true) : ? object
|
||||
public function getAttributeImplementing(string $interface) : null|object
|
||||
{
|
||||
exit(__FILE__);
|
||||
if ( $name = $this->uses[$className] ?? false ) {
|
||||
foreach(array_reverse($this->class['tags']) as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
|
||||
foreach($this->properties as $item) {
|
||||
foreach(array_reverse($item['tags']) as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->methods as $item) {
|
||||
foreach(array_reverse($item['tags']) as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
return $this->instanciateAnnotationObject($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($throwError) {
|
||||
throw new \TypeError("Annotation `$className` could not be found within your object `{$this->entityClass}`");
|
||||
}
|
||||
}
|
||||
elseif ($throwError) {
|
||||
throw new \InvalidArgumentException("Class `$className` was not found within {$this->entityClass} uses statement (or it's children / traits)");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getAttributeImplementing(string $interface) : ? object
|
||||
{
|
||||
foreach (array_reverse($this->reflectedClass->getAttributes(true)) as $item) {
|
||||
foreach ($this->reflectedClass->getAttributes(true) as $item) {
|
||||
if ($item->object instanceof $interface) {
|
||||
return $item->object;
|
||||
}
|
||||
@@ -293,14 +245,4 @@ class EntityResolver {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function instanciateAnnotationObject(array|\ReflectionAttribute $tagDefinition) : object
|
||||
{
|
||||
|
||||
if ($tagDefinition instanceof \ReflectionAttribute) {
|
||||
$obj = $tagDefinition->newInstance();
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user