From 4e04d09b323f7ec7a9b00dbfd905048401387972 Mon Sep 17 00:00:00 2001 From: Arsen Mirzaev Tatyano-Muradovich Date: Fri, 11 Oct 2024 10:01:40 +0300 Subject: [PATCH] TOTAL REBUILD --- LICENSE | 0 README.md | 2 +- composer.json | 2 +- mirzaev/arangodb/system/collection.php | 210 +++++++---- mirzaev/arangodb/system/connection.php | 338 +++++++++--------- mirzaev/arangodb/system/document.php | 217 +++++++---- .../system/enumerations/collection/type.php | 36 ++ mirzaev/arangodb/system/terminal.php | 16 +- 8 files changed, 512 insertions(+), 309 deletions(-) mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md create mode 100644 mirzaev/arangodb/system/enumerations/collection/type.php diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 2e46c75..ae981d1 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# PHP 8.2 Framework for working with ArangoDB +# PHP 8.3 Framework for working with ArangoDB 🀟 Very simple and flexible, easy to scale and integrate diff --git a/composer.json b/composer.json index 6a32ae9..c60539a 100755 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "keywords": [ "ArangoDB" ], - "type": "library", + "type": "framework", "license": "WTFPL", "homepage": "https://git.hood.su/mirzaev/arangodb", "authors": [ diff --git a/mirzaev/arangodb/system/collection.php b/mirzaev/arangodb/system/collection.php index a1a9f30..4d1dd2e 100755 --- a/mirzaev/arangodb/system/collection.php +++ b/mirzaev/arangodb/system/collection.php @@ -4,122 +4,198 @@ declare(strict_types=1); namespace mirzaev\arangodb; -use mirzaev\arangodb\terminal; +// Files of the project +use mirzaev\arangodb\connection, + mirzaev\arangodb\terminal, + mirzaev\arangodb\enumerations\collection\type; -use ArangoDBClient\Connection as _connection; -use ArangoDBClient\Collection as _collection; -use ArangoDBClient\Statement as _statement; -use ArangoDBClient\Document as _document; -use ArangoDBClient\CollectionHandler as _collection_handler; +// Library for ArangoDB +use ArangoDBClient\Statement as _statement, + ArangoDBClient\Document as _document, + ArangoDBClient\CollectionHandler as _collection_handler; + +// Built-in libraries +use exception; /** - * ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ + * Collection * * @package mirzaev\arangodb + * + * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License * @author Arsen Mirzaev Tatyano-Muradovich */ class collection { /** - * Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ + * Initialize a collection * - * @param _connection $session БСссия соСдинСния с Π±Π°Π·ΠΎΠΉ Π΄Π°Π½Π½Ρ‹Ρ… - * @param string $name НазваниС - * @param bool $edge ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Ρ‚ΡŒ ΠΊΠ°ΠΊ Ρ€Π΅Π±Ρ€ΠΎ? (ΠΈΠ½Π°Ρ‡Π΅: Π²Π΅Ρ€ΡˆΠΈΠ½Π°) + * @param string $collection Name of the collection + * @param type $type Type of the collection * @param ?terminal $terminal Π˜Π½ΡΡ‚Π°Π½Ρ†ΠΈΡ Ρ‚Π΅Ρ€ΠΌΠΈΠ½Π°Π»Π° + * @param array &$errors Registry of errors * - * @return string|null Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ + * @return string|null Identifier of the collection */ - public static function init(_connection $session, string $name, bool $edge = false, ?terminal $terminal = null): ?string + public static function initialize(string $name, type $type = type::document, ?terminal $terminal = null, array &$errors = []): ?string { - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ - $collections = new _collection_handler($session); + try { + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ + $collections = new _collection_handler(connection::$session); - if (!$collections->has($name)) { - // НС Π½Π°ΠΉΠ΄Π°Π½Π° коллСкция - - // Π—Π°ΠΏΠΈΡΡŒ Π² Π²Ρ‹Π²ΠΎΠ΄ - if ($terminal instanceof terminal) $terminal::write("ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ \"$name\" Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½Π°"); - - // Π—Π°ΠΏΠΈΡΡŒ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ Π½Π° сСрвСр ΠΈ Π΅Π³ΠΎ ΠΎΡ‚Π²Π΅Ρ‚ Π² Π±ΡƒΡ„Π΅Ρ€ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π° - $id = $collections->create($name, ['type' => $edge ? _collection::TYPE_EDGE : _collection::TYPE_DOCUMENT]); - - if ($collections->has($name)) { - // ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ Π½Π°ΠΉΠ΄Π΅Π½Π° (записана) + if (!$collections->has($name)) { + // НС Π½Π°ΠΉΠ΄Π°Π½Π° коллСкция // Π—Π°ΠΏΠΈΡΡŒ Π² Π²Ρ‹Π²ΠΎΠ΄ - if ($terminal instanceof terminal) $terminal::write("Π‘ΠΎΠ·Π΄Π°Π½Π° коллСкция \"$name\" с Ρ‚ΠΈΠΏΠΎΠΌ " . ($edge ? 'Ρ€Π΅Π±Ρ€ΠΎ' : 'Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚')); + if ($terminal instanceof terminal) $terminal::write("Not found $type collection: $name"); + + // Π—Π°ΠΏΠΈΡΡŒ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ Π½Π° сСрвСр ΠΈ Π΅Π³ΠΎ ΠΎΡ‚Π²Π΅Ρ‚ Π² Π±ΡƒΡ„Π΅Ρ€ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π° + $id = $collections->create($name, ['type' => $type->code()]); + + if ($collections->has($name)) { + // ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ Π½Π°ΠΉΠ΄Π΅Π½Π° (записана) + + // Π—Π°ΠΏΠΈΡΡŒ Π² Π²Ρ‹Π²ΠΎΠ΄ + if ($terminal instanceof terminal) $terminal::write("Created $type collection: $name"); + + // Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€Π° ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ + return $id; + } + } else { + // НайдСна коллСкция // Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€Π° ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ - return $id; + return $name; } - } else { - // НайдСна коллСкция - - // Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€Π° ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ - return $name; + } catch (exception $e) { + // Writing to registry of errors + $errors[] = [ + 'text' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack' => $e->getTrace() + ]; } return null; } /** - * Поиск + * Execute * - * @param _connection $session БСссия соСдинСния с Π±Π°Π·ΠΎΠΉ Π΄Π°Π½Π½Ρ‹Ρ… - * @param string $query AQL-запрос - * @param array $binds Binds for query [bind => value] + * @param string $query Query (AQL) + * @param array $parameters Binded parameters for placeholders [placholder => parameter] + * @param array &$errors Registry of errors * - * @return _document|array|string|int|null Π˜Π½ΡΡ‚Π°Π½Ρ†ΠΈΡ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° + * @return _document|array|string|int|null Instance of the document */ - public static function search(_connection $session, string $query, array $binds = []): _document|string|array|int|null + public static function execute(string $query, array $parameters = [], array &$errors = []): _document|string|array|int|null { - // Statement instance initialization - $instance = new _statement( - $session, - [ - 'query' => $query, - "batchSize" => 1000, - "sanitize" => true - ] - ); + try { + // Statement instance initialization + $instance = new _statement( + connection::$session, + [ + 'query' => $query, + "batchSize" => 1000, + "sanitize" => true + ] + ); - // Binds application - $instance->bind($binds); + // Binds application + $instance->bind($parameters); - // Sending the request - $response = $instance->execute(); + // Sending the request + $response = $instance->execute(); - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π±ΡƒΡ„Π΅Ρ€Π° Π²Ρ‹Π²ΠΎΠ΄Π° - $buffer = []; + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π±ΡƒΡ„Π΅Ρ€Π° Π²Ρ‹Π²ΠΎΠ΄Π° + $buffer = []; - foreach ($response->getAll() as $key => $value) { - $buffer[$key] = $value; + foreach ($response->getAll() as $key => $value) { + $buffer[$key] = $value; + } + + // Exit (success) + return is_array($buffer) && count($buffer) > 1 ? $buffer : $buffer[0] ?? null; + } catch (exception $e) { + // Writing to registry of errors + $errors[] = [ + 'text' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack' => $e->getTrace() + ]; } - return is_array($buffer) && count($buffer) > 1 ? $buffer : $buffer[0] ?? null; + // Exit (fail) + return null; } /** - * ΠžΡ‡ΠΈΡΡ‚ΠΊΠ° + * Truncate * - * @param _connection $session БСссия соСдинСния с Π±Π°Π·ΠΎΠΉ Π΄Π°Π½Π½Ρ‹Ρ… - * @param string $name НазваниС + * @param string $name Name of the collection + * @param array &$errors Registry of errors * * @return bool Бтатус выполнСния */ - public static function truncate(_connection $session, string $name): bool + public static function truncate(string $collection, array &$errors = []): bool { - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ - $collections = new _collection_handler($session); + try { + // Initizlizing of the collection handler + $collections = new _collection_handler(connection::$session); - if ($collections->has($name)) { - // НайдСна коллСкция + if ($collections->has($collection)) { + // Fount the collection - // ΠžΡ‡ΠΈΡΡ‚ΠΊΠ° - return $collections->truncate($name); + // Truncate and exit (success) + return $collections->truncate($collection); + } + } catch (exception $e) { + // Writing to registry of errors + $errors[] = [ + 'text' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack' => $e->getTrace() + ]; } + // Exit (fail) return false; } + + /** + * Count documents in ArangoDB + * + * @param string $collection Name of the collection + * @param array &$errors Registry of errors + * + * @return int|null Amount of documents in ArangoDB + */ + public static function count(string $collection, array &$errors = []): ?int + { + try { + // Count and exit (success) + return static::execute( + <<<'AQL' + RETURN LENGTH(@collection) + AQL, + [ + 'collection' => $collection + ] + ); + } catch (exception $e) { + // Writing to registry of errors + $errors[] = [ + 'text' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack' => $e->getTrace() + ]; + } + + // Exit (fail) + return null; + } } diff --git a/mirzaev/arangodb/system/connection.php b/mirzaev/arangodb/system/connection.php index 6b66e98..a661df6 100755 --- a/mirzaev/arangodb/system/connection.php +++ b/mirzaev/arangodb/system/connection.php @@ -4,201 +4,203 @@ declare(strict_types=1); namespace mirzaev\arangodb; -use ArangoDBClient\Connection as _connection; -use ArangoDBClient\Exception as _exception; -use ArangoDBClient\UpdatePolicy as _update; -use ArangoDBClient\ConnectionOptions as _options; +// Library for ArangoDB +use ArangoDBClient\Connection as _connection, + ArangoDBClient\Exception as _exception, + ArangoDBClient\UpdatePolicy as _update, + ArangoDBClient\ConnectionOptions as _options; +// Built-in libraries use exception; /** - * ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ + * Connection * * @package mirzaev\arangodb + * + * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License * @author Arsen Mirzaev Tatyano-Muradovich */ final class connection { - /** - * АдрСс - */ - protected string $adress = 'tcp://127.0.0.1:8529'; + /** + * АдрСс + */ + protected string $adress = 'tcp://127.0.0.1:8529'; - /** - * Π₯Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅ (Π½Π°Π·Π²Π°Π½ΠΈΠ΅) - */ - protected string $storage = 'root'; + /** + * Π₯Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅ (Π½Π°Π·Π²Π°Π½ΠΈΠ΅) + */ + protected string $storage = 'root'; - /** - * Π’ΠΈΠΏ Π°ΡƒΡ‚Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ - */ - protected string $auth = 'Basic'; + /** + * Π’ΠΈΠΏ Π°ΡƒΡ‚Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ + */ + protected string $auth = 'Basic'; - /** - * ПсСвдоним ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ - */ - protected string $name = 'root'; + /** + * ПсСвдоним ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ + */ + protected string $name = 'root'; - /** - * ΠŸΠ°Ρ€ΠΎΠ»ΡŒ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ - */ - protected string $password = ''; + /** + * ΠŸΠ°Ρ€ΠΎΠ»ΡŒ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ + */ + protected string $password = ''; - /** - * Π’ΠΈΠΏ соСдинСния - */ - protected string $connection = 'Keep-Alive'; + /** + * Π’ΠΈΠΏ соСдинСния + */ + protected string $connection = 'Keep-Alive'; - /** - * ВрСмя оТидания соСдинСния - */ - protected int $timeout_connect = 3; + /** + * ВрСмя оТидания соСдинСния + */ + protected int $timeout_connect = 3; - /** - * ВрСмя оТидания запроса - */ - protected int $timeout_request = 3; + /** + * ВрСмя оТидания запроса + */ + protected int $timeout_request = 3; - /** - * ΠŸΠ΅Ρ€Π΅ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒΡΡ Π»ΠΈ ΠΏΡ€ΠΈ Ρ€Π°Π·Ρ€Ρ‹Π²Π΅ соСдинСния - */ - protected bool $reconnect = true; + /** + * ΠŸΠ΅Ρ€Π΅ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒΡΡ Π»ΠΈ ΠΏΡ€ΠΈ Ρ€Π°Π·Ρ€Ρ‹Π²Π΅ соСдинСния + */ + protected bool $reconnect = true; - /** - * Π‘ΠΎΠ·Π΄Π°Π²Π°Ρ‚ΡŒ Π»ΠΈ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ, Ссли ΠΎΠ½Π° Π½Π΅ сущСствуСт - */ - protected bool $create = true; + /** + * Π‘ΠΎΠ·Π΄Π°Π²Π°Ρ‚ΡŒ Π»ΠΈ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ, Ссли ΠΎΠ½Π° Π½Π΅ сущСствуСт + */ + protected bool $create = true; - /** - * ДСйствия ΠΏΡ€ΠΈ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠΈ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ - */ - protected string $update = _update::LAST; + /** + * ДСйствия ΠΏΡ€ΠΈ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠΈ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ + */ + protected string $update = _update::LAST; - /** - * Активация Турналирования - */ - protected bool $journal = false; + /** + * Активация Турналирования + */ + protected bool $journal = false; - /** - * БСссия соСдинСния - */ - protected _connection $session; + /** + * БСссия соСдинСния + */ + public static _connection $session; - /** - * ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ‚ΠΎΡ€ - * - * @param array $settings Настройки - */ - public function __construct(array $settings = null) - { - // Π—Π°ΠΏΠΈΡΡŒ - @$this->__set('adress', $settings['adress'] ?? $settings['endpoint']); - @$this->__set('storage', $settings['storage'] ?? $settings['database']); - @$this->__set('auth', $settings['auth']); - @$this->__set('name', $settings['name']); - @$this->__set('password', $settings['password']); - @$this->__set('connection', $settings['connection']); - @$this->__set('timeout_connect', $settings['timeout_connect']); - @$this->__set('timeout_request', $settings['timeout_request']); - @$this->__set('reconnect', $settings['reconnect']); - @$this->__set('create', $settings['create']); - @$this->__set('update', $settings['update'] ?? $settings['policy']); - @$this->__set('journal', $settings['journal']); + /** + * ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ‚ΠΎΡ€ + * + * @param array $settings Настройки + */ + public function __construct(array $settings = null) + { + // Π—Π°ΠΏΠΈΡΡŒ + @$this->__set('adress', $settings['adress'] ?? $settings['endpoint']); + @$this->__set('storage', $settings['storage'] ?? $settings['database']); + @$this->__set('auth', $settings['auth']); + @$this->__set('name', $settings['name']); + @$this->__set('password', $settings['password']); + @$this->__set('connection', $settings['connection']); + @$this->__set('timeout_connect', $settings['timeout_connect']); + @$this->__set('timeout_request', $settings['timeout_request']); + @$this->__set('reconnect', $settings['reconnect']); + @$this->__set('create', $settings['create']); + @$this->__set('update', $settings['update'] ?? $settings['policy']); + @$this->__set('journal', $settings['journal']); - if ($this->journal) { - // Запрос Π½Π° Π°ΠΊΡ‚ΠΈΠ²Π°Ρ†ΠΈΡŽ Турналирования + if ($this->journal) { + // Запрос Π½Π° Π°ΠΊΡ‚ΠΈΠ²Π°Ρ†ΠΈΡŽ Турналирования - _exception::enableLogging(); - } + _exception::enableLogging(); + } - // ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ - $this->session = new _connection([ - _options::OPTION_ENDPOINT => $this->adress, - _options::OPTION_DATABASE => $this->storage, - _options::OPTION_AUTH_TYPE => $this->auth, - _options::OPTION_AUTH_USER => $this->name, - _options::OPTION_AUTH_PASSWD => $this->password, - _options::OPTION_CONNECTION => $this->connection, - _options::OPTION_CONNECT_TIMEOUT => $this->timeout_connect, - _options::OPTION_REQUEST_TIMEOUT => $this->timeout_request, - _options::OPTION_RECONNECT => $this->reconnect, - _options::OPTION_CREATE => $this->create, - _options::OPTION_UPDATE_POLICY => $this->update, - ]); - } + // ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ + static::$session = new _connection([ + _options::OPTION_ENDPOINT => $this->adress, + _options::OPTION_DATABASE => $this->storage, + _options::OPTION_AUTH_TYPE => $this->auth, + _options::OPTION_AUTH_USER => $this->name, + _options::OPTION_AUTH_PASSWD => $this->password, + _options::OPTION_CONNECTION => $this->connection, + _options::OPTION_CONNECT_TIMEOUT => $this->timeout_connect, + _options::OPTION_REQUEST_TIMEOUT => $this->timeout_request, + _options::OPTION_RECONNECT => $this->reconnect, + _options::OPTION_CREATE => $this->create, + _options::OPTION_UPDATE_POLICY => $this->update, + ]); + } - /** - * Π—Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ свойство - * - * @param mixed $name НазваниС - * @param mixed $value Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ - */ - public function __set(string $name, mixed $value): void - { - match ($name) { - 'adress', 'endpoint' => $this->adress = $value ?? throw new exception("Бвойство \"$name\" Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ пустым", 500), - 'storage', 'database', 'db' => $this->storage = $value ?? throw new exception("Бвойство \"$name\" Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ пустым", 500), - 'auth' => !$value ?: $this->auth = $value, - 'name' => !$value ?: $this->name = $value, - 'password' => !$value ?: $this->password = $value, - 'connection' => !$value ?: $this->connection = $value, - 'timeout_connect' => !$value ?: $this->timeout_connect = $value, - 'timeout_request' => !$value ?: $this->timeout_request = $value, - 'reconnect' => !$value ?: $this->reconnect = $value, - 'create' => !$value ?: $this->create = $value, - 'update', 'policy' => !$value ?: $this->update = $value, - 'journal' => !$value ?: $this->journal = $value, - default => throw new exception("Бвойство \"$name\" Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ", 404) - }; - } + /** + * Π—Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ свойство + * + * @param mixed $name НазваниС + * @param mixed $value Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ + */ + public function __set(string $name, mixed $value): void + { + match ($name) { + 'adress', 'endpoint' => $this->adress = $value ?? throw new exception("Бвойство \"$name\" Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ пустым", 500), + 'storage', 'database', 'db' => $this->storage = $value ?? throw new exception("Бвойство \"$name\" Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ пустым", 500), + 'auth' => !$value ?: $this->auth = $value, + 'name' => !$value ?: $this->name = $value, + 'password' => !$value ?: $this->password = $value, + 'connection' => !$value ?: $this->connection = $value, + 'timeout_connect' => !$value ?: $this->timeout_connect = $value, + 'timeout_request' => !$value ?: $this->timeout_request = $value, + 'reconnect' => !$value ?: $this->reconnect = $value, + 'create' => !$value ?: $this->create = $value, + 'update', 'policy' => !$value ?: $this->update = $value, + 'journal' => !$value ?: $this->journal = $value, + default => throw new exception("Бвойство \"$name\" Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ", 404) + }; + } - /** - * ΠŸΡ€ΠΎΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ свойство - * - * @param mixed $name НазваниС - */ - public function __get(string $name): mixed - { - return match ($name) { - 'adress', 'endpoint' => $this->adress, - 'storage', 'database', 'db' => $this->storage, - 'auth' => $this->auth, - 'name' => $this->name, - 'password' => $this->password, - 'connection' => $this->connection, - 'timeout_connect' => $this->timeout_connect, - 'timeout_request' => $this->timeout_request, - 'reconnect' => $this->reconnect, - 'create' => $this->create, - 'update', 'policy' => $this->update, - 'session' => $this->session, - 'journal' => $this->journal, - default => throw new exception("Бвойство \"$name\" Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ", 404) - }; - } + /** + * ΠŸΡ€ΠΎΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ свойство + * + * @param mixed $name НазваниС + */ + public function __get(string $name): mixed + { + return match ($name) { + 'adress', 'endpoint' => $this->adress, + 'storage', 'database', 'db' => $this->storage, + 'auth' => $this->auth, + 'name' => $this->name, + 'password' => $this->password, + 'connection' => $this->connection, + 'timeout_connect' => $this->timeout_connect, + 'timeout_request' => $this->timeout_request, + 'reconnect' => $this->reconnect, + 'create' => $this->create, + 'update', 'policy' => $this->update, + 'journal' => $this->journal, + default => throw new exception("Бвойство \"$name\" Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ", 404) + }; + } - /** - * ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ свойство Π½Π° ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΡΡ‚ΡŒ - * - * @param mixed $name НазваниС - */ - public function __isset(string $name): bool - { - return match ($name) { - 'adress', 'endpoint' => isset($this->adress), - 'storage', 'database', 'db' => isset($this->storage), - 'auth' => isset($this->auth), - 'name' => isset($this->name), - 'password' => isset($this->password), - 'connection' => isset($this->connection), - 'timeout_connect' => isset($this->timeout_connect), - 'timeout_request' => isset($this->timeout_request), - 'reconnect' => isset($this->reconnect), - 'create' => isset($this->create), - 'update', 'policy' => isset($this->update), - 'session' => isset($this->session), - 'journal' => isset($this->journal), - default => throw new exception("Бвойство \"$name\" Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ", 404) - }; - } + /** + * ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ свойство Π½Π° ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΡΡ‚ΡŒ + * + * @param mixed $name НазваниС + */ + public function __isset(string $name): bool + { + return match ($name) { + 'adress', 'endpoint' => isset($this->adress), + 'storage', 'database', 'db' => isset($this->storage), + 'auth' => isset($this->auth), + 'name' => isset($this->name), + 'password' => isset($this->password), + 'connection' => isset($this->connection), + 'timeout_connect' => isset($this->timeout_connect), + 'timeout_request' => isset($this->timeout_request), + 'reconnect' => isset($this->reconnect), + 'create' => isset($this->create), + 'update', 'policy' => isset($this->update), + 'journal' => isset($this->journal), + default => throw new exception("Бвойство \"$name\" Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ", 404) + }; + } } diff --git a/mirzaev/arangodb/system/document.php b/mirzaev/arangodb/system/document.php index e1801c6..193f119 100755 --- a/mirzaev/arangodb/system/document.php +++ b/mirzaev/arangodb/system/document.php @@ -4,111 +4,186 @@ declare(strict_types=1); namespace mirzaev\arangodb; -use mirzaev\arangodb\terminal; +// Files of the project +use mirzaev\arangodb\terminal, + mirzaev\arangodb\connection, + mirzaev\arangodb\enumerations\collection\type; -use ArangoDBClient\Connection as _connection; -use ArangoDBClient\Document as _document; -use ArangoDBClient\Edge as _edge; -use ArangoDBClient\DocumentHandler as _document_handler; -use ArangoDBClient\EdgeHandler as _edge_handler; +// Librsry for ArangoDB +use ArangoDBClient\Connection as _connection, + ArangoDBClient\Document as _document, + ArangoDBClient\Edge as _edge, + ArangoDBClient\DocumentHandler as _document_handler, + ArangoDBClient\EdgeHandler as _edge_handler; + +// Built-in libraries +use exception; /** - * Π”ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ + * Document + * + * Handlers of document in ArangoDB * * @package mirzaev\arangodb + * + * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License * @author Arsen Mirzaev Tatyano-Muradovich */ class document { /** - * Π—Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ + * Write * - * @param _connection $session БСссия соСдинСния с Π±Π°Π·ΠΎΠΉ Π΄Π°Π½Π½Ρ‹Ρ… * @param string $collection ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ * @param ?array $data Π”Π°Π½Π½Ρ‹Π΅ * @param bool $check ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π½Π° запись Π² Π±Π°Π·Ρƒ Π΄Π°Π½Π½Ρ‹Ρ… - * @param ?terminal $terminal Π˜Π½ΡΡ‚Π°Π½Ρ†ΠΈΡ Ρ‚Π΅Ρ€ΠΌΠΈΠ½Π°Π»Π° + * @param ?terminal $terminal Instance of the terminal + * @param array &$errors Registry of errors * * @return string|null Π˜Π΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ + * + * @todo + * 1. Bind parameters */ - public static function write(_connection $session, string $collection, ?array $data = [], bool $check = false, ?terminal $terminal = null): ?string - { - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ - collection::init($session, $collection, isset($data['_from'], $data['_to'])); + public static function write( + string $collection, + ?array $data = [], + bool $check = false, + ?terminal $terminal = null, + array &$errors = [] + ): ?string { + try { + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ + collection::initialize($collection, isset($data['_from'], $data['_to']) ? type::edge : type::document); - if (isset($data['_from'], $data['_to'])) { - // Π Π΅Π±Ρ€ΠΎ + if (isset($data['_from'], $data['_to'])) { + // Π Π΅Π±Ρ€ΠΎ - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° Ρ€Ρ‘Π±Π΅Ρ€ - $documents = new _edge_handler($session); + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° Ρ€Ρ‘Π±Π΅Ρ€ + $documents = new _edge_handler(connection::$session); - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Ρ€Π΅Π±Ρ€Π° - $document = new _edge(); + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Ρ€Π΅Π±Ρ€Π° + $document = new _edge(); - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π²Π΅Ρ€ΡˆΠΈΠ½ - $_from = $data['_from']; - $_to = $data['_to']; + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π²Π΅Ρ€ΡˆΠΈΠ½ + $_from = $data['_from']; + $_to = $data['_to']; - // ДСинициализация ΠΈΠ· Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… - unset($data['_from'], $data['_to']); - } else { - // Π’Π΅Ρ€ΡˆΠΈΠ½Π° + // ДСинициализация ΠΈΠ· Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… + unset($data['_from'], $data['_to']); + } else { + // Π’Π΅Ρ€ΡˆΠΈΠ½Π° - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° Π²Π΅Ρ€ΡˆΠΈΠ½ - $documents = new _document_handler($session); + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° Π²Π΅Ρ€ΡˆΠΈΠ½ + $documents = new _document_handler(connection::$session); - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π²Π΅Ρ€ΡˆΠΈΠ½Ρ‹ - $document = new _document(); + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π²Π΅Ρ€ΡˆΠΈΠ½Ρ‹ + $document = new _document(); + } + + // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π΄Π°Ρ‚Ρ‹ создания + $created = time(); + + foreach (['created' => $created, 'updated' => $created] + $data as $key => $value) { + // ΠŸΠ΅Ρ€Π΅Π±ΠΎΡ€ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ² + + // Π—Π°ΠΏΠΈΡΡŒ Π² ΠΈΠ½ΡΡ‚Π°Π½Ρ†ΠΈΡŽ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° + $document->set($key, $value); + } + + // Π—Π°ΠΏΠΈΡΡŒ Π½Π° сСрвСр ΠΈ Π΅Π³ΠΎ ΠΎΡ‚Π²Π΅Ρ‚ Π² Π±ΡƒΡ„Π΅Ρ€ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π° + $id = isset($_from, $_to) ? $documents->saveEdge($collection, $_from, $_to, $document) : $documents->insert($collection, $document); + + if ($check && $documents->has($collection, $id)) { + // НайдСн записанный Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ + + // Π—Π°ΠΏΠΈΡΡŒ Π² Π²Ρ‹Π²ΠΎΠ΄ + if ($terminal instanceof terminal) $terminal::write("Π’ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ \"$collection\" создан Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ \"$id\""); + } else if ($check) { + // НС Π½Π°ΠΉΠ΄Π΅Π½ записанный Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ + + // Π—Π°ΠΏΠΈΡΡŒ Π² Π²Ρ‹Π²ΠΎΠ΄ + if ($terminal instanceof terminal) $terminal::write("Π’ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ \"$collection\" Π½Π΅ ΡƒΠ΄Π°Π»ΠΎΡΡŒ Π½Π°ΠΉΡ‚ΠΈ созданный ΠΈΠ»ΠΈ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚"); + + return null; + } + + // Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€Π° ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ + return $id; + } catch (exception $e) { + // Writing to registry of errors + $errors[] = [ + 'text' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack' => $e->getTrace() + ]; } - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π΄Π°Ρ‚Ρ‹ создания - $created = time(); - - foreach (['created' => $created, 'updated' => $created] + $data as $key => $value) { - // ΠŸΠ΅Ρ€Π΅Π±ΠΎΡ€ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ² - - // Π—Π°ΠΏΠΈΡΡŒ Π² ΠΈΠ½ΡΡ‚Π°Π½Ρ†ΠΈΡŽ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° - $document->set($key, $value); - } - - // Π—Π°ΠΏΠΈΡΡŒ Π½Π° сСрвСр ΠΈ Π΅Π³ΠΎ ΠΎΡ‚Π²Π΅Ρ‚ Π² Π±ΡƒΡ„Π΅Ρ€ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π° - $id = isset($_from, $_to) ? $documents->saveEdge($collection, $_from, $_to, $document) : $documents->insert($collection, $document); - - if ($check && $documents->has($collection, $id)) { - // НайдСн записанный Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ - - // Π—Π°ΠΏΠΈΡΡŒ Π² Π²Ρ‹Π²ΠΎΠ΄ - if ($terminal instanceof terminal) $terminal::write("Π’ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ \"$collection\" создан Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ \"$id\""); - } else if ($check) { - // НС Π½Π°ΠΉΠ΄Π΅Π½ записанный Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ - - // Π—Π°ΠΏΠΈΡΡŒ Π² Π²Ρ‹Π²ΠΎΠ΄ - if ($terminal instanceof terminal) $terminal::write("Π’ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ \"$collection\" Π½Π΅ ΡƒΠ΄Π°Π»ΠΎΡΡŒ Π½Π°ΠΉΡ‚ΠΈ созданный ΠΈΠ»ΠΈ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚"); - - return null; - } - - // Π’ΠΎΠ·Π²Ρ€Π°Ρ‚ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€Π° ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ - return $id; + // Exit (fail) + return false; } /** - * ΠžΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ + * Update * - * @param _connection $session БСссия соСдинСния с Π±Π°Π·ΠΎΠΉ Π΄Π°Π½Π½Ρ‹Ρ… - * @param _document $document Π˜Π½ΡΡ‚Π°Π½Ρ†ΠΈΡ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° Π²Π΅Ρ€ΡˆΠΈΠ½Ρ‹ + * @param array &$errors Registry of errors + * @param _document $document Instance of the document * - * @return bool Бтатус ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ + * @return bool Has the document been updated? */ - public static function update(_connection $session, _document $document): bool + public static function update(_document $document, array &$errors = []): bool { - // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° Π²Π΅Ρ€ΡˆΠΈΠ½ - $documents = new _document_handler($session); + try { + // Initialize the handler of documents + $documents = new _document_handler(connection::$session); - // Π—Π°ΠΏΠΈΡΡŒ Π΄Π°Ρ‚Ρ‹ обновлСния Π² ΠΈΠ½ΡΡ‚Π°Π½Ρ†ΠΈΡŽ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π° - $document->set('updated', time()); + // Writing date of the updating to the updating document + $document->set('updated', time()); - // Π—Π°ΠΏΠΈΡΡŒ Π² Π±Π°Π·Ρƒ Π΄Π°Π½Π½Ρ‹Ρ… - return $documents->update($document); + // Writing and exit (success) + return $documents->update($document); + } catch (exception $e) { + // Writing to registry of errors + $errors[] = [ + 'text' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack' => $e->getTrace() + ]; + } + + // Exit (fail) + return false; + } + + /** + * Delete + * + * @param _document $document Instance of the document + * @param array &$errors Registry of errors + * + * @return bool Has the document been deleted? + */ + public static function delete(_document $document, array &$errors = []): bool + { + try { + // Initialize the handler of documents + $documents = new _document_handler(connection::$session); + + // Delete abd exit (success) + return $documents->remove($document); + } catch (exception $e) { + // Writing to registry of errors + $errors[] = [ + 'text' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack' => $e->getTrace() + ]; + } + + // Exit (fail) + return false; } } diff --git a/mirzaev/arangodb/system/enumerations/collection/type.php b/mirzaev/arangodb/system/enumerations/collection/type.php new file mode 100644 index 0000000..0e8e2e1 --- /dev/null +++ b/mirzaev/arangodb/system/enumerations/collection/type.php @@ -0,0 +1,36 @@ + + */ +enum type +{ + case document; + case edge; + + /** + * Read code of the type from library for ArangoDB + * + * @return int Code of the type + */ + public function code(): int + { + // Exit (success) + return match ($this) { + type::document => _collection::TYPE_DOCUMENT, + type::edge => _collection::TYPE_EDGE + }; + } +} diff --git a/mirzaev/arangodb/system/terminal.php b/mirzaev/arangodb/system/terminal.php index 16961e1..96fb4c8 100755 --- a/mirzaev/arangodb/system/terminal.php +++ b/mirzaev/arangodb/system/terminal.php @@ -4,12 +4,26 @@ declare(strict_types=1); namespace mirzaev\arangodb; +// Built-in libraries +use exception; + +/** + * Terminal + * + * lol sorry + * i did not want to do this + * + * @package mirzaev\arangodb + * + * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License + * @author Arsen Mirzaev Tatyano-Muradovich + */ class terminal { /** * ΠŸΡ€Π΅Ρ„ΠΈΠΊΡ */ - protected const PREFIX = 'arangodb'; + protected const string PREFIX = 'arangodb'; /** * Π—Π°ΠΏΠΈΡΡŒ Π² Π²Ρ‹Π²ΠΎΠ΄