2024-01-11 04:35:40 +07:00
< ? php
declare ( strict_types = 1 );
2024-01-11 05:38:30 +07:00
namespace $ { REPO_OWNER } \ $ { REPO_NAME } \models ;
2024-01-11 04:35:40 +07:00
// Framework for PHP
2024-12-15 04:20:44 +07:00
use mirzaev\minimal\model ,
mirzaev\minimal\http\enumerations\status ;
2024-01-11 04:35:40 +07:00
// Framework for ArangoDB
use mirzaev\arangodb\connection as arangodb ,
mirzaev\arangodb\collection ,
mirzaev\arangodb\document ;
2024-12-15 04:20:44 +07:00
// Library for ArangoDB
2024-01-11 04:35:40 +07:00
use ArangoDBClient\Document as _document ,
ArangoDBClient\DocumentHandler as _document_handler ;
// Built-in libraries
use exception ;
/**
2024-12-15 04:20:44 +07:00
* Models core
2024-01-11 04:35:40 +07:00
*
2024-12-15 04:20:44 +07:00
* @ package $ { REPO_OWNER } \ $ { REPO_NAME } \models
*
* @ param public ARANGODB Path to the file with ArangoDB session connection data
2024-12-15 22:16:01 +07:00
* @ param arangodb $$arangodb Instance of the ArangoDB session
2024-12-15 04:20:44 +07:00
*
2024-12-15 22:16:01 +07:00
* @ method void __construct ( bool $$initialize , ? arangodb $$arangodb ) Constructor
* @ method _document | static | array | null read ( string $$filter , string $$sort , int $$amount , int $$page , string $$return , array $$parameters , array & $$errors ) Read document from ArangoDB
2024-12-15 04:20:44 +07:00
*
* @ license http :// www . wtfpl . net / Do What The Fuck You Want To Public License
* @ author $ { REPO_OWNER } < mail @ domain . zone >
2024-01-11 04:35:40 +07:00
*/
class core extends model
{
/**
2024-12-15 04:20:44 +07:00
* ArangoDB connection daa
*
* @ var string ARANGODB Path to the file with ArangoDB session connection data
2024-01-11 04:35:40 +07:00
*/
2024-12-15 04:20:44 +07:00
final public const string ARANGODB = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings' . DIRECTORY_SEPARATOR . 'arangodb.php' ;
2024-01-11 04:35:40 +07:00
/**
2024-12-15 04:20:44 +07:00
* ArangoDB
*
2024-12-15 22:16:01 +07:00
* @ var arangodb $$arangodb Instance of the ArangoDB session
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +07:00
protected static arangodb $$arangodb ;
2024-01-11 04:35:40 +07:00
/**
2024-12-15 04:20:44 +07:00
* Constructor
2024-01-11 04:35:40 +07:00
*
2024-12-15 22:16:01 +07:00
* @ param bool $$initialize Initialize a model ?
* @ param ? arangodb $$arangodb Instance of the ArangoDB session
2024-01-11 04:35:40 +07:00
*
* @ return void
*/
2024-12-15 22:16:01 +07:00
public function __construct ( bool $$initialize = true , ? arangodb $$arangodb = null )
2024-01-11 04:35:40 +07:00
{
// For the extends system
2024-12-15 22:16:01 +07:00
parent :: __construct ( $$initialize );
2024-01-11 04:35:40 +07:00
2024-12-15 22:16:01 +07:00
if ( $$initialize ) {
2024-01-11 04:35:40 +07:00
// Initializing is requested
2024-12-15 04:20:44 +07:00
// Writing an instance of a session of ArangoDB to the property
2024-12-15 22:16:01 +07:00
self :: $$arangodb = $$arangodb ? ? new arangodb ( require static :: ARANGODB );
2024-01-11 04:35:40 +07:00
}
}
/**
2024-12-15 04:20:44 +07:00
* Read document from ArangoDB
2024-01-11 04:35:40 +07:00
*
2024-12-15 22:16:01 +07:00
* @ param string $$filter Expression for filtering ( AQL )
* @ param string $$sort Expression for sorting ( AQL )
* @ param int $$amount Amount of documents for collect
* @ param int $$page Page
* @ param string $$return Expression describing the parameters to return ( AQL )
* @ param array $$parameters Binded parameters for placeholders [ 'placeholder' => parameter ]
* @ param array & $$errors Registry of errors
2024-01-11 04:35:40 +07:00
*
2024-12-15 04:20:44 +07:00
* @ return mixed An array of instances of documents from ArangoDB , if they are found
2024-01-11 04:35:40 +07:00
*/
2024-12-15 04:20:44 +07:00
public static function _read (
2024-12-15 22:16:01 +07:00
string $$filter = '' ,
string $$sort = 'd.created DESC, d._key DESC' ,
int $$amount = 1 ,
int $$page = 1 ,
string $$return = 'd' ,
array $$parameters = [],
array & $$errors = []
2024-12-15 04:20:44 +07:00
) : _document | static | array | null {
2024-01-11 04:35:40 +07:00
try {
2024-12-15 04:20:44 +07:00
if ( collection :: initialize ( static :: COLLECTION , static :: TYPE )) {
2024-01-11 04:35:40 +07:00
// Initialized the collection
2024-12-15 04:20:44 +07:00
// Read from ArangoDB
2024-12-15 22:16:01 +07:00
$$result = collection :: execute (
2024-01-11 04:35:40 +07:00
sprintf (
<<< 'AQL'
2024-12-15 04:20:44 +07:00
FOR d IN @@ collection
2024-01-11 04:35:40 +07:00
% s
% s
2024-12-15 04:20:44 +07:00
LIMIT @ offset , @ amount
2024-01-11 04:35:40 +07:00
RETURN % s
AQL ,
2024-12-15 22:16:01 +07:00
empty ( $$filter ) ? '' : " FILTER $ $filter " ,
empty ( $$sort ) ? '' : " SORT $ $sort " ,
empty ( $$return ) ? 'd' : $$return
2024-12-15 04:20:44 +07:00
),
[
'@collection' => static :: COLLECTION ,
2024-12-15 22:16:01 +07:00
'offset' => -- $$page <= 0 ? 0 : $$page * $$amount ,
'amount' => $$amount
] + $$parameters ,
errors : $$errors
2024-01-11 04:35:40 +07:00
);
2024-12-15 22:16:01 +07:00
if ( $$amount === 1 && $$result instanceof _document ) {
2024-12-15 04:20:44 +07:00
// Received only 1 document and @todo rebuild
2024-01-11 04:35:40 +07:00
2024-12-15 04:20:44 +07:00
// Initializing the object
2024-12-15 22:16:01 +07:00
$$object = new static ;
2024-12-15 04:20:44 +07:00
2024-12-15 22:16:01 +07:00
if ( method_exists ( $$object , '__document' )) {
2024-12-15 04:20:44 +07:00
// Object can implement a document from ArangoDB
// Writing the instance of document from ArangoDB to the implement object
2024-12-15 22:16:01 +07:00
$$object -> __document ( $$result );
2024-12-15 04:20:44 +07:00
// Exit (success)
2024-12-15 22:16:01 +07:00
return $$object ;
2024-12-15 04:20:44 +07:00
}
}
2024-01-11 04:35:40 +07:00
2024-12-15 04:20:44 +07:00
// Exit (success)
2024-12-15 22:16:01 +07:00
return $$result ;
2024-12-15 04:20:44 +07:00
} else throw new exception ( 'Failed to initialize ' . static :: TYPE . ' collection: ' . static :: COLLECTION );
2024-12-15 22:16:01 +07:00
} catch ( exception $$e ) {
2024-12-15 04:20:44 +07:00
// Writing to registry of errors
2024-12-15 22:16:01 +07:00
$$errors [] = [
'text' => $$e -> getMessage (),
'file' => $$e -> getFile (),
'line' => $$e -> getLine (),
'stack' => $$e -> getTrace ()
2024-01-11 04:35:40 +07:00
];
}
// Exit (fail)
2024-12-15 04:20:44 +07:00
return null ;
2024-01-11 04:35:40 +07:00
}
/**
* Write
*
2024-12-15 22:16:01 +07:00
* @ param string $$name Name of the property
* @ param mixed $$value Value of the property
2024-01-11 04:35:40 +07:00
*
* @ return void
*/
2024-12-15 22:16:01 +07:00
public function __set ( string $$name , mixed $$value = null ) : void
2024-01-11 04:35:40 +07:00
{
2024-12-15 22:16:01 +07:00
match ( $$name ) {
'arangodb' => ( function () use ( $$value ) {
if ( isset ( static :: $$arangodb )) throw new exception ( 'Forbidden to reinitialize the ArangoDB session($$this::$$arangodb)' , status :: internal_server_error -> value );
else if ( $$value instanceof arangodb ) self :: $$arangodb = $$value ;
else throw new exception ( 'Session of connection to ArangoDB ($$this::$$arangodb) is need to be mirzaev\arangodb\connection' , status :: internal_server_error -> value );
2024-01-11 04:35:40 +07:00
})(),
2024-12-15 22:16:01 +07:00
default => parent :: __set ( $$name , $$value )
2024-01-11 04:35:40 +07:00
};
}
/**
* Read
*
2024-12-15 22:16:01 +07:00
* @ param string $$name Name of the property
2024-01-11 04:35:40 +07:00
*
* @ return mixed Content of the property , if they are found
*/
2024-12-15 22:16:01 +07:00
public function __get ( string $$name ) : mixed
2024-01-11 04:35:40 +07:00
{
2024-12-15 22:16:01 +07:00
return match ( $$name ) {
default => parent :: __get ( $$name )
2024-01-11 04:35:40 +07:00
};
}
/**
* Delete
*
2024-12-15 22:16:01 +07:00
* @ param string $$name Name of the property
2024-01-11 04:35:40 +07:00
*
* @ return void
*/
2024-12-15 22:16:01 +07:00
public function __unset ( string $$name ) : void
2024-01-11 04:35:40 +07:00
{
// Deleting a property and exit (success)
2024-12-15 22:16:01 +07:00
parent :: __unset ( $$name );
2024-01-11 04:35:40 +07:00
}
/**
* Check of initialization
*
2024-12-15 22:16:01 +07:00
* @ param string $$name Name of the property
2024-01-11 04:35:40 +07:00
*
* @ return bool The property is initialized ?
*/
2024-12-15 22:16:01 +07:00
public function __isset ( string $$name ) : bool
2024-01-11 04:35:40 +07:00
{
// Check of initialization of the property and exit (success)
2024-12-15 22:16:01 +07:00
return parent :: __isset ( $$name );
2024-01-11 04:35:40 +07:00
}
/**
* Call a static property or method
*
2024-12-15 22:16:01 +07:00
* @ param string $$name Name of the property or the method
* @ param array $$arguments Arguments for the method
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +07:00
public static function __callStatic ( string $$name , array $$arguments ) : mixed
2024-01-11 04:35:40 +07:00
{
2024-12-15 22:16:01 +07:00
return match ( $$name ) {
2024-01-11 04:35:40 +07:00
'arangodb' => ( new static ) -> __get ( 'arangodb' ),
2024-12-15 22:16:01 +07:00
default => throw new exception ( " Not found: $ $name " , 500 )
2024-01-11 04:35:40 +07:00
};
}
}