4in1_ws_web/src/engine/ModelProperty.php
2025-04-28 14:47:25 +03:00

76 lines
1.9 KiB
PHP

<?php
namespace engine;
class ModelProperty
{
public function __construct(
protected ?ModelFieldType $type,
protected mixed $realType,
protected bool $nullable,
protected string $modelName,
protected string $dbName
) {}
public function getDbName(): string {
return $this->dbName;
}
public function getModelName(): string {
return $this->modelName;
}
public function isNullable(): bool {
return $this->nullable;
}
public function getType(): ?ModelFieldType {
return $this->type;
}
public function fromRawValue(mixed $value): mixed {
if ($this->nullable && is_null($value))
return null;
switch ($this->type) {
case ModelFieldType::BOOLEAN:
return (bool)$value;
case ModelFieldType::INTEGER:
return (int)$value;
case ModelFieldType::FLOAT:
return (float)$value;
case ModelFieldType::ARRAY:
return array_filter(explode(',', $value));
case ModelFieldType::JSON:
$val = jsonDecode($value);
if (!$val)
$val = null;
return $val;
case ModelFieldType::SERIALIZED:
$val = unserialize($value);
if ($val === false)
$val = null;
return $val;
case ModelFieldType::BITFIELD:
return new MySQLBitField($value);
case ModelFieldType::BACKED_ENUM:
try {
return $this->realType::from($value);
} catch (\ValueError $e) {
if ($this->nullable)
return null;
throw $e;
}
default:
return (string)$value;
}
}
}