|
1 | 1 | <?php
|
2 | 2 | /**
|
3 | 3 | * Base Adapter extended by all adapters
|
4 |
| - * |
| 4 | + * |
5 | 5 | * Provide default functionalities
|
6 | 6 | */
|
7 | 7 | namespace Embed\Adapters;
|
8 | 8 |
|
9 | 9 | use Embed\Url;
|
10 | 10 | use Embed\FastImage;
|
11 | 11 |
|
12 |
| -abstract class Adapter { |
13 |
| - public $providers = array(); |
14 |
| - public $options = array( |
15 |
| - 'minImageWidth' => 0, |
16 |
| - 'minImageHeight' => 0, |
17 |
| - 'getBiggerImage' => false, |
18 |
| - 'getBiggerIcon' => false, |
19 |
| - 'facebookAccessToken' => null, |
20 |
| - 'soundcloudClientId' => null, |
21 |
| - 'embedlyKey' => null |
22 |
| - ); |
23 |
| - |
24 |
| - abstract protected function initProviders (Url $Url); |
25 |
| - |
26 |
| - public function __construct (Url $Url, array $options = null) { |
27 |
| - if ($options !== null) { |
28 |
| - $this->options = array_replace($this->options, $options); |
29 |
| - } |
30 |
| - |
31 |
| - $this->initProviders($Url); |
32 |
| - |
33 |
| - if ($Url->getUrl() !== $this->url) { |
34 |
| - $this->initProviders(new Url($this->url)); |
35 |
| - } |
36 |
| - } |
37 |
| - |
38 |
| - public function __get ($name) { |
39 |
| - $method = 'get'.$name; |
40 |
| - |
41 |
| - if (method_exists($this, $method)) { |
42 |
| - return $this->$name = $this->$method(); |
43 |
| - } |
44 |
| - } |
45 |
| - |
46 |
| - public function getFromProviders ($name) { |
47 |
| - $method = 'get'.$name; |
48 |
| - |
49 |
| - foreach ($this->providers as $Provider) { |
50 |
| - if (($value = $Provider->$method())) { |
51 |
| - return $value; |
52 |
| - } |
53 |
| - } |
54 |
| - } |
55 |
| - |
56 |
| - public function getUrlFromProviders ($name) { |
57 |
| - $method = 'get'.$name; |
58 |
| - |
59 |
| - foreach ($this->providers as $Provider) { |
60 |
| - if (($url = $Provider->$method())) { |
61 |
| - return $this->Url->getAbsolute($url); |
62 |
| - } |
63 |
| - } |
64 |
| - } |
65 |
| - |
66 |
| - public function getTitle () { |
67 |
| - return $this->getFromProviders('title') ?: $this->Url->getUrl(); |
68 |
| - } |
69 |
| - |
70 |
| - public function getDescription () { |
71 |
| - return $this->getFromProviders('description'); |
72 |
| - } |
73 |
| - |
74 |
| - public function getUrl () { |
75 |
| - return $this->getUrlFromProviders('url') ?: $this->Url->getUrl(); |
76 |
| - } |
77 |
| - |
78 |
| - public function getSource () { |
79 |
| - return $this->getUrlFromProviders('source'); |
80 |
| - } |
81 |
| - |
82 |
| - public function getAuthorName () { |
83 |
| - return $this->getFromProviders('authorName'); |
84 |
| - } |
85 |
| - |
86 |
| - public function getAuthorUrl () { |
87 |
| - return $this->getUrlFromProviders('authorUrl'); |
88 |
| - } |
89 |
| - |
90 |
| - public function getAspectRatio () { |
91 |
| - $width = $this->width; |
92 |
| - $height = $this->height; |
93 |
| - |
94 |
| - if ($width && (strpos($width, '%') === false) && $height && (strpos($height, '%') === false)) { |
95 |
| - return round(($height / $width) * 100, 3); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - public function getImage () { |
100 |
| - foreach ($this->images as $image) { |
101 |
| - try { |
102 |
| - $Image = new FastImage($image); |
103 |
| - } catch (\Exception $Exception) { |
104 |
| - continue; |
105 |
| - } |
106 |
| - |
107 |
| - if ($Image->getType()) { |
108 |
| - list($width, $height) = $Image->getSize(); |
109 |
| - |
110 |
| - if (($width >= $this->options['minImageWidth']) && ($height >= $this->options['minImageHeight'])) { |
111 |
| - $this->imageWidth = $width; |
112 |
| - $this->imageHeight = $height; |
113 |
| - |
114 |
| - return $image; |
115 |
| - } |
116 |
| - } |
117 |
| - } |
118 |
| - } |
119 |
| - |
120 |
| - public function getProviderIcon () { |
121 |
| - if ($this->options['getBiggerIcon']) { |
122 |
| - $icons = FastImage::sortImagesBySize($this->providerIcons); |
123 |
| - |
124 |
| - return current($icons); |
125 |
| - } |
126 |
| - |
127 |
| - foreach ($this->providerIcons as $icon) { |
128 |
| - try { |
129 |
| - $Icon = new FastImage($icon); |
130 |
| - } catch (\Exception $Exception) { |
131 |
| - continue; |
132 |
| - } |
133 |
| - |
134 |
| - if ($Icon->getType()) { |
135 |
| - return $icon; |
136 |
| - } |
137 |
| - } |
138 |
| - } |
139 |
| - |
140 |
| - public function getProviderName () { |
141 |
| - return $this->getFromProviders('providerName') ?: $this->Url->getDomain(); |
142 |
| - } |
143 |
| - |
144 |
| - public function getProviderUrl () { |
145 |
| - return $this->getUrlFromProviders('providerUrl') ?: ($this->Url->getScheme().'://'.$this->Url->getDomain(true)); |
146 |
| - } |
147 |
| - |
148 |
| - public function getImageWidth () { |
149 |
| - return null; |
150 |
| - } |
151 |
| - |
152 |
| - public function getImageHeight () { |
153 |
| - return null; |
154 |
| - } |
155 |
| - |
156 |
| - public function getWidth () { |
157 |
| - return $this->getFromProviders('width'); |
158 |
| - } |
159 |
| - |
160 |
| - public function getHeight () { |
161 |
| - return $this->getFromProviders('height'); |
162 |
| - } |
| 12 | +abstract class Adapter |
| 13 | +{ |
| 14 | + public $providers = array(); |
| 15 | + public $options = array( |
| 16 | + 'minImageWidth' => 0, |
| 17 | + 'minImageHeight' => 0, |
| 18 | + 'getBiggerImage' => false, |
| 19 | + 'getBiggerIcon' => false, |
| 20 | + 'facebookAccessToken' => null, |
| 21 | + 'soundcloudClientId' => null, |
| 22 | + 'embedlyKey' => null |
| 23 | + ); |
| 24 | + |
| 25 | + abstract protected function initProviders (Url $Url); |
| 26 | + |
| 27 | + public function __construct(Url $Url, array $options = null) |
| 28 | + { |
| 29 | + if ($options !== null) { |
| 30 | + $this->options = array_replace($this->options, $options); |
| 31 | + } |
| 32 | + |
| 33 | + $this->initProviders($Url); |
| 34 | + |
| 35 | + if ($Url->getUrl() !== $this->url) { |
| 36 | + $this->initProviders(new Url($this->url)); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public function __get($name) |
| 41 | + { |
| 42 | + $method = 'get'.$name; |
| 43 | + |
| 44 | + if (method_exists($this, $method)) { |
| 45 | + return $this->$name = $this->$method(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public function getFromProviders($name) |
| 50 | + { |
| 51 | + $method = 'get'.$name; |
| 52 | + |
| 53 | + foreach ($this->providers as $Provider) { |
| 54 | + if (($value = $Provider->$method())) { |
| 55 | + return $value; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public function getUrlFromProviders($name) |
| 61 | + { |
| 62 | + $method = 'get'.$name; |
| 63 | + |
| 64 | + foreach ($this->providers as $Provider) { |
| 65 | + if (($url = $Provider->$method())) { |
| 66 | + return $this->Url->getAbsolute($url); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public function getTitle() |
| 72 | + { |
| 73 | + return $this->getFromProviders('title') ?: $this->Url->getUrl(); |
| 74 | + } |
| 75 | + |
| 76 | + public function getDescription() |
| 77 | + { |
| 78 | + return $this->getFromProviders('description'); |
| 79 | + } |
| 80 | + |
| 81 | + public function getUrl() |
| 82 | + { |
| 83 | + return $this->getUrlFromProviders('url') ?: $this->Url->getUrl(); |
| 84 | + } |
| 85 | + |
| 86 | + public function getSource() |
| 87 | + { |
| 88 | + return $this->getUrlFromProviders('source'); |
| 89 | + } |
| 90 | + |
| 91 | + public function getAuthorName() |
| 92 | + { |
| 93 | + return $this->getFromProviders('authorName'); |
| 94 | + } |
| 95 | + |
| 96 | + public function getAuthorUrl() |
| 97 | + { |
| 98 | + return $this->getUrlFromProviders('authorUrl'); |
| 99 | + } |
| 100 | + |
| 101 | + public function getAspectRatio() |
| 102 | + { |
| 103 | + $width = $this->width; |
| 104 | + $height = $this->height; |
| 105 | + |
| 106 | + if ($width && (strpos($width, '%') === false) && $height && (strpos($height, '%') === false)) { |
| 107 | + return round(($height / $width) * 100, 3); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public function getImage() |
| 112 | + { |
| 113 | + foreach ($this->images as $image) { |
| 114 | + try { |
| 115 | + $Image = new FastImage($image); |
| 116 | + } catch (\Exception $Exception) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + if ($Image->getType()) { |
| 121 | + list($width, $height) = $Image->getSize(); |
| 122 | + |
| 123 | + if (($width >= $this->options['minImageWidth']) && ($height >= $this->options['minImageHeight'])) { |
| 124 | + $this->imageWidth = $width; |
| 125 | + $this->imageHeight = $height; |
| 126 | + |
| 127 | + return $image; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public function getProviderIcon() |
| 134 | + { |
| 135 | + if ($this->options['getBiggerIcon']) { |
| 136 | + $icons = FastImage::sortImagesBySize($this->providerIcons); |
| 137 | + |
| 138 | + return current($icons); |
| 139 | + } |
| 140 | + |
| 141 | + foreach ($this->providerIcons as $icon) { |
| 142 | + try { |
| 143 | + $Icon = new FastImage($icon); |
| 144 | + } catch (\Exception $Exception) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + if ($Icon->getType()) { |
| 149 | + return $icon; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public function getProviderName() |
| 155 | + { |
| 156 | + return $this->getFromProviders('providerName') ?: $this->Url->getDomain(); |
| 157 | + } |
| 158 | + |
| 159 | + public function getProviderUrl() |
| 160 | + { |
| 161 | + return $this->getUrlFromProviders('providerUrl') ?: ($this->Url->getScheme().'://'.$this->Url->getDomain(true)); |
| 162 | + } |
| 163 | + |
| 164 | + public function getImageWidth() |
| 165 | + { |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
| 169 | + public function getImageHeight() |
| 170 | + { |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + public function getWidth() |
| 175 | + { |
| 176 | + return $this->getFromProviders('width'); |
| 177 | + } |
| 178 | + |
| 179 | + public function getHeight() |
| 180 | + { |
| 181 | + return $this->getFromProviders('height'); |
| 182 | + } |
163 | 183 | }
|
0 commit comments