|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace MazenTouati\NoEmoji\Entities; |
|
5
|
|
|
|
|
6
|
|
|
use MazenTouati\Simple2wayConfig\S2WConfig; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A file is a self-contained object that serves as layer between the package and the file system with an extra functionalities relative to the package |
|
10
|
|
|
* |
|
11
|
|
|
* @author Mazen Touati <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class File |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The file content |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $content; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Configuration object |
|
25
|
|
|
* |
|
26
|
|
|
* @var S2WConfig |
|
27
|
|
|
*/ |
|
28
|
|
|
public $config; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The configuration dot-path that lead to the file's source path |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public $src; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(S2WConfig $c, $src = 'storage.input.src') |
|
38
|
|
|
{ |
|
39
|
|
|
$this->config = $c; |
|
40
|
|
|
$base = $this->config->get('storage.base'); |
|
41
|
|
|
$src = $this->config->get($src); |
|
42
|
|
|
$this->content = file_get_contents($base.$src); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Puts delimiters between group to make separating them easier |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function prepareFileForScrapping(): void |
|
51
|
|
|
{ |
|
52
|
|
|
// Note: the EOG 's (End Of Group) Marker should start with @ |
|
53
|
|
|
|
|
54
|
|
|
// Replace the EOF marker with the EOG marker |
|
55
|
|
|
$content = str_replace('#EOF', '@EOG', $this->content); |
|
56
|
|
|
|
|
57
|
|
|
// Add the Marker between groups |
|
58
|
|
|
$replacement = "@EOG\n\n$1"; |
|
59
|
|
|
$pattern = "/(# group: .+)/m"; |
|
60
|
|
|
$this->content = preg_replace($pattern, $replacement, $content); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Executes a Closure that alter the file content |
|
65
|
|
|
* |
|
66
|
|
|
* @param \Closure $callback |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
|
|
public function replaceContent(\Closure $callback): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->content = $callback($this->content); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Converts the data into a JSON file |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $path Output path |
|
79
|
|
|
* @param string $fileTitle The file's title |
|
80
|
|
|
* @param array $data The file data |
|
81
|
|
|
* |
|
82
|
|
|
* @return boolean|array Return false on error or an array the hold the created file details |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function toJSON(string $path, string $fileTitle, array $data) |
|
85
|
|
|
{ |
|
86
|
|
|
$size = file_put_contents($path, json_encode($data)); |
|
87
|
|
|
if ($size === false) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return [ |
|
92
|
|
|
'path' => $path, |
|
93
|
|
|
'fileTitle' => $fileTitle, |
|
94
|
|
|
'size' => $size |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
/** |
|
98
|
|
|
* Converts the data into a text file |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $path Output path |
|
101
|
|
|
* @param string $fileTitle The file's title |
|
102
|
|
|
* @param string $data The file content |
|
103
|
|
|
* |
|
104
|
|
|
* @return boolean|array Return false on error or an array the hold the created file details |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function toText(string $path, string $fileTitle, string $data) |
|
107
|
|
|
{ |
|
108
|
|
|
$size = file_put_contents($path, $data); |
|
109
|
|
|
if ($size === false) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return [ |
|
114
|
|
|
'path' => $path, |
|
115
|
|
|
'fileTitle' => $fileTitle, |
|
116
|
|
|
'size' => $size |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Converts a JSON file to an array |
|
122
|
|
|
* |
|
123
|
|
|
* @param boolean $assocArray Indicates if returned data should be an associative arrays or not. |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function fromJSON($assocArray = false) |
|
128
|
|
|
{ |
|
129
|
|
|
return json_decode($this->content, $assocArray); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|