Collection.php
1.83 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
70
<?php
namespace ACP\ListScreenRepository;
use AC;
use AC\ListScreen;
use AC\ListScreenCollection;
use AC\ListScreenRepository\Filter;
use AC\ListScreenRepository\ListScreenPermissionTrait;
use AC\ListScreenRepository\Sort;
use AC\Type\ListScreenId;
use WP_User;
/**
* Repository for the deprecated PHP storage
*/
final class Collection implements AC\ListScreenRepository {
use ListScreenPermissionTrait;
private $list_screens;
public function __construct( ListScreenCollection $list_screens ) {
$this->list_screens = $list_screens;
// Since this is in memory only, enforce read only
foreach ( $list_screens as $list_screen ) {
$list_screen->set_read_only( true );
}
}
public function find_by_user( ListScreenId $id, WP_User $user ): ?ListScreen {
$list_screens = ( new Filter\ListId( $id ) )->filter( $this->find_all() );
$list_screen = $list_screens->get_first() ?: null;
return $list_screen && $this->user_can_view_list_screen( $list_screen, $user )
? $list_screen
: null;
}
public function find_all_by_user( string $key, WP_User $user, Sort $sort = null ): ListScreenCollection {
$list_screens = $this->find_all_by_key( $key );
return ( new Filter\User( $user ) )->filter( $list_screens );
}
public function find_all_by_key( string $key, Sort $sort = null ): ListScreenCollection {
$list_screens = ( new Filter\ListKey( $key ) )->filter( $this->list_screens );
return $sort
? $sort->sort( $list_screens )
: $list_screens;
}
public function find_all( Sort $sort = null ): ListScreenCollection {
return $this->list_screens;
}
public function find( ListScreenId $id ): ?ListScreen {
$list_screens = ( new Filter\ListId( $id ) )->filter( $this->list_screens );
return $list_screens->get_first() ?: null;
}
public function exists( ListScreenId $id ): bool {
return null !== $this->find( $id );
}
}