Entries.php
1.8 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
<?php
/**
* SearchWP Entry Collection.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP;
use SearchWP\Entry;
use SearchWP\Source;
/**
* Class Entries is a collection of Entry objects.
*
* @since 4.0
*/
class Entries {
/**
* Collection items.
*
* @since 4.0
* @var array
*/
private $items = [];
/**
* Entries contrsuctor.
*
* @param Source $source The Source of the Entries.
* @param array $ids The Source Entry IDs.
* @since 4.0
*/
function __construct( Source $source, array $ids = [] ) {
if ( ! empty( $ids ) ) {
$source_name = $source->get_name();
foreach ( $ids as $id ) {
$entry = new Entry( $source_name, $id, false, false );
$this->add( $entry );
}
}
}
/**
* Adds an Entry to the collection.
*
* @since 4.0
* @param Entry $entry The Entry to add.
* @return Entries
*/
public function add( Entry $entry ) {
$this->items[ $this->key( $entry ) ] = $entry;
return $this;
}
/**
* Removes an Entry from the collection.
*
* @since 4.0
* @param Entry $entry The Entry to remove.
* @return Entries
*/
public function remove( Entry $entry ) {
unset( $this->items[ $this->key( $entry ) ] );
return $this;
}
/**
* Generates a unique key for an Entry.
*
* @since 4.0
* @param Entry $entry The collection Entry.
* @return string
*/
private function key( Entry $entry ) {
return md5( $entry->get_source()->get_name() . $entry->get_id() );
}
/**
* Getter for collection item IDs.
*
* @since 4.0
* @return array
*/
public function get_ids() {
return array_values( array_map( function( $entry ) {
return $entry->get_id();
}, $this->items ) );
}
/**
* Getter for collection items.
*
* @since 4.0
* @return array
*/
public function get() {
return $this->items;
}
}