Skip to content

Commit 7681347

Browse files
author
Jan Galek
committed
Added: option to turn off throw exception
1 parent a643d0b commit 7681347

5 files changed

Lines changed: 65 additions & 6 deletions

File tree

docs/en/usage.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,23 @@ Every properties you find in [Video documentation](https://github.com/TroiaStudi
7979
</div>
8080
```
8181

82+
### Exception
83+
you can turn of throw exception and get empty video object
84+
85+
#### Basic
86+
```php
87+
$apiKey = 'MySuperSecretYoutubeApiKey';
88+
$youtube = new \TroiaStudio\YoutubeAPI\Reader($apiKey, null, false);
89+
```
90+
91+
#### Nette
92+
93+
config.neon
94+
```neon
95+
extensions:
96+
youtubeAPI: TroiaStudio\YoutubeAPI\DI\Extension
97+
98+
youtubeAPI:
99+
apiKey: 'MySuperSecretYoutubeApiKey'
100+
exception: false
101+
```

src/TroiaStudio/Utils/Youtube-API/DI/Extension.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ public function loadConfiguration()
1818
$builder = $this->getContainerBuilder();
1919
$config = $this->getConfig([
2020
'apiKey' => null,
21-
'httpClient' => null
21+
'httpClient' => null,
22+
'exception' => true
2223
]);
2324

2425
$builder->addDefinition($this->prefix('troiastudioyoutubeapi'))
2526
->setClass('TroiaStudio\YoutubeAPI\Reader', [
26-
'apiKey' => $config['apiKey'],
27-
'httpClient' => $config['httpClient']
27+
'apiKey' => $config['apiKey'],
28+
'httpClient' => $config['httpClient'],
29+
'exception' => $config['exception']
2830
]);
2931
}
3032
}

src/TroiaStudio/Utils/Youtube-API/Reader.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ class Reader
4646
/** @var Client */
4747
private $httpClient;
4848

49-
public function __construct($apiKey = null, $httpClient = null)
49+
/** @var bool */
50+
private $exception;
51+
52+
public function __construct($apiKey = null, $httpClient = null, $exception = true)
5053
{
5154
if ($apiKey !== null) {
5255
$this->apiKey = $apiKey;
@@ -58,6 +61,7 @@ public function __construct($apiKey = null, $httpClient = null)
5861
]);
5962
}
6063
$this->httpClient = $httpClient;
64+
$this->exception = $exception;
6165
}
6266

6367
public function setUrl($url)
@@ -145,7 +149,11 @@ private function createVideo($data, $videoId)
145149
$json = Nette\Utils\Json::decode($data);
146150

147151
if (!isset($json->items[0]->snippet) || !isset($json->items[0]->contentDetails) || !isset($json->items[0]->status) || !isset($json->items[0]->statistics)) {
148-
throw new \RuntimeException("Empty YouTube response, probably wrong '{$videoId}' video id.");
152+
if ($exception) {
153+
throw new \RuntimeException("Empty YouTube response, probably wrong '{$videoId}' video id.");
154+
} else {
155+
$this->videos[$videoId] = new Video;
156+
}
149157
}
150158

151159
$snippet = $json->items[0]->snippet;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace TroiaStudio\YoutubeAPI;
3+
4+
use Nette;
5+
6+
class Thumbnail
7+
{
8+
/** @var string */
9+
public $url = '';
10+
11+
/** @var integer */
12+
public $width = 0;
13+
14+
/** @var integer */
15+
public $height = 0;
16+
}

src/TroiaStudio/Utils/Youtube-API/Video.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,18 @@ class Video
4545
/**
4646
* @var array
4747
*/
48-
public $thumbs;
48+
public $thumbs = [
49+
'default' => null,
50+
'medium' => null,
51+
'high' => null,
52+
'standard' => null,
53+
'maxres' => null
54+
];
55+
56+
public function __construct()
57+
{
58+
foreach ($this->thumbs as $index => $thumb) {
59+
$this->thumbs[$index] = new Thumbnail;
60+
}
61+
}
4962
}

0 commit comments

Comments
 (0)