-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Manuel Canga
committed
Sep 16, 2024
1 parent
1fe026b
commit 9eaf34b
Showing
5 changed files
with
162 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |