|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PhpBotFramework. |
|
5
|
|
|
* |
|
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
|
8
|
|
|
* published by the Free Software Foundation, version 3. |
|
9
|
|
|
* |
|
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
|
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13
|
|
|
* Lesser General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Entities; |
|
20
|
|
|
|
|
21
|
|
|
use PhpBotFramework\Exceptions\BotException; |
|
22
|
|
|
|
|
23
|
|
|
class File |
|
24
|
|
|
{ |
|
25
|
|
|
private $file; |
|
26
|
|
|
|
|
27
|
|
|
private $format_name; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* \brief Fill this object with another file. |
|
31
|
|
|
* @param string $file File_id or local/remote path to the file. |
|
32
|
|
|
* @param string $format_name Format name of the file (audio, document, ...) |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function init(string $file, string $format_name) |
|
35
|
|
|
{ |
|
36
|
7 |
|
$this->file = $file; |
|
37
|
7 |
|
$this->format_name = $format_name; |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* \brief (<i>Internal</i>) Check if the path to the file given is local or a file_id/url. |
|
42
|
|
|
* @return bool True if the file is a local path. |
|
43
|
|
|
*/ |
|
44
|
7 |
|
public function is_local() : bool |
|
45
|
|
|
{ |
|
46
|
7 |
|
$host = parse_url($this->file, PHP_URL_HOST); |
|
47
|
|
|
|
|
48
|
|
|
// If it has not an url host and it is a file_id |
|
49
|
7 |
|
if ($host === null && !is_numeric($this->file)) { |
|
50
|
|
|
// Then it is a local path |
|
51
|
3 |
|
return true; |
|
52
|
|
|
} else { |
|
53
|
4 |
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* \brief (<i>Internal</i>) Get string component of this file. |
|
59
|
|
|
* @return string File as string (file_id/path to the file). |
|
60
|
|
|
*/ |
|
61
|
4 |
|
public function getString() |
|
62
|
|
|
{ |
|
63
|
4 |
|
return $this->file; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
public function getResource() |
|
67
|
|
|
{ |
|
68
|
3 |
|
$resource = fopen($this->file, 'r'); |
|
69
|
|
|
|
|
70
|
3 |
|
if ($resource !== false) { |
|
71
|
3 |
|
return $resource; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
7 |
|
public function getFormatName() : string |
|
78
|
|
|
{ |
|
79
|
7 |
|
return $this->format_name; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|