84 lines
2.4 KiB
PHP
84 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Common;
|
|
|
|
use Ulmus\Attribute\Property\{ Field };
|
|
use Ulmus\Migration\FieldDefinition;
|
|
use Ulmus\Ulmus,
|
|
Ulmus\Adapter\AdapterInterface,
|
|
Ulmus\Query\WhereRawParameter;
|
|
|
|
class EntityField implements WhereRawParameter
|
|
{
|
|
public function __construct(
|
|
public string $name,
|
|
public string $entityClass,
|
|
public ? string $alias,
|
|
protected EntityResolver $entityResolver,
|
|
protected bool $select = false
|
|
) {}
|
|
|
|
public function name($useAlias = true) : string
|
|
{
|
|
$name = $this->entityResolver->searchFieldAnnotation($this->name, [ Field::class ] )->name ?? $this->name;
|
|
|
|
$name = $this->entityResolver->databaseAdapter()->adapter()->escapeIdentifier($name, AdapterInterface::IDENTIFIER_FIELD);
|
|
|
|
$fullname = $useAlias && $this->alias ? "{$this->alias}.$name" : $name;
|
|
|
|
if ($this->select) {
|
|
$select = $this->entityResolver->searchFieldAnnotation($this->name, [Field\Select::class]);
|
|
|
|
if ($select) {
|
|
return "(" . str_replace(['%NAME%'], [$fullname], $select->sql) . ") AS code";
|
|
}
|
|
}
|
|
|
|
return $fullname;
|
|
}
|
|
|
|
public static function isScalarType($type) : bool
|
|
{
|
|
switch($type) {
|
|
case 'int':
|
|
case 'bool':
|
|
case 'string':
|
|
case 'float':
|
|
case 'double':
|
|
case 'mixed':
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function generateCreateColumn(AdapterInterface $adapter, $field) : string
|
|
{
|
|
$definition = new FieldDefinition($adapter, $field);
|
|
|
|
return implode(" ", [
|
|
$adapter->escapeIdentifier($definition->getSqlName(), AdapterInterface::IDENTIFIER_FIELD),
|
|
$definition->getSqlType(),
|
|
$definition->getSqlParams(),
|
|
]);
|
|
}
|
|
|
|
public static function generateAlterColumn(AdapterInterface $adapter, array $field) : string
|
|
{
|
|
$definition = new FieldDefinition($adapter, $field['definition']);
|
|
|
|
return $adapter->generateAlterColumn($definition, $field);
|
|
}
|
|
|
|
public static function isObjectType($type) : bool
|
|
{
|
|
# @Should be fixed with isBuiltIn() instead, it won't be correct based only on name
|
|
# return strpos($type, "\\") !== false;
|
|
}
|
|
|
|
public function __toString() : string
|
|
{
|
|
return $this->name();
|
|
}
|
|
}
|