SegmentRepository.php
3.55 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace ACP\Bookmark;
use AC\Type\ListScreenId;
use ACP\Bookmark\Entity\Segment;
use ACP\Bookmark\Type\SegmentId;
use DateTime;
use InvalidArgumentException;
use RuntimeException;
class SegmentRepository {
public const FILTER_USER = 'user_id';
public const FILTER_LIST_SCREEN = 'list_screen_id';
public const FILTER_GLOBAL = 'global';
public const ORDER_BY = 'orderby';
public const ORDER = 'order';
public const TABLE = 'ac_segments';
public function find( SegmentId $id ): ?Segment {
global $wpdb;
$sql = "
SELECT *
FROM " . $wpdb->prefix . self::TABLE . "
WHERE id = %s
";
$result = $wpdb->get_row( $wpdb->prepare( $sql, $id->get_id() ) );
if ( ! isset( $result->id ) ) {
return null;
}
return $this->create_segment_from_row( $result );
}
/**
* @param array $args
*
* @return Segment[]
*/
public function find_all( array $args = [] ): array {
global $wpdb;
$args = array_merge( [
self::FILTER_USER => null,
self::FILTER_LIST_SCREEN => null,
self::FILTER_GLOBAL => null, // Global available to all users
self::ORDER_BY => 'date_created', // e.g. `name`, `date_created`, `id`, `user_id`
self::ORDER => null,
], $args );
$sql = "
SELECT *
FROM {$wpdb->prefix}" . self::TABLE;
$and = [];
if ( $args[ self::FILTER_LIST_SCREEN ] ) {
$listScreenId = $args[ self::FILTER_LIST_SCREEN ];
if ( ! $listScreenId instanceof ListScreenId ) {
throw new InvalidArgumentException( 'Expected a ListScreenId for list screen id.' );
}
$and[] = $wpdb->prepare( "`list_screen_id` = %s", $listScreenId->get_id() );
}
if ( $args[ self::FILTER_USER ] ) {
$and[] = $wpdb->prepare( "`user_id` = %d", $args[ self::FILTER_USER ] );
}
if ( is_bool( $args[ self::FILTER_GLOBAL ] ) ) {
$and[] = $wpdb->prepare( "`global` = %d", $args[ self::FILTER_GLOBAL ] );
}
if ( $and ) {
$sql .= "\nWHERE " . implode( "\nAND ", $and );
}
$order = $args[ self::ORDER ] === 'ASC'
? $args[ self::ORDER ]
: 'DESC';
$sql .= sprintf( "\nORDER BY `%s` %s", $wpdb->_escape( $args[ self::ORDER_BY ] ), $order );
$segments = [];
foreach ( $wpdb->get_results( $sql ) as $row ) {
$segments[ $row->id ] = $this->create_segment_from_row( $row );
}
return $segments;
}
private function create_segment_from_row( object $row ): Segment {
return new Segment(
new SegmentId( (int) $row->id ),
new ListScreenId( $row->list_screen_id ),
(int) $row->user_id,
$row->name,
unserialize( $row->url_parameters, [ 'allowed_classes' => false ] ),
(bool) $row->global
);
}
public function create(
ListScreenId $list_screen_id,
int $user_id,
string $name,
array $url_parameters,
bool $global
): Segment {
global $wpdb;
$inserted = $wpdb->insert(
$wpdb->prefix . self::TABLE,
[
'list_screen_id' => $list_screen_id->get_id(),
'user_id' => $user_id,
'name' => $name,
'url_parameters' => serialize( $url_parameters ),
'global' => $global,
'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ),
],
[
'%s',
'%d',
'%s',
'%s',
'%d',
'%s',
]
);
if ( $inserted !== 1 ) {
throw new RuntimeException( 'Failed to save segment.' );
}
return new Segment(
new SegmentId( $wpdb->insert_id ),
$list_screen_id,
$user_id,
$name,
$url_parameters,
$global
);
}
public function delete( SegmentId $id ): void {
global $wpdb;
$wpdb->delete(
$wpdb->prefix . self::TABLE,
[
'id' => $id->get_id(),
],
[
'%d',
]
);
}
}