Skip to content
Snippets Groups Projects
Commit 56dac374 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

sorm configuration for db table, relationships and callbacks

parent 3a9652b2
No related branches found
No related tags found
No related merge requests found
Pipeline #6325 passed
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
{ {
use SimpleORMapConfigurationTrait;
/** /**
* Defines `_` as character used when joining composite primary keys. * Defines `_` as character used when joining composite primary keys.
*/ */
......
<?php
trait SimpleORMapConfigurationTrait
{
protected static function configureDbTable(array &$config, string $db_table)
{
$config['db_table'] = $db_table;
}
// Relationships
protected static function configureBelongsTo(array &$config, string $relation, string $class_name, array $options = [])
{
self::configureRelation($config, 'belongs_to', $relation, $class_name, $options);
}
protected static function configureHasOne(array &$config, string $relation, string $class_name, array $options = [])
{
self::configureRelation($config, 'has_one', $relation, $class_name, $options);
}
protected static function configureHasMany(array &$config, string $relation, string $class_name, array $options = [])
{
self::configureRelation($config, 'has_many', $relation, $class_name, $options);
}
protected static function configureHasAndBelongsToMany(array &$config, string $relation, string $class_name, array $options = [])
{
self::configureRelation($config, 'has_and_belongs_to_many', $relation, $class_name, $options);
}
private static function configureRelation(array &$config, string $relation_type, string $relation, string $class_name, array $options = [])
{
if (!self::validateRelationOptions($relation_type, $options)) {
throw new Exception('Options for relation are invalid');
}
if (!isset($config[$relation_type])) {
$config[$relation_type] = [];
}
$config[$relation_type][$relation] = array_merge(compact('class_name'), $options);
}
private static function validateRelationOptions(string $relation_type, array $options): bool
{
return true;
}
// Callbacks
protected static function registerBeforeCreateCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'before_create', $callback, $prepend);
}
protected static function registerAfterCreateCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'after_create', $callback, $prepend);
}
protected static function registerBeforeUpdateCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'before_update', $callback, $prepend);
}
protected static function registerAfterUpdateCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'after_update', $callback, $prepend);
}
protected static function registerBeforeStoreCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'before_store', $callback, $prepend);
}
protected static function registerAfterStoreCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'after_store', $callback, $prepend);
}
protected static function registerBeforeDeleteCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'before_delete', $callback, $prepend);
}
protected static function registerAfterDeleteCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'after_delete', $callback, $prepend);
}
protected static function registerBeforeInitializeCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'before_initialize', $callback, $prepend);
}
protected static function registerAfterInitializeCallback(array &$config, $callback, bool $prepend = false)
{
self::registerCallback($config, 'after_initialize', $callback, $prepend);
}
protected static function registerCallback(array &$config, string $callback_type, $callback, bool $prepend = false)
{
if (!self::validateCallback($callback)) {
throw new Exception('Callback is invalid');
}
if (!isset($config['registered_callbacks'])) {
$config['registered_callbacks'] = [];
}
if (!isset($config['registered_callbacks'][$callback_type])) {
$config['registered_callbacks'][$callback_type] = [];
}
if ($prepend) {
array_unshift($config['registered_callbacks'][$callback_type], $callback);
} else {
$config['registered_callbacks'][$callback_type][] = $callback;
}
}
private static function validateCallback($callback): bool
{
return true;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment