Files
api/services/nginx/app/objects/accounts_o.php
T
Jeppe Bundgaard d3a8d0d83d Add enabled property to accounts_o
- Introduced `$enabled` property to `accounts_o` for managing account status.
- Updated property initialization and `asArray` method to include `enabled`.
2025-11-28 10:48:59 +01:00

150 lines
5.1 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use Exception;
use traits\db_object_t;
class accounts_o extends db
{
use db_object_t;
/**
* The phone number
* @var object_property $phone The phone number
*/
public object_property $phone;
/**
* The country code (e.g. 31 for +31)
* @var object_property $country_code The country code (e.g. 31 for +31)
*/
public object_property $country_code;
/**
* The account name
* @var object_property $name The account name
*/
public object_property $name;
/**
* The profile picture property
* @var object_property $profile_picture The profile picture property
*/
public object_property $profile_picture;
/**
* The enabled property
* @var object_property $enabled The enabled property
*/
public object_property $enabled;
/**
* The (last) seen at date
* @var object_property $seen_at The seen at date
*/
public object_property $seen_at;
/**
* The created at date
* @var object_property $created_at The created at date
*/
public object_property $created_at;
/**
* The updated at date
* @var object_property $updated_at The updated at date
*/
public object_property $updated_at;
/**
* The deleted at date
* @var object_property $deleted_at The deleted at date
*/
public object_property $deleted_at;
public function structure(): void
{
$this->setTable('accounts');
}
/**
* Add a new account
* @param array $data The account data
* @return int The account id
* @throws Exception If the account was not created successfully
*/
public function add(array $data): int
{
global /** @var db $db */
$db;
// Set the default values
$data_default = [
'phone' => 0,
'country_code' => 45,
'name' => '',
'profile_picture' => null,
];
// Merge the default values with the data
$data = array_merge($data_default, $data);
// Sanitize the input
$data['phone'] = (int)$db->escape_string($data['phone']);
$data['country_code'] = (int)$data['country_code'];
$data['name'] = (string)$db->escape_string($data['name']);
$data['profile_picture'] = $data['profile_picture'] ? (int)$data['profile_picture'] : null;
// Add the object
$tmp_id = self::add_object($data);
if (!$tmp_id) {
throw new Exception('The account was not created successfully.');
}
return $tmp_id;
}
public function getObjectProperties(): void
{
$this->phone = new object_property($this->table, $this->id, 'phone', 'int', false);
$this->country_code = new object_property($this->table, $this->id, 'country_code', 'int', false);
$this->name = new object_property($this->table, $this->id, 'name', 'string', false);
$this->profile_picture = new object_property($this->table, $this->id, 'profile_picture', 'int', false);
$this->enabled = new object_property($this->table, $this->id, 'enabled', 'bool', false);
$this->seen_at = new object_property($this->table, $this->id, 'seen_at', 'datetime', false);
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'datetime', false);
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'datetime', false);
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'datetime', false);
}
/**
* Invalidate the cached object when it is changed
* @throws Exception If no object is selected
*/
public function objectChanged(): void
{
self::requireSelected();
// Invalidate the cache
self::deleteCached(self::$asArrayCacheKey);
}
/**
* Get the object as an array
* @return array The object as an array
* @throws Exception If no object is selected
*/
public function asArray(): array
{
self::requireSelected();
$cached = self::getCached(self::$asArrayCacheKey);
// If the object is cached, return it
if (!empty($cached)) return $cached;
// Convert the object to an array
$array = [
'id' => (int)$this->id,
'phone' => (int)$this->phone->value(),
'country_code' => (int)$this->country_code->value(),
'name' => (string)$this->name->value(),
'profile_picture' => (int)$this->profile_picture->value(),
'enabled' => (bool)$this->enabled->value(),
'seen_at' => $this->seen_at->value(),
'created_at' => $this->created_at->value(),
'updated_at' => $this->updated_at->value(),
//'deleted_at' => $this->deleted_at->value(),
];
// Cache the object
self::cache(self::$asArrayCacheKey, $array);
self::setCachedExpiration(self::$asArrayCacheKey, self::$asArrayCacheExpiration);
// Return the object as an array
return $array;
}
}