ListScreenFactory.php
1.35 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
<?php
declare( strict_types=1 );
namespace AC;
use InvalidArgumentException;
use WP_Screen;
class ListScreenFactory implements ListScreenFactoryInterface {
/**
* @var ListScreenFactoryInterface[]
*/
private static $factories = [];
public static function add( ListScreenFactoryInterface $factory ): void {
array_unshift( self::$factories, $factory );
}
public function create( string $key, array $settings = [] ): ListScreen {
foreach ( self::$factories as $factory ) {
if ( $factory->can_create( $key ) ) {
return $factory->create( $key, $settings );
}
}
throw new InvalidArgumentException( 'Invalid key' );
}
public function can_create( string $key ): bool {
foreach ( self::$factories as $factory ) {
if ( $factory->can_create( $key ) ) {
return true;
}
}
return false;
}
public function can_create_by_wp_screen( WP_Screen $screen ): bool {
foreach ( self::$factories as $factory ) {
if ( $factory->can_create_by_wp_screen( $screen ) ) {
return true;
}
}
return false;
}
public function create_by_wp_screen( WP_Screen $screen, array $settings = [] ): ListScreen {
foreach ( self::$factories as $factory ) {
if ( $factory->can_create_by_wp_screen( $screen ) ) {
return $factory->create_by_wp_screen( $screen, $settings );
}
}
throw new InvalidArgumentException( 'Invalid screen' );
}
}