2019-08-21 20:13:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Ulmus\Common;
|
|
|
|
|
2020-10-16 15:27:54 +00:00
|
|
|
use Ulmus\Annotation\Annotation;
|
|
|
|
use Ulmus\Migration\FieldDefinition;
|
2020-04-09 13:50:09 +00:00
|
|
|
use Ulmus\Ulmus,
|
2020-04-09 17:20:07 +00:00
|
|
|
Ulmus\Adapter\AdapterInterface,
|
2020-04-09 13:50:09 +00:00
|
|
|
Ulmus\Annotation\Property\Field;
|
2019-08-21 20:13:00 +00:00
|
|
|
|
|
|
|
class EntityField
|
|
|
|
{
|
|
|
|
public string $name;
|
|
|
|
|
|
|
|
public string $entityClass;
|
|
|
|
|
|
|
|
public string $alias;
|
|
|
|
|
|
|
|
protected EntityResolver $entityResolver;
|
|
|
|
|
2020-04-09 13:50:09 +00:00
|
|
|
public function __construct(string $entityClass, string $name, string $alias, EntityResolver $resolver)
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
|
|
|
$this->entityClass = $entityClass;
|
|
|
|
$this->name = $name;
|
|
|
|
$this->alias = $alias;
|
2020-04-09 13:50:09 +00:00
|
|
|
$this->entityResolver = $resolver;
|
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
|
|
|
|
public function name($useAlias = true) : string
|
|
|
|
{
|
2020-04-09 13:50:09 +00:00
|
|
|
$name = $this->entityResolver->searchFieldAnnotation($this->name, new Field() )->name ?? $this->name;
|
|
|
|
|
2020-10-16 15:27:54 +00:00
|
|
|
$name = $this->entityResolver->databaseAdapter()->adapter()->escapeIdentifier($name, AdapterInterface::IDENTIFIER_FIELD);
|
|
|
|
|
2020-04-09 17:20:07 +00:00
|
|
|
return $useAlias ? "{$this->alias}.$name" : $name;
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function isScalarType($type) : bool
|
|
|
|
{
|
|
|
|
switch($type) {
|
|
|
|
case 'int':
|
|
|
|
case 'bool':
|
|
|
|
case 'string':
|
|
|
|
case 'float':
|
|
|
|
case 'double':
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 15:27:54 +00:00
|
|
|
public static function generateCreateColumn($field) : string
|
|
|
|
{
|
|
|
|
$definition = new FieldDefinition($field);
|
|
|
|
|
|
|
|
# column_name data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT] column_constraint;
|
|
|
|
|
|
|
|
return implode(" ", [
|
|
|
|
$definition->getSqlName(),
|
|
|
|
$definition->getSqlType(),
|
2020-10-20 19:40:44 +00:00
|
|
|
$definition->getSqlParams(),
|
2020-10-16 15:27:54 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
public static function isObjectType($type) : bool
|
|
|
|
{
|
2021-01-26 16:48:40 +00:00
|
|
|
# @Should be fixed with isBuiltIn() instead, it won't be correct based only on name
|
2020-01-29 21:11:16 +00:00
|
|
|
# return strpos($type, "\\") !== false;
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString() : string
|
|
|
|
{
|
|
|
|
return $this->name();
|
|
|
|
}
|
|
|
|
}
|