From 8631073a7b641431047359c0aa933d83d37365e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20D=C3=BChr?= Date: Sun, 22 Oct 2017 00:20:29 +0200 Subject: [PATCH] init --- .gitignore | 1 + composer.json | 11 ++++++ composer.lock | 77 ++++++++++++++++++++++++++++++++++++ index.php | 33 ++++++++++++++++ src/FacebookPageProvider.php | 41 +++++++++++++++++++ src/GooglePlaceProvider.php | 36 +++++++++++++++++ src/Rating.php | 9 +++++ src/StarRating.php | 31 +++++++++++++++ src/iRatingProvider.php | 7 ++++ 9 files changed, 246 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 index.php create mode 100644 src/FacebookPageProvider.php create mode 100644 src/GooglePlaceProvider.php create mode 100644 src/Rating.php create mode 100644 src/StarRating.php create mode 100644 src/iRatingProvider.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ead86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..25b3f96 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "require": { + "facebook/graph-sdk": "^5.6" + }, + "autoload": { + "psr-4": { + "DieSchittigs\\StarScraper\\": "src/" + } + } + +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..7ed8300 --- /dev/null +++ b/composer.lock @@ -0,0 +1,77 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "876aa854c4ce02081a031bb2a00f5579", + "content-hash": "0719d5c0f2ea9b27280d086fced50c4d", + "packages": [ + { + "name": "facebook/graph-sdk", + "version": "5.6.1", + "source": { + "type": "git", + "url": "https://github.com/facebook/php-graph-sdk.git", + "reference": "2f9639c15ae043911f40ffe44080b32bac2c5280" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/facebook/php-graph-sdk/zipball/2f9639c15ae043911f40ffe44080b32bac2c5280", + "reference": "2f9639c15ae043911f40ffe44080b32bac2c5280", + "shasum": "" + }, + "require": { + "php": "^5.4|^7.0" + }, + "require-dev": { + "guzzlehttp/guzzle": "~5.0", + "mockery/mockery": "~0.8", + "phpunit/phpunit": "~4.0" + }, + "suggest": { + "guzzlehttp/guzzle": "Allows for implementation of the Guzzle HTTP client", + "paragonie/random_compat": "Provides a better CSPRNG option in PHP 5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.x-dev" + } + }, + "autoload": { + "psr-4": { + "Facebook\\": "src/Facebook/" + }, + "files": [ + "src/Facebook/polyfills.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Facebook Platform" + ], + "authors": [ + { + "name": "Facebook", + "homepage": "https://github.com/facebook/php-graph-sdk/contributors" + } + ], + "description": "Facebook SDK for PHP", + "homepage": "https://github.com/facebook/php-graph-sdk", + "keywords": [ + "facebook", + "sdk" + ], + "time": "2017-08-16 17:28:07" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..8afd66c --- /dev/null +++ b/index.php @@ -0,0 +1,33 @@ +addProvider( + new GooglePlaceProvider( + '{{GoogleApiKey}}', + '{{GoogleMapsPlaceID}}' + ) +); +$starRating->addProvider( + new FacebookPageProvider( + '{{FacebookAppID}}', + '{{FacebookAppSecret}}', + '{{FacebookPage}}' + ) +); + +$rating = $starRating->getRating(); + +echo ' +"aggregateRating": { + "@type": "AggregateRating", + "bestRating": "'. $rating->bestRating .'", + "ratingCount": "'. $rating->ratingCount .'", + "ratingValue": "'. $rating->ratingValue .'" +} +'; diff --git a/src/FacebookPageProvider.php b/src/FacebookPageProvider.php new file mode 100644 index 0000000..e011b81 --- /dev/null +++ b/src/FacebookPageProvider.php @@ -0,0 +1,41 @@ + $appID, + 'app_secret' => $appSecret, + 'default_graph_version' => 'v2.10', + 'default_access_token' => "$appID|$appSecret" + ]); + try { + $response = $fb->get("$pageId?fields=overall_star_rating,rating_count"); + $this->result = $response->getDecodedBody(); + } catch(\Facebook\Exceptions\FacebookResponseException $e) { + echo 'Graph returned an error: ' . $e->getMessage(); + } catch(\Facebook\Exceptions\FacebookSDKException $e) { + echo 'Facebook SDK returned an error: ' . $e->getMessage(); + } + } + public function getRating(){ + if( + !$this->result || + !$this->result['id'] || + !$this->result['rating_count'] || + !$this->result['overall_star_rating'] + ) return null; + $rating = new Rating(); + $rating->bestRating = $this->BEST_RATING; + $rating->ratingCount = $this->result['rating_count']; + $rating->ratingValue = $this->result['overall_star_rating']; + return $rating; + } +} + diff --git a/src/GooglePlaceProvider.php b/src/GooglePlaceProvider.php new file mode 100644 index 0000000..66fc0b6 --- /dev/null +++ b/src/GooglePlaceProvider.php @@ -0,0 +1,36 @@ +result->reviews; + } catch (\Exception $e){ + echo 'Google API returned an error: ' . $e->getMessage(); + } + if($placeReviews && is_array($placeReviews) && !empty($placeReviews)){ + $this->reviews = $placeReviews; + } + } + public function getRating(){ + if(!$this->reviews) return null; + $rating = new Rating(); + $rating->bestRating = $this->BEST_RATING; + $rating->ratingCount = count($this->reviews); + $score = 0; + foreach($this->reviews as $review){ + $score += $review->rating; + } + $rating->ratingValue = $score / $rating->ratingCount; + return $rating; + } +} + diff --git a/src/Rating.php b/src/Rating.php new file mode 100644 index 0000000..9f34dde --- /dev/null +++ b/src/Rating.php @@ -0,0 +1,9 @@ +best_rating = $best_rating; + } + public function addProvider(iRatingProvider $provider){ + $this->providers[] = $provider; + } + public function getRating(){ + if(!$this->providers) return null; + $rating = new Rating; + $rating->bestRating = $this->best_rating; + $rating->ratingCount = 0; + $score = 0; + $validProviders = 0; + foreach($this->providers as $provider){ + $_rating = $provider->getRating(); + if(!$_rating) continue; + $rating->ratingCount += $_rating->ratingCount; + $score += $_rating->ratingValue / $_rating->bestRating * $rating->bestRating; + $validProviders ++; + } + $rating->ratingValue = $score / $validProviders; + return $rating; + } +} \ No newline at end of file diff --git a/src/iRatingProvider.php b/src/iRatingProvider.php new file mode 100644 index 0000000..b77e12d --- /dev/null +++ b/src/iRatingProvider.php @@ -0,0 +1,7 @@ +