Add accounts_o object for account management
- Implemented `accounts_o` class with account properties (`phone`, `country_code`, `name`, etc.). - Added `add` method for creating new account entries with input sanitization and default values. - Introduced `asArray` method for converting object data into array format. - Included property initialization in `getObjectProperties` for `accounts_o` object. - Set up table structure and placeholders for caching logic in `objectChanged`.
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?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 (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->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);
|
||||
}
|
||||
|
||||
public function objectChanged(): void
|
||||
{
|
||||
//TODO: Add cache invalidation
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the object as an array
|
||||
* @return array The object as an array
|
||||
*/
|
||||
public function asArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'phone' => $this->phone->value(),
|
||||
'country_code' => $this->country_code->value(),
|
||||
'name' => $this->name->value(),
|
||||
'profile_picture' => $this->profile_picture->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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user