/
File.php
89 lines (79 loc) · 3.14 KB
/
File.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
<?php
namespace mauriziocingolani\yii2fmwktelegrambot;
use Yii;
/**
* This object represents a file ready to be downloaded. The file can be downloaded via the link
* <code>https://api.telegram.org/file/bot<token>/<file_path>. It is guaranteed that the link
* will be valid for at least 1 hour. When the link expires, a new one can be requested by calling <code>getFile</code>.
*
* @property string $file_id Unique identifier for this file
* @property integer $file_size File size, if known (optional)
* @property string $file_path File path (optional)
*
* @property string $fileUrl URL the file can be downloaded from
*
* @author Maurizio Cingolani <mauriziocingolani74@gmail.com>
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @version 1.1
*
* @see https://core.telegram.org/bots/api#file
*/
class File extends \yii\base\BaseObject {
private $_file_id;
private $_file_size;
private $_file_path;
/**
* Builds a new instance of this class, and populates the instance properties with the object
* passed as parameter. The properties the object should have are:
* <ul>
* <li>file_id</li>
* <li>file_size (optional)</li>
* <li>file_path (optional)</li>
* </ul>
* These names reflect the names of the private properties of this class, and their value will be assigned
* to the corrisponding class property.
* @param mixed $object Object with file data
*/
public function __construct($object) {
foreach ($object as $key => $value) :
$k = "_$key";
$this->$k = $value;
endforeach;
}
/**
* Returns the unique identifier for this file
* @return integer Unique identifier for this file
*/
public function getFile_id() {
return (int) $this->_file_id;
}
/**
* Returns the file size, if known.
* @return integer File size, if known
*/
public function getFile_size() {
return $this->_file_size ? (int) $this->_file_size : null;
}
/**
* Returns the file path. Use <code>https://api.telegram.org/file/bot<token>/<file_path></code> to get the file.
* @return integer File path
*/
public function getFile_path() {
return $this->_file_path ? $this->_file_path : null;
}
/**
* Returns the URL for the file to be downloaded: <code>https://api.telegram.org/file/bot<token>/<file_path></code>.
* You can either pass the token as a parameter, or use the Telegram component $token property.
* @param string $token Telegram Bot token (optional)
* @return string File URL the file can be downloaded from
* @throws yii\base\InvalidConfigException When the $token property of the Telegram component is missing.
*/
public function getFileUrl($token = null) {
if (!$token) :
$token = Yii::$app->telegram->token;
if (!$token)
throw new yii\base\InvalidConfigException(__METHOD__ . ': $token parameter missing in Telegram component configuration.');
endif;
return TelegramBot::FILE_URL . $token . '/' . $this->_file_path;
}
}