- WIP on notes 2.x

This commit is contained in:
mcndave committed 2024-05-28 08:59:12 -04:00
1 parent 2de3139c80
commit f3be11a590
42 files changed
+270 -820

No files matched your search

+18 -70
View File
@@ -6,13 +6,11 @@ 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;
use Notes\Annotation;
Ulmus\Attribute\Obj\Table,
Ulmus\Attribute\Obj\AdapterAttributeInterface,
Ulmus\Attribute\Property\Field,
Ulmus\Attribute\Property\Relation,
Ulmus\Attribute\Property\Virtual;
use Notes\ObjectReflection;
@@ -37,7 +35,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 +49,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 +60,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 +100,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;
}
}
@@ -156,7 +154,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 +165,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 +194,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 +207,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,55 +233,15 @@ 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
{
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
public function getAttributeImplementing(string $interface) : null|object
{
foreach (array_reverse($this->reflectedClass->getAttributes(true)) as $item) {
if ($item->object instanceof $interface) {
@@ -293,14 +251,4 @@ class EntityResolver {
return null;
}
public function instanciateAnnotationObject(array|\ReflectionAttribute $tagDefinition) : object
{
if ($tagDefinition instanceof \ReflectionAttribute) {
$obj = $tagDefinition->newInstance();
}
return $obj;
}
}