20250804
This commit is contained in:
@@ -8,13 +8,23 @@
|
||||
|
||||
use Exception;
|
||||
use goodboyalex\php_components_pack\classes\ObjectArray;
|
||||
use goodboyalex\php_components_pack\classes\Tuple;
|
||||
use goodboyalex\php_db_components_pack\attributes\AutoIncrement;
|
||||
use goodboyalex\php_db_components_pack\attributes\Check;
|
||||
use goodboyalex\php_db_components_pack\attributes\ConvertToDB;
|
||||
use goodboyalex\php_db_components_pack\attributes\DataType;
|
||||
use goodboyalex\php_db_components_pack\attributes\DefaultValue;
|
||||
use goodboyalex\php_db_components_pack\attributes\FieldName;
|
||||
use goodboyalex\php_db_components_pack\attributes\ForeignKey;
|
||||
use goodboyalex\php_db_components_pack\attributes\IgnoredInDB;
|
||||
use goodboyalex\php_db_components_pack\attributes\NotNull;
|
||||
use goodboyalex\php_db_components_pack\attributes\PrimaryKey;
|
||||
use goodboyalex\php_db_components_pack\attributes\Unique;
|
||||
use goodboyalex\php_db_components_pack\classes\ConditionBuilder;
|
||||
use goodboyalex\php_db_components_pack\classes\DataBaseHeadItem;
|
||||
use goodboyalex\php_db_components_pack\classes\DBItemProperty;
|
||||
use goodboyalex\php_db_components_pack\enums\DBOperation;
|
||||
use goodboyalex\php_db_components_pack\enums\DBType;
|
||||
use goodboyalex\php_db_components_pack\interfaces\IDBItem;
|
||||
use PDO;
|
||||
use ReflectionClass;
|
||||
@@ -68,11 +78,11 @@
|
||||
continue;
|
||||
|
||||
// - получаю рефлексию класса
|
||||
$reflectedClass = new ReflectionClass(get_class($source));
|
||||
$class = new ReflectionClass(get_class($source));
|
||||
|
||||
try {
|
||||
// - получаю рефлексию свойства
|
||||
$reflectionProperty = $reflectedClass->getProperty($key);
|
||||
$property = $class->getProperty($key);
|
||||
}
|
||||
catch (ReflectionException) {
|
||||
// - если ошибка, то вывожу пустой массив
|
||||
@@ -80,22 +90,12 @@
|
||||
}
|
||||
|
||||
// - пропускаю не публичные свойства
|
||||
if (!$reflectionProperty->isPublic())
|
||||
if (!$property->isPublic())
|
||||
// -- пропускаю
|
||||
continue;
|
||||
|
||||
// - получаю атрибуты
|
||||
$attributes = $reflectionProperty->getAttributes();
|
||||
|
||||
/**
|
||||
* Фильтруем поля, игнорируемые для данной операции
|
||||
*
|
||||
* @var PrimaryKey|null $pkAttr Атрибут первичного ключа.
|
||||
*/
|
||||
$pkAttr = self::FindAttribute($attributes, PrimaryKey::class);
|
||||
|
||||
// - это первичный ключ?
|
||||
$isPrimary = $pkAttr !== null;
|
||||
$attributes = $property->getAttributes();
|
||||
|
||||
/**
|
||||
* Фильтруем поля, игнорируемые для данной операции
|
||||
@@ -131,8 +131,100 @@
|
||||
$converterFromDB = $convertAttr?->ConvertFromDB;
|
||||
$compareFunc = $convertAttr?->Compare;
|
||||
|
||||
// - получаем свойства столбца
|
||||
/**
|
||||
* Атрибут первичного ключа.
|
||||
*
|
||||
* @var PrimaryKey|null $pkAttr Атрибут первичного ключа.
|
||||
*/
|
||||
$pkAttr = self::FindAttribute($attributes, PrimaryKey::class);
|
||||
|
||||
// -- это первичный ключ?
|
||||
$isPrimary = $pkAttr !== null;
|
||||
|
||||
/**
|
||||
* Тип данных поля.
|
||||
*
|
||||
* @var DataType|null $dtAttr Атрибут типа данных
|
||||
*/
|
||||
$dtAttr = self::FindAttribute($attributes, DataType::class);
|
||||
|
||||
// - тип данных
|
||||
$dataType = $dtAttr !== null
|
||||
? new Tuple($dtAttr->Type, $dtAttr->Size)
|
||||
: new Tuple(self::GetDBTypeForType($property->getType()->getName()), 0);
|
||||
|
||||
/**
|
||||
* Атрибут "не пустое значение".
|
||||
*
|
||||
* @var NotNull|null $nnAttr Атрибут нет пустому значению.
|
||||
*/
|
||||
$nnAttr = self::FindAttribute($attributes, NotNull::class);
|
||||
|
||||
// -- это первичный ключ?
|
||||
$isNotNull = $nnAttr !== null;
|
||||
|
||||
/**
|
||||
* Атрибут "уникальное значение".
|
||||
*
|
||||
* @var Unique|null $unqAttr Атрибут уникального значения.
|
||||
*/
|
||||
$unqAttr = self::FindAttribute($attributes, Unique::class);
|
||||
|
||||
// -- это уникальный ключ?
|
||||
$isUnique = $unqAttr !== null;
|
||||
|
||||
/**
|
||||
* Ключ для связывания поля.
|
||||
*
|
||||
* @var ForeignKey|null $chkAttr Атрибут связывания.
|
||||
*/
|
||||
$chkAttr = self::FindAttribute($attributes, ForeignKey::class);
|
||||
|
||||
// - связывание с другой таблицей
|
||||
$foreignWith = $chkAttr !== null
|
||||
? new Tuple($chkAttr->TableName, $chkAttr->FieldName)
|
||||
: new Tuple(null, null);
|
||||
|
||||
/**
|
||||
* Атрибут проверки поля.
|
||||
*
|
||||
* @var Check|null $chkAttr Атрибут проверки поля.
|
||||
*/
|
||||
$chkAttr = self::FindAttribute($attributes, Check::class);
|
||||
|
||||
// - проверка данных поля
|
||||
$checkFunc = $chkAttr !== null
|
||||
? $chkAttr->Condition
|
||||
: new ConditionBuilder();
|
||||
|
||||
/**
|
||||
* Атрибут значения по умолчанию поля.
|
||||
*
|
||||
* @var DefaultValue|null $dvAttr Атрибут значения по умолчанию поля.
|
||||
*/
|
||||
$dvAttr = self::FindAttribute($attributes, DefaultValue::class);
|
||||
|
||||
// - значение по умолчанию
|
||||
$default = $dvAttr?->Value;
|
||||
|
||||
/**
|
||||
* Атрибут "автоматической генерации".
|
||||
*
|
||||
* @var AutoIncrement|null $aiAttr Атрибут "автоматической генерации".
|
||||
*/
|
||||
$aiAttr = self::FindAttribute($attributes, AutoIncrement::class);
|
||||
|
||||
// -- это атрибут "автоматической генерации"?
|
||||
$isAutoIncrement = $aiAttr !== null;
|
||||
|
||||
// - создаю заголовок
|
||||
$columnHeader = new DataBaseHeadItem($fieldName, $dataType, $isNotNull, $isUnique, $isPrimary,
|
||||
$foreignWith, $checkFunc, $default, $isAutoIncrement);
|
||||
|
||||
|
||||
// - создаю объект свойства
|
||||
$item = new DBItemProperty($key, $value, $fieldName, $isPrimary, $isIgnore, $converterToDB,
|
||||
$item = new DBItemProperty($key, $value, $fieldName, $columnHeader, $isIgnore, $converterToDB,
|
||||
$converterFromDB, $compareFunc);
|
||||
|
||||
// - добавляю в массив
|
||||
@@ -143,6 +235,28 @@
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает тип из базы данных по типу переменной.
|
||||
*
|
||||
* @param string $type Тип.
|
||||
*
|
||||
* @return DBType Тип из базы данных.
|
||||
*/
|
||||
private static function GetDBTypeForType (string $type): DBType
|
||||
{
|
||||
/**
|
||||
* @noinspection SpellCheckingInspection Отключаю проверку из-за того, что многие типы будут в lower case
|
||||
*/
|
||||
return match (strtolower($type)) {
|
||||
"int", "integer" => DBType::INT,
|
||||
"float", "double" => DBType::FLOAT,
|
||||
"bool", "boolean" => DBType::BOOL,
|
||||
"dateonly", "timeonly", "datetime", "datetimeimmutable" => DBType::DATE,
|
||||
"array" => DBType::ARRAY,
|
||||
default => DBType::STRING
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Подготавливает массив параметров
|
||||
*
|
||||
|
Reference in New Issue
Block a user