created from svoboda/antivertical

This commit is contained in:
2025-11-03 19:56:35 +07:00
parent 544b317560
commit 737856f951
5 changed files with 351 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
vendor

33
composer.json Normal file
View File

@@ -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"
}
}
}

97
composer.lock generated Normal file
View File

@@ -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"
}

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace mirzaev\record\interfaces;
// Svoboda time
use svoboda\time\statement as svoboda;
// Baza database
use mirzaev\baza\database,
mirzaev\baza\column,
mirzaev\baza\record,
mirzaev\baza\enumerations\encoding,
mirzaev\baza\enumerations\type;
// Built-in libraries
use Exception as exception,
RuntimeException as exception_runtime,
LogicException as exception_logic,
InvalidArgumentException as exception_invalid_argument;
/**
* Active Record
*
* The "Active Record" architectural pattern
*
* @see https://en.wikipedia.org/wiki/Active_record_pattern Active Record
*
* @package mirzaev\record\interfaces
*
* @method static|false read(callable $filter) Read from the database
* @method static|false update() Update the record in the database
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
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;
}

View File

@@ -0,0 +1,154 @@
<?php
declare(strict_types=1);
namespace mirzaev\record\traits;
// Svoboda time
use svoboda\time\statement as svoboda;
// Baza database
use mirzaev\baza\database,
mirzaev\baza\column,
mirzaev\baza\record,
mirzaev\baza\enumerations\encoding,
mirzaev\baza\enumerations\type;
// Built-in libraries
use Exception as exception,
RuntimeException as exception_runtime,
LogicException as exception_logic,
InvalidArgumentException as exception_invalid_argument;
/**
* Active Record
*
* The "Active Record" architectural pattern
*
* @see https://en.wikipedia.org/wiki/Active_record_pattern Active Record
*
* @package mirzaev\record\traits
*
* @method self __construct(?record $record) Constructor
* @method static|false read(callable $filter) Read from the database
* @method static|false update() Update the record in the database
* @method void __set(string $name, mixed $value = null) Write into the database record property
* @method mixed __get(string $name) Read from the database record property
* @method bool __isset(string $name) Check that the database record property is initialized
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
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})
};
}
}