PHP 8.4 + MINIMAL 3.2.0 + many improvements

This commit is contained in:
2024-12-15 00:20:44 +03:00
parent 7f7cdced69
commit 0843fd83a5
26 changed files with 931 additions and 416 deletions

View File

@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\models\traits\document as document_trait,
${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface,
${REPO_OWNER}\${REPO_NAME}\models\interfaces\collection as collection_interface,
${REPO_OWNER}\${REPO_NAME}\models\enumerations\language;
// Library for ArangoDB
use ArangoDBClient\Document as _document;
// Framework for ArangoDB
use mirzaev\arangodb\collection,
mirzaev\arangodb\document;
// Built-in libraries
use exception;
/**
* Buffer
*
* Storage of data in the document from ArangoDB
*
* @uses document
* @uses document_interface
* @uses collection_interface
*
* @param static COLLECTION Name of the collection in ArangoDB
* @param static TYPE Type of the collection in ArangoDB
*
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author ${REPO_OWNER} <mail@domain.zone>
*/
trait buffer
{
/**
* Write to buffer of the document
*
* @param array $data Data for writing (merge)
* @param array &$errors Registry of errors
*
* @return bool Is data has written into the document from ArangoDB?
*/
public function write(array $data, array &$errors = []): bool
{
try {
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $errors)) {
// Initialized the collection
// Is the instance of the document from ArangoDB are initialized?
if (!isset($this->document)) throw new exception('The instance of the sessoin document from ArangoDB is not initialized');
// Writing data into buffer of the instance of the document from ArangoDB
$this->document->buffer = array_replace_recursive($this->document->buffer ?? [], $data);
// Is the buffer of the instance of the document from ArangoDB exceed 10 megabytes?
if (mb_strlen(json_encode($this->document->buffer)) > 10485760) throw new exception('The buffer size exceeds 10 megabytes');
// Serializing parameters
if ($this->document->language instanceof language) $this->document->language = $this->document->language->name;
// Writing to ArangoDB and exit (success)
return document::update($this->document, errors: $errors);
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
} catch (exception $e) {
// Writing to the registry of errors
$errors[] = [
'text' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'stack' => $e->getTrace()
];
}
// Exit (fail)
return false;
}
}

View File

@@ -0,0 +1,206 @@
<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface,
${REPO_OWNER}\${REPO_NAME}\models\interfaces\collection as collection_interface,
${REPO_OWNER}\${REPO_NAME}\models\connect;
// Library для ArangoDB
use ArangoDBClient\Document as _document;
// Framework for ArangoDB
use mirzaev\arangodb\connection as arangodb,
mirzaev\arangodb\document as framework_document,
mirzaev\arangodb\collection;
// Built-in libraries
use exception;
/**
* Trait for implementing a document instance from ArangoDB
*
* @uses document_interface
*
* @var protected readonly _document|null $document An instance of the ArangoDB document
*
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author ${REPO_OWNER} <mail@domain.zone>
*/
trait document
{
/**
* Document
*
* @var _document $document An instance of the document from ArangoDB
*/
protected readonly _document $document;
/**
* Constructor
*
* @param bool $initialize Initialize a model?
* @param ?arangodb $arangodb Instance of a session of ArangoDB
* @param _document|null|false $document An instance of the ArangoDB document
*
* @return void
*/
public function __construct(
bool $initialize = true,
?arangodb $arangodb = null,
_document|null|false $document = false
) {
// For the extends system
parent::__construct($initialize, $arangodb);
// Writing to the property
if ($document instanceof _document) $this->__document($document);
else if ($document === null) throw new exception('Failed to initialize an instance of the document from ArangoDB');
}
/**
* Write or read document
*
* @param _document|null $document Instance of document from ArangoDB
*
* @return _document|null Instance of document from ArangoDB
*/
public function __document(?_document $document = null): ?_document
{
// Writing a property storing a document instance to ArangoDB
if ($document) $this->document ??= $document;
// Read a property storing a document instance to ArangoDB and exit (success)
return $this->document ?? null;
}
/**
* Connect
*
* @param collecton_interface $document Document
* @param array &$errors Registry of errors
*
* @return string|null The identifier of the created edge of the "connect" collection, if created
*/
public function connect(collection_interface $document, array &$errors = []): ?string
{
try {
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $errors)) {
if (collection::initialize(connect::COLLECTION, connect::TYPE, errors: $errors)) {
if (collection::initialize($document::COLLECTION, $document::TYPE, errors: $errors)) {
// Initialized collections
if ($this->document instanceof _document) {
// Initialized instance of the document from ArangoDB
// Writing document and exit (success)
return framework_document::write(
connect::COLLECTION,
[
'_from' => $document->getId(),
'_to' => $this->document->getId()
],
errors: $errors
);
} else throw new exception('The instance of the document from ArangoDB is not initialized');
} else throw new exception('Failed to initialize ' . $document::TYPE . ' collection: ' . $document::COLLECTION);
} else throw new exception('Failed to initialize ' . connect::TYPE . ' collection: ' . connect::COLLECTION);
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
} catch (exception $e) {
// Writing to the registry of errors
$errors[] = [
'text' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'stack' => $e->getTrace()
];
}
// Exit (fail)
return null;
}
/**
* Write
*
* Write a property into an instance of the ArangoDB document
*
* @param string $name Name of the property
* @param mixed $value Content of the property
*
* @return void
*/
public function __set(string $name, mixed $value = null): void
{
// Writing to the property into an instance of the ArangoDB document and exit (success)
$this->document->{$name} = $value;
}
/**
* Read
*
* Read a property from an instance of the ArangoDB docuemnt
*
* @param string $name Name of the property
*
* @return mixed Content of the property
*/
public function __get(string $name): mixed
{
// Read a property from an instance of the ArangoDB document and exit (success)
return match ($name) {
default => $this->document->{$name}
};
}
/**
* Delete
*
* Deinitialize the property in an instance of the ArangoDB document
*
* @param string $name Name of the property
*
* @return void
*/
public function __unset(string $name): void
{
// Delete the property in an instance of the ArangoDB document and exit (success)
unset($this->document->{$name});
}
/**
* Check of initialization
*
* Check of initialization of the property into an instance of the ArangoDB document
*
* @param string $name Name of the property
*
* @return bool The property is initialized?
*/
public function __isset(string $name): bool
{
// Check of initializatio nof the property and exit (success)
return isset($this->document->{$name});
}
/**
* Execute a method
*
* Execute a method from an instance of the ArangoDB document
*
* @param string $name Name of the method
* @param array $arguments Arguments for the method
*
* @return mixed Result of execution of the method
*/
public function __call(string $name, array $arguments = []): mixed
{
// Execute the method and exit (success)
return method_exists($this->document, $name) ? $this->document->{$name}($arguments) ?? null : null;
}
}

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
// Built-in libraries
use exception;
/**
* Files
*
* Trait with files handlers
*
* @method static void delete(string $directory, array &$errors)
*
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author ${REPO_OWNER} <mail@domain.zone>
*/
trait files
{
/**
* Delete
*
* Delete files recursively
*
* @param string $directory Directory
* @param array &$errors Registry of errors
*
* @return void
*/
private static function delete(string $directory, array &$errors = []): void
{
try {
if (file_exists($directory)) {
// Directory exists
// Deleting descendant files and directories (enter to the recursion)
foreach (scandir($directory) as $file) {
if ($file === '.' || $file === '..') continue;
else if (is_dir("$directory/$file")) static::delete("$directory/$file", $errors);
else unlink("$directory/$file");
}
// Deleting the directory
rmdir($directory);
// Exit (success)
return;
} else throw new exception('Directory does not exist');
} catch (exception $e) {
// Writing to the registry of errors
$errors[] = [
'text' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'stack' => $e->getTrace()
];
}
// Exit (fail)
return;
}
}

24
author/project/system/models/traits/status.php Normal file → Executable file
View File

@@ -4,24 +4,38 @@ declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\models\traits\document as document_trait,
${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface;
// Built-in libraries
use exception;
/**
* Trait fo initialization of a status
* Status (DUMB SHIT)
*
* Trait for initialization of a status
*
* @uses document_trait
* @uses document_interface
*
* @method bool|null status(array &$errors) Check document by its status
*
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
*
* @author ${REPO_OWNER} < mail >
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author ${REPO_OWNER} <mail@domain.zone>
*/
trait status
{
/**
* Initialize of a status
* Status
*
* Check document by its status
*
* @param array &$errors Registry of errors
*
* @return ?bool Status, if they are found
* @return ?bool Status, if found
*/
public function status(array &$errors = []): ?bool
{
@@ -29,7 +43,7 @@ trait status
// Read from ArangoDB and exit (success)
return $this->document->active ?? false;
} catch (exception $e) {
// Write to the registry of errors
// Writing to the registry of errors
$errors[] = [
'text' => $e->getMessage(),
'file' => $e->getFile(),