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); } /** * Alias for set() * @param mixed $value The value of the field in the database table */ public function update(mixed $value): void { $this->set($value); } /** * 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); } }