commit 348669937bc7663e847874a08a50d6a7c253a7b7 Author: luke.rodham Date: Tue Feb 16 16:47:16 2016 +0000 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45b1244 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +/vendor \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9e047e6 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "lukerodham/trustpilot-api", + "keywords": ["trustpilot", "trust", "pilot"], + "description": "A TrustPilot API client", + "authors": [ + { + "name": "Luke Rodham", + "email": "lkrodham@gmail.com", + "homepage": "http://www.snowysocial.co.uk" + } + ], + "autoload": { + "psr-4": { + "LukeRodham\\TrustPilot\\": "src" + } + }, + "require": { + "php": ">=5.4", + "guzzlehttp/guzzle": "^6.1" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..15bd1c1 --- /dev/null +++ b/composer.lock @@ -0,0 +1,241 @@ +{ + "_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": "3fdb8c95c1982cdb8618ebe1b86a042c", + "content-hash": "93690a851bbcaf630586c9fe11914538", + "packages": [ + { + "name": "guzzlehttp/guzzle", + "version": "6.1.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "c6851d6e48f63b69357cbfa55bca116448140e0c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/c6851d6e48f63b69357cbfa55bca116448140e0c", + "reference": "c6851d6e48f63b69357cbfa55bca116448140e0c", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "~1.0", + "guzzlehttp/psr7": "~1.1", + "php": ">=5.5.0" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "~4.0", + "psr/log": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.1-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2015-11-23 00:47:50" + }, + { + "name": "guzzlehttp/promises", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "b1e1c0d55f8083c71eda2c28c12a228d708294ea" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/b1e1c0d55f8083c71eda2c28c12a228d708294ea", + "reference": "b1e1c0d55f8083c71eda2c28c12a228d708294ea", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2015-10-15 22:28:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.2.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "f5d04bdd2881ac89abde1fb78cc234bce24327bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5d04bdd2881ac89abde1fb78cc234bce24327bb", + "reference": "f5d04bdd2881ac89abde1fb78cc234bce24327bb", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "PSR-7 message implementation", + "keywords": [ + "http", + "message", + "stream", + "uri" + ], + "time": "2016-01-23 01:23:02" + }, + { + "name": "psr/http-message", + "version": "1.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", + "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2015-05-04 20:22:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.4" + }, + "platform-dev": [] +} diff --git a/src/ApiWrapper.php b/src/ApiWrapper.php new file mode 100644 index 0000000..2e24eff --- /dev/null +++ b/src/ApiWrapper.php @@ -0,0 +1,92 @@ +apiKey = $apiKey; + $this->email = $email; + $this->password = $password; + $this->businessUnitId = $businessUnitId; + + $this->client = new Client(['base_uri' => $this->baseUri]); + } + + /** + * @return Client + */ + public function getClient() + { + return $this->client; + } + + /** + * @return array + * @throws InvalidApiCredentialsException + */ + public function getDefaultHeaders() + { + return [ + 'headers' => [ + 'apikey' => $this->getApiKey() + ] + ]; + } + + /** + * @return string + * @throws InvalidApiCredentialsException + */ + private function getApiKey() + { + if (!$this->apiKey) { + throw new InvalidApiCredentialsException('No API key has been set.'); + } + + return $this->apiKey; + } + + /** + * @return string + */ + public function getBusinessUnitId() + { + return $this->businessUnitId; + } +} \ No newline at end of file diff --git a/src/Exceptions/InvalidApiCredentialsException.php b/src/Exceptions/InvalidApiCredentialsException.php new file mode 100644 index 0000000..3724e26 --- /dev/null +++ b/src/Exceptions/InvalidApiCredentialsException.php @@ -0,0 +1,7 @@ +client = $client; + } + + /** + * @param array $queryParams + * + * @return ReviewTransformer[] + */ + public function latest($queryParams = []) + { + $url = '/v1/reviews/latest'; + + if ($this->client->getBusinessUnitId() !== '') { + $url = '/v1/business-units/' . $this->client->getBusinessUnitId() . '/reviews'; + } + + $reviews = $this->client->getClient()->request( + 'GET', + $url, + array_merge($this->client->getDefaultHeaders(), ['query' => $queryParams]) + ); + + $reviews = json_decode($reviews->getBody()->getContents(), true); + + return (new ReviewTransformer())->transformArray($reviews); + } +} \ No newline at end of file diff --git a/src/Transformers/ReviewTransformer.php b/src/Transformers/ReviewTransformer.php new file mode 100644 index 0000000..7b67a67 --- /dev/null +++ b/src/Transformers/ReviewTransformer.php @@ -0,0 +1,140 @@ +review; + } + + /** + * @param mixed $review + */ + public function setReview($review) + { + $this->review = $review; + } + + /** + * @return mixed + */ + public function getCompanyReply() + { + return $this->companyReply; + } + + /** + * @param mixed $companyReply + */ + public function setCompanyReply($companyReply) + { + $this->companyReply = $companyReply; + } + + /** + * @return mixed + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + /** + * @param mixed $createdAt + */ + public function setCreatedAt($createdAt) + { + $this->createdAt = $createdAt; + } + + /** + * @return mixed + */ + public function getReviewLink() + { + return $this->reviewLink; + } + + /** + * @param mixed $reviewLink + */ + public function setReviewLink($reviewLink) + { + $this->reviewLink = $reviewLink; + } + + /** + * @return mixed + */ + public function getRating() + { + return $this->rating; + } + + /** + * @param mixed $rating + */ + public function setRating($rating) + { + $this->rating = $rating; + } + + /** + * @param array $reviews + * + * @return array + */ + public function transformArray(array $reviews) + { + $transformedData = []; + + foreach ($reviews['reviews'] as $review) { + + $transformedData[] = $this->transform($review); + } + + return $transformedData; + } + + /** + * @param $review + * + * @return ReviewTransformer + */ + public function transform($review) + { + $reviewObj = new self; + $reviewObj->setReview($review['text']); + $reviewObj->setCompanyReply($review['companyReply']['text']); + $reviewObj->setRating($review['stars']); + $reviewObj->setCreatedAt($review['createdAt']); + + return $reviewObj; + } +} \ No newline at end of file diff --git a/src/TrustPilot.php b/src/TrustPilot.php new file mode 100644 index 0000000..cacbe3a --- /dev/null +++ b/src/TrustPilot.php @@ -0,0 +1,29 @@ +client = new ApiWrapper($apiKey, $businessUnitId, $email, $password); + } + + /** + * @return Reviews + */ + public function reviews() + { + return new Reviews($this->client); + } +} \ No newline at end of file