diff --git a/README.md b/README.md index 70c204a..80d20aa 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,18 @@ BingPhoto is a simple PHP class to fetch Bing's image of the day with meta data. It is also possible to cache the images locally, which can be useful in combination with a periodic cronjob. See the `cacheDir` parameter for this (optional) feature. Disclaimer: this might be a copyright issue. +## Installation + +Use [Composer](https://getcomposer.org/) to install this package: + +```sh +composer require grubersjoe/bing-daily-photo +``` + ## Basic usage ```php -$bing = new BingPhoto(); +$bing = new grubersjoe\BingPhoto(); $image = $bing->getImage(); // Example result ($image) @@ -41,9 +49,9 @@ Breaking change: the parameter `resolution` was renamed to `quality`. See also t ```php // Fetches two images of the day starting yesterday from Bing -$bing = new BingPhoto([ +$bing = new grubersjoe\BingPhoto([ 'n' => 2, - 'date' => BingPhoto::YESTERDAY + 'date' => grubersjoe\BingPhoto::YESTERDAY ]); foreach ($bing->getImages() as $image) { @@ -53,9 +61,9 @@ foreach ($bing->getImages() as $image) { ```php // Fetches the current image of the day in low resolution from the French Bing portal -$bing = new BingPhoto([ +$bing = new grubersjoe\BingPhoto([ 'locale' => 'fr-FR', - 'quality' => BingPhoto::QUALITY_LOW, + 'quality' => grubersjoe\BingPhoto::QUALITY_LOW, ]); printf('', $bing->getImage()['url']); @@ -63,11 +71,11 @@ printf('', $bing->getImage()['url']); ```php // Fetches three images of the day in high quality from the German Bing portal, starting yesterday -$bing = new BingPhoto([ +$bing = new grubersjoe\BingPhoto([ 'n' => 3, - 'date' => BingPhoto::YESTERDAY, + 'date' => grubersjoe\BingPhoto::YESTERDAY, 'locale' => 'de-DE', - 'quality' => BingPhoto::QUALITY_HIGH, + 'quality' => grubersjoe\BingPhoto::QUALITY_HIGH, ]); foreach ($bing->getImages() as $image) { @@ -78,9 +86,9 @@ foreach ($bing->getImages() as $image) { ```php // Using the local cache // (remember to create the directory first!) -$bing = new BingPhoto([ +$bing = new grubersjoe\BingPhoto([ 'cacheDir' => '/tmp/bing-photo', 'n' => 5, - 'quality' => BingPhoto::QUALITY_LOW, + 'quality' => grubersjoe\BingPhoto::QUALITY_LOW, ]); ``` diff --git a/composer.json b/composer.json index 7bd4dbe..5e0e930 100644 --- a/composer.json +++ b/composer.json @@ -1,4 +1,22 @@ { + "name": "grubersjoe/bing-daily-photo", + "description": "A class to fetch Bing's image of the day with meta data", + "keywords": ["bing", "daily", "photo"], + "license": "MIT", + "authors": [ + { + "name": "Jonathan Gruber", + "homepage": "https://grubersjoe.de" + } + ], + "autoload": { + "psr-4": { + "grubersjoe\\": "src/" + } + }, + "require": { + "php": "^5.6.0 || ^7.0" + }, "require-dev": { "phpunit/phpunit": "^7.0" } diff --git a/BingPhoto.php b/src/BingPhoto.php similarity index 93% rename from BingPhoto.php rename to src/BingPhoto.php index 28f4cef..83c49e7 100644 --- a/BingPhoto.php +++ b/src/BingPhoto.php @@ -1,5 +1,7 @@ cacheImages(); } else { - throw new Exception(sprintf('Given cache directory %s does not exist', $cacheDir)); + throw new \Exception(sprintf('Given cache directory %s does not exist', $cacheDir)); } } } @@ -100,7 +102,7 @@ private function setArgs(array $args) $defaultArgs = [ 'cacheDir' => false, 'date' => self::TODAY, - 'locale' => str_replace('_', '-', Locale::getDefault()), + 'locale' => str_replace('_', '-', \Locale::getDefault()), 'n' => 1, 'quality' => self::QUALITY_HIGH, ]; @@ -126,7 +128,7 @@ private function sanitizeArgs(array $args) /** * Fetches the image meta data from Bing (JSON) - * @throws Exception + * @throws \Exception */ private function fetchImagesMetadata() { @@ -140,7 +142,7 @@ private function fetchImagesMetadata() $this->setAbsoluteUrl(); $this->setQuality(); } else { - throw new Exception('Unable to retrieve JSON data: ' . $error); + throw new \Exception('Unable to retrieve JSON data: ' . $error); } } @@ -153,14 +155,14 @@ private function cacheImages() $fetchList = []; // Build a list of to be cached dates - $baseDate = (new DateTime())->modify(sprintf('-%d day', $this->args['date'] - 1)); + $baseDate = (new \DateTime())->modify(sprintf('-%d day', $this->args['date'] - 1)); for ($i = 0; $i < $this->args['n']; $i++) { $date = $baseDate->modify('-1 day')->format('Ymd'); $fetchList[$date] = true; } // Check current cache - $dirIterator = new DirectoryIterator($this->args['cacheDir']); + $dirIterator = new \DirectoryIterator($this->args['cacheDir']); foreach ($dirIterator as $image) { if ($image->isFile() && $image->getExtension() === 'jpg') { $imageShouldBeCached = in_array($image->getBasename('.jpg'), array_keys($fetchList)); @@ -198,7 +200,7 @@ private function fetchImageFiles(array $fetchList) } } } - } catch (Exception $e) { + } catch (\Exception $e) { error_log($e->getMessage()); exit($e->getMessage()); } diff --git a/BingPhotoTest.php b/src/BingPhotoTest.php similarity index 92% rename from BingPhotoTest.php rename to src/BingPhotoTest.php index a238db5..92cb432 100644 --- a/BingPhotoTest.php +++ b/src/BingPhotoTest.php @@ -1,5 +1,7 @@ assertCount($count, $bingPhoto->getImages()); } @@ -40,21 +42,22 @@ public function testCount($args = []) /** * @dataProvider qualityArgsProvider * @param $args - * @throws Exception + * @throws \Exception */ public function testQuality($args = []) { $bingPhoto = new BingPhoto($args); foreach ($bingPhoto->getImages() as $image) { list($width, $height) = getimagesize($image['url']); - $this->assertEquals($width . 'x' . $height, $args['quality'] ?? BingPhoto::QUALITY_HIGH); + $quality = isset($args['quality']) ? $args['quality'] : BingPhoto::QUALITY_HIGH; + $this->assertEquals($width . 'x' . $height, $quality); } } /** * @dataProvider cacheArgsProvider * @param $args - * @throws Exception + * @throws \Exception */ public function testCache($args = []) { @@ -97,12 +100,12 @@ public function testCache($args = []) /** * @dataProvider invalidCacheArgsProvider * @param $args - * @throws Exception + * @throws \Exception */ public function testInvalidCache($args = []) { if (!empty($args['cacheDir']) && !file_exists($args['cacheDir'])) { - $this->expectException(Exception::class); + $this->expectException(\Exception::class); } $bingPhoto = new BingPhoto($args);