Files
api/services/nginx/app/classes/object_property.php
T
Jeppe Bundgaard f206e7f4fb Add JSON type support in object property handling
- Added `json` to the list of valid types for object properties.
- Implemented JSON decoding for column values when the type is `json`.
2025-11-05 13:16:31 +01:00

119 lines
4.5 KiB
PHP

<?php
namespace classes;
class object_property
{
public int $id; // The table of the objects in the database (e.g. users)
private string $table; // The id of the object in the database
private string $column; // The column name of the field in the database table (e.g. id, name, email)
private string $type; // The data type of the field in the database table (e.g. int, varchar, text)
private bool $required; // Whether the field is required or not
private mixed $default; // The default value of the field
private mixed $fake_value; // The fake value of the field, used for testing purposes (When the object id is -1)
public function __construct(string $table, int $id, string $column, string $type, bool $required = false, mixed $default = null)
{
$this->table = $table;
$this->id = $id;
$this->column = $column;
$this->type = $type;
$this->required = $required;
$this->default = $default;
}
public function __toString(): string
{
// Return the value of the field in the database table
return $this->value();
}
/**
* Get the value of the field in the database table
* @return mixed The value of the field in the database table
*/
public function value(): mixed
{
if ($this->id < 0) {
// If the object id is less than 0, return the fake value
return $this->fake_value ?? $this->default;
}
// Get the value of the field in the database table
global $db;
// Sanitize the id to prevent SQL injection
$this->id = (int)$this->id;
// Check if the id is valid
if ($this->id <= 0) {
throw new \InvalidArgumentException("Invalid ID: $this->id");
}
// Check if the table and column are valid
if (empty($this->table) || empty($this->column)) {
throw new \InvalidArgumentException("Table or column cannot be empty");
}
// Check if the type is valid
if (!in_array($this->type, ['int', 'varchar', 'text', 'date', 'datetime', 'string', 'timestamp', 'boolean', 'bool', 'float', 'json'])) {
throw new \InvalidArgumentException("Invalid type: $this->type");
}
$sql = "SELECT $this->column FROM $this->table WHERE id = $this->id";
$result = $db->query($sql);
$row = $db->fetch_assoc($result);
if (empty($row)) {
throw new \RuntimeException("No record found for column '$this->column' in table '$this->table' with ID $this->id");
}
// If the type is set to json, decode it.
$value = $row[$this->column];
if ($this->type === 'json' && $value !== null) {
return json_decode($value, true);
}
return $row[$this->column];
}
/**
* Set the value of the field in the database table
* @param mixed $value The value of the field in the database table
*/
public function set(mixed $value): void
{
if ($this->id < 0) {
// If the object id is less than 0, set the fake value
$this->fake_value = $value;
return;
}
// Set the value of the field in the database table
global /** @var db $db */
$db;
// If the value is null, set it to null
if ($value === null) {
$sql = "UPDATE $this->table SET $this->column = NULL WHERE id = $this->id";
} // If the value is an integer, set it to the integer value
elseif (is_int($value)) {
$sql = "UPDATE $this->table SET $this->column = $value WHERE id = $this->id";
} // If the value is a float, set it to the float value
elseif (is_float($value)) {
$sql = "UPDATE $this->table SET $this->column = $value WHERE id = $this->id";
} // If the value is a string, escape it
else {
$value = $db->escape_string($value);
$sql = "UPDATE $this->table SET $this->column = '$value' WHERE id = $this->id";
}
$db->query($sql);
}
/**
* Nullify the value of the field in the database table
*/
public function nullify(): void
{
if ($this->id < 0) {
// If the object id is less than 0, set the fake value to null
$this->fake_value = null;
return;
}
// Set the value of the field in the database table to null
global /** @var db $db */
$db;
$sql = "UPDATE $this->table SET $this->column = NULL WHERE id = $this->id";
$db->query($sql);
}
}