Skip to content

Commit

Permalink
refactor: simpler metabox factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Canga committed Sep 16, 2024
1 parent 1fe026b commit 9eaf34b
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 54 deletions.
1 change: 1 addition & 0 deletions trunk/config/display-metadata.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'assets' => [
'subpath' => '/assets',
],
'default-metabox' => Type\None::class,
'metabox-types' => [
'post' => [
'type' => Type\Post::class,
Expand Down
15 changes: 7 additions & 8 deletions trunk/config/services.conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
);
},

Expand Down
65 changes: 65 additions & 0 deletions trunk/src/Dto/class-id-url-params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Trasweb\Plugins\DisplayMetadata\Dto;


/**
* Class IdUrlParams
*/
class Id_Url_Params
{
/**
* @var array<string, int> $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<string, mixed> $url_params
*
* @return array<string, int>
*/
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);
}
}
58 changes: 12 additions & 46 deletions trunk/src/Helper/class-metabox-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,48 +14,14 @@
*/
class Metabox_Factory
{
private const DEFAULT_METABOX = Type\None::class;
/**
* array<string,array{type:class-string,model:class-string}>
*/
private array $metabox_types_by_screen_var_key;
/**
* @var array<string, int>
*/
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<string, mixed> $screen_vars
*
* @return array<string, int>
*/
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<string, mixed> $metabox_types
*
* @return array<string,array{type: class-string, model:class-string}>
*/
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;
}

/**
Expand All @@ -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;
Expand Down
77 changes: 77 additions & 0 deletions trunk/src/Iterator/class-metabox-types-iterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Trasweb\Plugins\DisplayMetadata\Iterator;

use Trasweb\Plugins\DisplayMetadata\Model;
use Trasweb\Plugins\DisplayMetadata\Type;
use Traversable;

/**
* Class MetaboxTypesIterator
*/
class Metabox_Types_Iterator implements \IteratorAggregate
{
/**
* array<string,array{type:class-string,model:class-string}>
*/
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<string, mixed> $metabox_types
*
* @return array<string,array{type: class-string, model:class-string}>
*/
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);
}
}

0 comments on commit 9eaf34b

Please sign in to comment.