| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\User_Meta\Application; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\User_Meta\Domain\Custom_Meta_Interface; |
| 6 |
|
| 7 |
/** |
| 8 |
* The collector to get custom user meta. |
| 9 |
* |
| 10 |
* @makePublic |
| 11 |
*/ |
| 12 |
class Custom_Meta_Collector { |
| 13 |
|
| 14 |
/** |
| 15 |
* All custom meta. |
| 16 |
* |
| 17 |
* @var array<Custom_Meta_Interface> |
| 18 |
*/ |
| 19 |
private $custom_meta; |
| 20 |
|
| 21 |
/** |
| 22 |
* The constructor. |
| 23 |
* |
| 24 |
* @param Custom_Meta_Interface ...$custom_meta All custom meta. |
| 25 |
*/ |
| 26 |
public function __construct( Custom_Meta_Interface ...$custom_meta ) { |
| 27 |
$this->custom_meta = $custom_meta; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns all the custom meta. |
| 32 |
* |
| 33 |
* @return array<Custom_Meta_Interface> All the custom meta. |
| 34 |
*/ |
| 35 |
public function get_custom_meta(): array { |
| 36 |
return $this->custom_meta; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns all the custom meta, sorted by rendering priority. |
| 41 |
* |
| 42 |
* @return array<Custom_Meta_Interface> All the custom meta, sorted by rendering priority. |
| 43 |
*/ |
| 44 |
public function get_sorted_custom_meta(): array { |
| 45 |
$custom_meta = $this->get_custom_meta(); |
| 46 |
|
| 47 |
\usort( |
| 48 |
$custom_meta, |
| 49 |
static function ( Custom_Meta_Interface $a, Custom_Meta_Interface $b ) { |
| 50 |
return ( $a->get_render_priority() <=> $b->get_render_priority() ); |
| 51 |
}, |
| 52 |
); |
| 53 |
|
| 54 |
return $custom_meta; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the custom meta that can't be empty. |
| 59 |
* |
| 60 |
* @return array<string> The custom meta that can't be empty. |
| 61 |
*/ |
| 62 |
public function get_non_empty_custom_meta(): array { |
| 63 |
$non_empty_custom_meta = []; |
| 64 |
foreach ( $this->custom_meta as $custom_meta ) { |
| 65 |
if ( ! $custom_meta->is_empty_allowed() ) { |
| 66 |
$non_empty_custom_meta[] = $custom_meta->get_key(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $non_empty_custom_meta; |
| 71 |
} |
| 72 |
} |
| 73 |
|