From 9eaf34bc9fc388d9628842d9fb11f7a9abab57d8 Mon Sep 17 00:00:00 2001 From: Manuel Canga Date: Mon, 16 Sep 2024 16:58:04 +0200 Subject: [PATCH] refactor: simpler metabox factory --- trunk/config/display-metadata.conf.php | 1 + trunk/config/services.conf.php | 15 ++-- trunk/src/Dto/class-id-url-params.php | 65 ++++++++++++++++ trunk/src/Helper/class-metabox-factory.php | 58 +++----------- .../Iterator/class-metabox-types-iterator.php | 77 +++++++++++++++++++ 5 files changed, 162 insertions(+), 54 deletions(-) create mode 100644 trunk/src/Dto/class-id-url-params.php create mode 100644 trunk/src/Iterator/class-metabox-types-iterator.php diff --git a/trunk/config/display-metadata.conf.php b/trunk/config/display-metadata.conf.php index 382d9f4..336d664 100644 --- a/trunk/config/display-metadata.conf.php +++ b/trunk/config/display-metadata.conf.php @@ -14,6 +14,7 @@ 'assets' => [ 'subpath' => '/assets', ], + 'default-metabox' => Type\None::class, 'metabox-types' => [ 'post' => [ 'type' => Type\Post::class, diff --git a/trunk/config/services.conf.php b/trunk/config/services.conf.php index f25d719..fd43099 100644 --- a/trunk/config/services.conf.php +++ b/trunk/config/services.conf.php @@ -5,8 +5,10 @@ use Trasweb\Autoload; use Trasweb\Parser; use Trasweb\Plugins\DisplayMetadata\Display_Metadata as Plugin; +use Trasweb\Plugins\DisplayMetadata\Dto\Id_Url_Params; use Trasweb\Plugins\DisplayMetadata\Helper\Metabox_Factory; use Trasweb\Plugins\DisplayMetadata\Helper\Metabox_View; +use Trasweb\Plugins\DisplayMetadata\Iterator\Metabox_Types_Iterator; use Trasweb\Plugins\DisplayMetadata\UserCase\Register_Metabox; use Trasweb\Screen; @@ -41,16 +43,13 @@ ), 'metabox_factory' => static function (): Metabox_Factory { - $screen_vars = $_GET ?: []; - - // fix profile admin pages screen vars - if (defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE) { - $screen_vars['user_id'] = get_current_user_id(); - } + $metabox_types_config = Plugin::config('metabox-types'); + $default_metabox = Plugin::Config('default-metabox'); + $metabox_types = new Metabox_Types_Iterator($metabox_types_config, $default_metabox); return new Metabox_Factory( - $screen_vars, - Plugin::config('metabox-types'), + Id_Url_Params::create_from_globals(), + $metabox_types, ); }, diff --git a/trunk/src/Dto/class-id-url-params.php b/trunk/src/Dto/class-id-url-params.php new file mode 100644 index 0000000..7ab250e --- /dev/null +++ b/trunk/src/Dto/class-id-url-params.php @@ -0,0 +1,65 @@ + $id_params + */ + private array $id_params; + + public function __construct(array $params) + { + $this->id_params = $this->check_params($params); + } + + /** + * Url vars should be numeric (they, which we use, are ids) + * + * @param array $url_params + * + * @return array + */ + private function check_params(array $url_params): array + { + $screen_var_checker = fn($item_id) => (int)($item_id ?? 0); + + return array_filter(array_map($screen_var_checker, $url_params)); + } + + /** + * Named method in order to build params from global vars (like GET). + * ) + * @return static + */ + public static function create_from_globals(): static + { + $params = $_GET ?? []; + + if (defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE) { + $params['user_id'] = get_current_user_id(); + } + + return new static($params); + } + + /** + * Retrieve a value of params according to its key. + * + * @param string $item_id_key + * @param int $default Default value when key is not found. + * + * @return int + */ + public function get(string $item_id_key, int $default = 0): int + { + return (int)($this->id_params[$item_id_key] ?? $default); + } +} \ No newline at end of file diff --git a/trunk/src/Helper/class-metabox-factory.php b/trunk/src/Helper/class-metabox-factory.php index 0f50b73..1785b20 100644 --- a/trunk/src/Helper/class-metabox-factory.php +++ b/trunk/src/Helper/class-metabox-factory.php @@ -3,6 +3,8 @@ namespace Trasweb\Plugins\DisplayMetadata\Helper; +use Trasweb\Plugins\DisplayMetadata\Dto\Id_Url_Params; +use Trasweb\Plugins\DisplayMetadata\Iterator\Metabox_Types_Iterator; use Trasweb\Plugins\DisplayMetadata\Model; use Trasweb\Plugins\DisplayMetadata\Type; use Trasweb\Plugins\DisplayMetadata\Type\Metabox_Type; @@ -12,48 +14,14 @@ */ class Metabox_Factory { - private const DEFAULT_METABOX = Type\None::class; - /** - * array - */ - private array $metabox_types_by_screen_var_key; - /** - * @var array - */ - private array $screen_vars; + private Metabox_Types_Iterator $metabox_types; - public function __construct(array $screen_vars, array $metabox_types_by_screen_var_key) { - $this->screen_vars = $this->check_screen_vars($screen_vars); - $this->metabox_types_by_screen_var_key = $this->check_metabox_types($metabox_types_by_screen_var_key); - } + private Id_Url_Params $id_params; - /** - * Screen vars should be numeric (they, which we use, are ids) - * - * @param array $screen_vars - * - * @return array - */ - private function check_screen_vars(array $screen_vars): array + public function __construct(Id_Url_Params $id_params, Metabox_Types_Iterator $metabox_types) { - $screen_var_checker = fn($item_id) => absint($item_id ?? 0); - - return array_filter(array_map($screen_var_checker, $screen_vars)); - } - - /** - * Metabox types should to be Metabox objects. - * - * @param array $metabox_types - * - * @return array - */ - private function check_metabox_types(array $metabox_types): array - { - $type_checker = static fn($metabox_type) => is_a($metabox_type['type'], Type\Metabox_Type::class, allow_string: true); - $model_checker = static fn($metabox_type) => is_a($metabox_type['model'], Model\Metabox_Model::class, allow_string: true); - - return array_filter(array_filter($metabox_types, $type_checker), $model_checker); + $this->id_params = $id_params; + $this->metabox_types = $metabox_types; } /** @@ -64,15 +32,13 @@ private function check_metabox_types(array $metabox_types): array */ final public function instance_current_metabox(Metabox_View $metabox_View): Type\Metabox_Type { - $current_metabox = new (self::DEFAULT_METABOX)(new Model\Custom_Model(), $metabox_View); - - foreach ($this->metabox_types_by_screen_var_key as $item_id_key => $metabox) { - $item_id = (int)( $this->screen_vars[$item_id_key] ?? 0 ); + $default_metabox = $this->metabox_types->get_default_metabox(); + $current_metabox = new $default_metabox(new Model\Custom_Model(), $metabox_View); - $metabox_type = $metabox['type'] ?? null; - $metabox_model = $metabox['model'] ?? null; + foreach ($this->metabox_types as $item_id_key => list('type' => $metabox_type, 'model' => $metabox_model)) { + $item_id = $this->id_params->get($item_id_key); - if ($item_id && $metabox_type && $metabox_model ) { + if ($item_id && $metabox_type && $metabox_model) { $metabox_model = new $metabox_model($item_id); $current_metabox = new $metabox_type($metabox_model, $metabox_View); break; diff --git a/trunk/src/Iterator/class-metabox-types-iterator.php b/trunk/src/Iterator/class-metabox-types-iterator.php new file mode 100644 index 0000000..0632232 --- /dev/null +++ b/trunk/src/Iterator/class-metabox-types-iterator.php @@ -0,0 +1,77 @@ + + */ + private array $metabox_types; + private string $default_metabox; + + public function __construct(array $metabox_types, string $default_metabox) + { + $this->metabox_types = $metabox_types; + $this->default_metabox = $default_metabox; + } + + /** + * Retrieve FQN of default metabox + * + * @return string + */ + public function get_default_metabox(): string + { + return $this->default_metabox; + } + + /** + * Loop through all metabox types + * + * @return Traversable + */ + public function getIterator(): Traversable + { + foreach ($this->metabox_types as $metabox_type_key_id => $metabox) { + $metabox_type = $metabox['type'] ?? null; + $metabox_model = $metabox['model'] ?? null; + + if (null !== $metabox_type && null !== $metabox_model) { + yield $metabox_type_key_id => $metabox; + } + } + } + + /** + * Metabox types should to be Metabox objects. + * + * @param array $metabox_types + * + * @return array + */ + private function check_metabox_types(array $metabox_types): array + { + $type_checker = static fn($metabox_type) => is_a( + $metabox_type['type'], + Type\Metabox_Type::class, + allow_string: true + ); + $model_checker = static fn($metabox_type) => is_a( + $metabox_type['model'], + Model\Metabox_Model::class, + allow_string: true + ); + + return array_filter(array_filter($metabox_types, $type_checker), $model_checker); + } +} \ No newline at end of file