FieldRepository.php
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace ACA\JetEngine;
use AC\ListScreen;
use ACA\JetEngine\Column;
use ACA\JetEngine\Field\Field;
use ACA\JetEngine\Utils\Api;
use ACP;
final class FieldRepository {
/**
* @var FieldFactory
*/
private $field_factory;
public function __construct() {
$this->field_factory = new FieldFactory();
}
public function find_by_column( Column\Meta $column ): ?Field {
$fields = $this->find_by_list_screen( $column->get_list_screen() );
if ( empty( $fields ) ) {
return null;
}
$field = array_filter( $fields, static function ( $field ) use ( $column ) {
return $field->get_name() === $column->get_type();
} );
return empty( $field ) ? null : current( $field );
}
/**
* @return Field[]
*/
public function find_by_list_screen( ListScreen $list_screen ): array {
switch ( true ) {
case $list_screen instanceof ListScreen\Post:
return $this->map_meta_types( Api::MetaBox()->get_fields_for_context( 'post_type', $list_screen->get_post_type() ) );
case $list_screen instanceof ACP\ListScreen\Taxonomy:
return $this->map_meta_types( Api::MetaBox()->get_fields_for_context( 'taxonomy', $list_screen->get_taxonomy() ) );
case $list_screen instanceof ACP\ListScreen\User:
$fields = array_merge( ...array_values( Api::MetaBox()->get_fields_for_context( 'user' ) ) );
return $this->map_meta_types( $fields );
}
return [];
}
/**
* @return Field[]
*/
private function map_meta_types( array $meta_types ): array {
$fields = [];
foreach ( $meta_types as $field ) {
if ( isset( $field['object_type'] ) && $field['object_type'] === 'field' ) {
$fields[] = $this->field_factory->create( $field );
}
}
return array_filter( $fields );
}
}