]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Twitter/Media.php
Use the owner, not the author
[friendica.git] / src / Object / Api / Twitter / Media.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Object\Api\Twitter;
23
24 use Friendica\BaseDataTransferObject;
25 use Friendica\Model\Post;
26
27 /**
28  * Class Media
29  *
30  * @see https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities#media
31  */
32 class Media extends BaseDataTransferObject
33 {
34         /** @var string */
35         protected $display_url;
36         /** @var string */
37         protected $expanded_url;
38         /** @var int */
39         protected $id;
40         /** @var string */
41         protected $id_str;
42         /** @var array */
43         protected $indices;
44         /** @var string */
45         protected $media_url;
46         /** @var string */
47         protected $media_url_https;
48         /** @var string */
49         protected $sizes;
50         /** @var string */
51         protected $type;
52         /** @var string */
53         protected $url;
54
55         /**
56          * Creates a media entity array
57          *
58          * @param array $attachment
59          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
60          */
61         public function __construct(array $media, string $url, array $indices)
62         {
63                 $this->display_url     = $media['url'];
64                 $this->expanded_url    = $media['url'];
65                 $this->id              = $media['id'];
66                 $this->id_str          = (string)$media['id'];
67                 $this->indices         = $indices;
68                 $this->media_url       = $media['url'];
69                 $this->media_url_https = $media['url'];
70                 $this->type            = $media['type'] == Post\Media::IMAGE ? 'photo' : 'video';
71                 $this->url             = $url;
72
73                 if (!empty($media['height']) && !empty($media['width'])) {
74                         if (($media['height'] <= 680) && ($media['width'] <= 680)) {
75                                 $size = 'small';
76                         } elseif (($media['height'] <= 1200) && ($media['width'] <= 1200)) {
77                                 $size = 'medium';
78                         } else {
79                                 $size = 'large';
80                         }
81
82                         $this->sizes = [
83                                 $size => [
84                                         'h'      => $media['height'],
85                                         'resize' => 'fit',
86                                         'w'      => $media['width'],
87                                 ]
88                         ];
89                 }
90         }
91
92         /**
93          * Returns the current entity as an array
94          *
95          * @return array
96          */
97         public function toArray(): array
98         {
99                 $status = parent::toArray();
100
101                 if (empty($status['indices'])) {
102                         unset($status['indices']);
103                 }
104
105                 return $status;
106         }
107 }