UserColumnStateRepository.php
1.5 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
<?php
declare( strict_types=1 );
namespace ACP\Export\Repository;
use AC\Type\ListScreenId;
use ACP\Export\ColumnStateCollection;
use ACP\Export\Type\ColumnState;
use ACP\Export\UserPreference\ExportedColumns;
class UserColumnStateRepository {
/**
* @var ExportedColumns
*/
private $storage;
public function __construct() {
$this->storage = new ExportedColumns();
}
public function find_all_by_list_id( ListScreenId $list_id ): ColumnStateCollection {
$collection = new ColumnStateCollection();
if ( ! $this->storage->exists( $list_id ) ) {
return $collection;
}
foreach ( $this->storage->get( $list_id ) as $data ) {
if ( ! isset( $data['column_name'], $data['active'] ) ) {
continue;
}
$collection->add( new ColumnState( $data['column_name'], (bool) $data['active'] ) );
}
return $collection;
}
public function find_all_active_by_list_id( ListScreenId $list_id ): ColumnStateCollection {
$collection = new ColumnStateCollection();
foreach ( $this->find_all_by_list_id( $list_id ) as $state ) {
if ( $state->is_active() ) {
$collection->add( $state );
}
}
return $collection;
}
public function save( ListScreenId $list_id, ColumnStateCollection $column_states ): void {
$data = [];
foreach ( $column_states as $column_state ) {
$data[] = [
'column_name' => $column_state->get_column_name(),
'active' => $column_state->is_active(),
];
}
$data
? $this->storage->save( $list_id, $data )
: $this->storage->delete( $list_id );
}
}