-
Notifications
You must be signed in to change notification settings - Fork 0
/
Microblog.php
133 lines (111 loc) · 3.21 KB
/
Microblog.php
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
<?php
namespace Neonus\Microblog;
use Exception;
class Microblog
{
private $dataDirectory = '';
/**
* Constructor.
*
* @param string $dataDirectory Path to directory with post files.
* @return Null
*/
public function __construct($dataDirectory)
{
if (empty($dataDirectory)) {
throw new Exception('Data directory is not set');
}
$this->dataDirectory = rtrim($dataDirectory, '/') . '/';
}
/**
* Get list of all posts ordered by date.
*
* @param bool $reverseSort Sort array in ASC or DSC order. Default NULL is ASC.
* @return Array of Post class
*/
public function getPosts($reverseSort = null)
{
$posts = array();
$fileNames = $this->loadFiles($this->dataDirectory);
foreach ($fileNames as $fileName) {
if (preg_match('/^([\d-]+)_(.*)\.([a-zA-Z]+)$/', $fileName, $matches)) {
// if date could not be recognized, return plain number
if (!$date = strtotime($matches[1])) {
$date = intval($matches[1]);
}
$posts[] = new Post($date, $matches[2], $matches[3], $this->dataDirectory . $fileName);
}
}
if ($reverseSort) {
usort($posts, array($this, "sortByDateReverse"));
} else {
usort($posts, array($this, "sortByDate"));
}
return $posts;
}
/**
* Search for post by specified URL.
*
* @param string $url URL to look for
* @return Post Instance of blog post object or FALSE if none found
*/
public function getPostByUrl($url)
{
// first get array of URL of all posts, indexed by url
$posts = $this->getPostsIndexedByUrl();
// now check if such key exists in array
if (array_key_exists($url, $posts)) {
return $posts[$url];
}
return false;
}
/**
* Loads all files from specified directory.
*
* @param string $directory Path to directory containig data files
* @return Array of file names
*/
private function loadFiles($directory)
{
$files = array();
if ($dirHandle = opendir($directory)) {
while (false !== ($entry = readdir($dirHandle))) {
if ($entry !== '.' && $entry !== '..') {
$files[] = $entry;
}
}
closedir($dirHandle);
} else {
throw new Exception('Cannot open dir');
}
return $files;
}
private function sortByDate($a, $b)
{
if ($a->getDate() == $b->getDate()) {
return 0;
}
return ($a->getDate() < $b->getDate()) ? -1 : 1;
}
private function sortByDateReverse($a, $b)
{
if ($a->getDate() == $b->getDate()) {
return 0;
}
return ($a->getDate() < $b->getDate()) ? 1 : -1;
}
/**
* Get all posts in array indexed by URL
*
* @return Array of indexed posts. URL is key.
*/
private function getPostsIndexedByUrl()
{
$posts = $this->getPosts();
$indexedPosts = array();
foreach ($posts as $post) {
$indexedPosts[$post->getUrl()] = $post;
}
return $indexedPosts;
}
}