*/ class record { /** * Parameters * * Mapped with database::COLUMN * * @var array $parameters Parameters of the record */ public protected(set) array $parameters = []; /** * Constructor * * @param string|null $row Row for converting to record instance parameters * * @return void */ public function __construct(?string $row = null) { // Initializing parameters if (isset($row)) $this->parameters = static::deserialize($row); } /** * Columns * * Combine parameters of the record with columns of the database * The array of parameters of the record will become associative * * @return static The instance from which the method was called (fluent interface) */ public function columns(database $database): static { // Combining database columns with record parameters $this->parameters = array_combine($database->columns, $this->parameters); // Exit (success) return $this; } /** * Serialize * * Convert record instance to values for writing into the database * * @return string Serialized record */ public function serialize(): string { // Declaring the buffer of generated row $serialized = ''; foreach ($this->parameters as $value) { // Iterating over parameters // Generating row by RFC 4180 $serialized .= ',' . preg_replace('/(?<=[^^])"(?=[^$])/', '""', preg_replace('/(?<=[^^]),(?=[^$])/', '\,', $value ?? '')); } // Trimming excess first comma in the buffer of generated row $serialized = mb_substr($serialized, 1, mb_strlen($serialized)); // Exit (success) return $serialized; } /** * Deserialize * * Convert values from the database and write to the record instance * * @param string $row Row from the database * * @return array Deserialized record */ public function deserialize(string $row): array { // Separating row by commas preg_match_all('/(.*)(?>(?parameters[$name] = $value; } /** * Read * * Read the parameter * * @param string $name Name of the parameter * * @return mixed Content of the parameter */ public function __get(string $name): mixed { // Reading the parameter and exit (success) return $this->parameters[$name]; } /** * Delete * * Delete the parameter * * @param string $name Name of the parameter * * @return void */ public function __unset(string $name): void { // Deleting the parameter and exit (success) unset($this->parameter[$name]); } /** * Check for initializing * * Check for initializing the parameter * * @param string $name Name of the parameter * * @return bool Is the parameter initialized? */ public function __isset(string $name): bool { // Checking for initializing the parameter and exit (success) return isset($this->parameters[$name]); } }