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 FileEntity 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 = null) |
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
|
|
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function create(array $data) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$file = $this->getFilePath($this->getId($data)); |
61
|
|
|
$source = $this->stringify($data); |
62
|
|
|
|
63
|
|
|
file_put_contents($file, $source); |
64
|
|
|
|
65
|
|
|
return $file; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function read($id) |
72
|
|
|
{ |
73
|
|
|
$file = $this->getFilePath($id); |
74
|
|
|
|
75
|
|
|
return $this->parse(file_get_contents($file)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function update($id, array $data) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$file = $this->getFilePath($id); |
84
|
|
|
$source = $this->stringify($data); |
85
|
|
|
|
86
|
|
|
file_put_contents($file, $source); |
87
|
|
|
|
88
|
|
|
return $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function delete($id) |
95
|
|
|
{ |
96
|
|
|
$file = $this->getFilePath($id); |
97
|
|
|
|
98
|
|
|
unlink($file); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Calculate the id of a new row. |
103
|
|
|
* |
104
|
|
|
* @param array $data |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function getId(array $data) |
109
|
|
|
{ |
110
|
|
|
return uniqid(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the path of a file. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function getFilePath($filename) |
119
|
|
|
{ |
120
|
|
|
return $this->getBasePath()."/{$filename}.{$this->extension}"; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Transform the data to a string. |
125
|
|
|
* |
126
|
|
|
* @param array $data |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
abstract protected function stringify(array $data); |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Transform the string to an array. |
134
|
|
|
* |
135
|
|
|
* @param string $source |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
abstract protected function parse($source); |
140
|
|
|
} |
141
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: