From 56dac3746c3d4d2a26cead6aef964723b3947edb Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Willms <tleilax+github@gmail.com> Date: Fri, 6 May 2022 11:58:31 +0200 Subject: [PATCH] sorm configuration for db table, relationships and callbacks --- lib/models/SimpleORMap.class.php | 2 + lib/models/SimpleORMapConfigurationTrait.php | 124 +++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 lib/models/SimpleORMapConfigurationTrait.php diff --git a/lib/models/SimpleORMap.class.php b/lib/models/SimpleORMap.class.php index 4bce0cf0540..ab00b3d178b 100644 --- a/lib/models/SimpleORMap.class.php +++ b/lib/models/SimpleORMap.class.php @@ -16,6 +16,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate { + use SimpleORMapConfigurationTrait; + /** * Defines `_` as character used when joining composite primary keys. */ diff --git a/lib/models/SimpleORMapConfigurationTrait.php b/lib/models/SimpleORMapConfigurationTrait.php new file mode 100644 index 00000000000..675752a1ce1 --- /dev/null +++ b/lib/models/SimpleORMapConfigurationTrait.php @@ -0,0 +1,124 @@ +<?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; + } +} -- GitLab