From 737856f951bf975e292cd35b4f393f8a065af9ae Mon Sep 17 00:00:00 2001 From: mirzaev Date: Mon, 3 Nov 2025 19:56:35 +0700 Subject: [PATCH] created from `svoboda/antivertical` --- .gitignore | 1 + composer.json | 33 +++++ composer.lock | 97 ++++++++++++ mirzaev/record/system/interfaces/record.php | 66 +++++++++ mirzaev/record/system/traits/record.php | 154 ++++++++++++++++++++ 5 files changed, 351 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 mirzaev/record/system/interfaces/record.php create mode 100644 mirzaev/record/system/traits/record.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d0d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..42e4e8b --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "mirzaev/record", + "description": "Active Record pattern for Baza", + "homepage": "https://git.svoboda.works/mirzaev/record", + "type": "pattern", + "keywords": [ + "baza" + ], + "readme": "README.md", + "license": "WTFPL", + "authors": [ + { + "name": "Arsen Mirzaev Tatyano-Muradovich", + "email": "arsen@mirzaev.sexy", + "homepage": "https://mirzaev.sexy", + "role": "Creator" + } + ], + "support": { + "wiki": "https://git.svoboda.works/mirzaev/record/wiki", + "issues": "https://git.svoboda.works/mirzaev/record/issues" + }, + "require": { + "php": "^8.4", + "svoboda/time": "^1.0", + "mirzaev/baza": "^3.4" + }, + "autoload": { + "psr-4": { + "mirzaev\\record\\": "mirzaev/record/system" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..f6d7dbc --- /dev/null +++ b/composer.lock @@ -0,0 +1,97 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "0c82d08a56b9613dcd0f25c1e08ce433", + "packages": [ + { + "name": "mirzaev/baza", + "version": "3.4.0", + "source": { + "type": "git", + "url": "https://git.svoboda.works/mirzaev/baza", + "reference": "ec2721dc7727ebcd43b780892f3df6831dfff06b" + }, + "require": { + "php": "^8.4" + }, + "type": "database", + "autoload": { + "psr-4": { + "mirzaev\\baza\\": "mirzaev/baza/system/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "WTFPL" + ], + "authors": [ + { + "name": "Arsen Mirzaev Tatyano-Muradovich", + "email": "arsen@mirzaev.sexy", + "homepage": "https://mirzaev.sexy", + "role": "Programmer" + } + ], + "description": "Lightweight binary database by pure PHP", + "homepage": "https://git.svoboda.works/mirzaev/baza", + "keywords": [ + "Plain", + "binary", + "lightweight" + ], + "support": { + "email": "arsen@mirzaev.sexy", + "issues": "https://git.svoboda.works/mirzaev/baza/issues", + "wiki": "https://git.svoboda.works/mirzaev/baza/wiki" + }, + "time": "2025-08-08T14:27:29+00:00" + }, + { + "name": "svoboda/time", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://git.svoboda.works/svoboda/time", + "reference": "0764960606d56a9f0865ebb07671d30a7d232b6a" + }, + "require": { + "php": "^8.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "svoboda\\time\\": "svoboda/time/system" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "WTFPL" + ], + "authors": [ + { + "name": "Arsen Mirzaev Tatyano-Muradovich", + "email": "arsen@mirzaev.sexy", + "homepage": "https://mirzaev.sexy", + "role": "Programmer" + } + ], + "description": "Time since Svoboda was created", + "homepage": "https://git.svoboda.works/svoboda/time", + "time": "2025-02-02T11:04:25+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": {}, + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": "^8.4" + }, + "platform-dev": {}, + "plugin-api-version": "2.6.0" +} diff --git a/mirzaev/record/system/interfaces/record.php b/mirzaev/record/system/interfaces/record.php new file mode 100644 index 0000000..29daff4 --- /dev/null +++ b/mirzaev/record/system/interfaces/record.php @@ -0,0 +1,66 @@ + + */ +interface ar +{ + /** + * Constructor + * + * @throws exception_invalid_argument If not initialized the database columns parameters + * + * @return void + */ + public function __construct(); + + /** + * Read + * + * Search for the record in the database + * + * @return static|false The record impementator object, if found + */ + public function read(callable $filter): static|false; + + /** + * Update + * + * Write the record new values into the database + * + * @return static|false The updated record, if updated (new instance) + */ + public function update(): static|false; +} diff --git a/mirzaev/record/system/traits/record.php b/mirzaev/record/system/traits/record.php new file mode 100644 index 0000000..f8ead01 --- /dev/null +++ b/mirzaev/record/system/traits/record.php @@ -0,0 +1,154 @@ + + */ +trait record +{ + /** + * Read + * + * Search for the record in the database + * + * @return static|false The record impementator object, if found + */ + public function read(callable $filter): static|false + { + // Reading from the database + $record = $this->database->read( + filter: $filter, + amount: 1, + offset: 0 + )[0] ?? false; + + if ($record instanceof record) { + // Initialized the record + + // Exit (success) + return new static($record); + } + + // Exit (fail) + return false; + } + + /** + * Update + * + * Write the record new values into the database + * + * @return static|false The updated record, if updated (new instance) + */ + public function update(): static|false + { + // Writing into the database + $record = $this->database->read( + filter: fn(record $record) => $record->identifier === $this->record->identifier, + update: function (record &$record) { + $this->record->updated = mirzaev::timestamp(); + $record = $this->record; + }, + amount: 1, + offset: 0 + )[0] ?? false; + + if ($record instanceof record) { + // Initialized the record + + // Exit (success) + return new static($record); + } + + // Exit (fail) + return false; + } + + /** + * Write + * + * Write into the database record property + * + * @param string $name Name of the property + * @param mixed $value Value of the property + * + * @return void + */ + public function __set(string $name, mixed $value = null): void + { + match ($name) { + 'record' => $this->record = $value, + default => $this->record->{$name} = $value + }; + } + + /** + * Read + * + * Read from the database record property + * + * @param string $name Name of the property + * + * @return mixed Content of the property, if they are found + */ + public function __get(string $name): mixed + { + return match ($name) { + 'record' => $this->record, + default => $this->record->{$name} + }; + } + + /** + * Check for initialization + * + * Check that the database record property is initialized + * + * @param string $name Name of the property + * + * @return bool The property is initialized? + */ + public function __isset(string $name): bool + { + // Check of initialization of the property and exit (success) + return match ($name) { + 'record' => isset($this->record), + default => isset($this->record->{$name}) + }; + } +}