1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Folk\Entities; |
4
|
|
|
|
5
|
|
|
use Folk\SearchQuery; |
6
|
|
|
use SimpleCrud\Row; |
7
|
|
|
use FilesystemIterator; |
8
|
|
|
use RecursiveDirectoryIterator; |
9
|
|
|
|
10
|
|
|
abstract class File extends AbstractEntity implements EntityInterface |
11
|
|
|
{ |
12
|
|
|
protected $extension; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Returns the base path. |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
abstract protected function getBasePath(); |
20
|
|
|
|
21
|
|
|
protected function getIterator() |
22
|
|
|
{ |
23
|
|
|
return new RecursiveDirectoryIterator($this->getBasePath(), FilesystemIterator::SKIP_DOTS); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function search(SearchQuery $search) |
30
|
|
|
{ |
31
|
|
|
$result = []; |
32
|
|
|
$words = $search->getWords(); |
33
|
|
|
$start = strlen($this->getBasePath()) + 1; |
34
|
|
|
$length = -strlen($this->extension) - 1; |
35
|
|
|
|
36
|
|
|
foreach ($this->getIterator() as $file) { |
37
|
|
|
if (!$file->isFile() || $file->getExtension() !== $this->extension) { |
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$id = substr($file->getPathname(), $start, $length); |
42
|
|
|
|
43
|
|
|
foreach ($words as $word) { |
44
|
|
|
if (strpos($id, $word) === false) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$result[$id] = $this->parse(file_get_contents($file->getPathname())); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$sort = $search->getSort(); |
53
|
|
|
|
54
|
|
|
if ($sort) { |
|
|
|
|
55
|
|
|
uasort($result, function ($a, $b) use ($sort) { |
56
|
|
|
if ($a[$sort] === $b[$sort]) { |
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return ($a[$sort] < $b[$sort]) ? -1 : 1; |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
if ($search->getDirection() === 'DESC') { |
64
|
|
|
$result = array_reverse($result, true); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($search->getPage() !== null) { |
69
|
|
|
$offset = ($search->getPage() * 50) - 50; |
70
|
|
|
|
71
|
|
|
$result = array_slice($result, $offset, 50, true); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function create(array $data) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$id = $this->getId($data); |
83
|
|
|
$file = $this->getFilePath($id); |
84
|
|
|
$source = $this->stringify($data); |
85
|
|
|
|
86
|
|
|
file_put_contents($file, $source); |
87
|
|
|
|
88
|
|
|
return $id; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function read($id) |
95
|
|
|
{ |
96
|
|
|
$file = $this->getFilePath($id); |
97
|
|
|
|
98
|
|
|
return $this->parse(file_get_contents($file)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function update($id, array $data) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$file = $this->getFilePath($id); |
107
|
|
|
$source = $this->stringify($data); |
108
|
|
|
|
109
|
|
|
file_put_contents($file, $source); |
110
|
|
|
|
111
|
|
|
return $data; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function delete($id) |
118
|
|
|
{ |
119
|
|
|
$file = $this->getFilePath($id); |
120
|
|
|
|
121
|
|
|
unlink($file); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Calculate the id of a new row. |
126
|
|
|
* |
127
|
|
|
* @param array $data |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function getId(array $data) |
132
|
|
|
{ |
133
|
|
|
$list = glob($this->getBasePath()."/*.{$this->extension}"); |
134
|
|
|
|
135
|
|
|
return $list ? count($list) + 1 : uniqid(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the path of a file. |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
protected function getFilePath($filename) |
144
|
|
|
{ |
145
|
|
|
return $this->getBasePath()."/{$filename}.{$this->extension}"; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Transform the data to a string. |
150
|
|
|
* |
151
|
|
|
* @param array $data |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
abstract protected function stringify(array $data); |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Transform the string to an array. |
159
|
|
|
* |
160
|
|
|
* @param string $source |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
abstract protected function parse($source); |
165
|
|
|
} |
166
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: