initial commit

This commit is contained in:
luke.rodham
2016-02-16 16:47:16 +00:00
commit 348669937b
8 changed files with 576 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.idea
/vendor

21
composer.json Normal file
View File

@@ -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"
}
}

241
composer.lock generated Normal file
View File

@@ -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": []
}

92
src/ApiWrapper.php Normal file
View File

@@ -0,0 +1,92 @@
<?php namespace LukeRodham\TrustPilot;
use GuzzleHttp\Client;
use LukeRodham\TrustPilot\Exceptions\InvalidApiCredentialsException;
class ApiWrapper
{
/**
* @var string
*/
private $apiKey;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $password;
/**
* @var Client
*/
private $client;
/**
* @var string
*/
private $baseUri = 'https://api.trustpilot.com/';
/**
* @var string
*/
private $businessUnitId;
/**
* ApiWrapper constructor.
*
* @param string $apiKey
* @param string $businessUnitId
* @param string $email
* @param string $password
*/
public function __construct($apiKey, $businessUnitId = '', $email = '', $password = '')
{
$this->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;
}
}

View File

@@ -0,0 +1,7 @@
<?php namespace LukeRodham\TrustPilot\Exceptions;
use Exception;
class InvalidApiCredentialsException extends Exception
{
}

44
src/Sections/Reviews.php Normal file
View File

@@ -0,0 +1,44 @@
<?php namespace LukeRodham\TrustPilot\Sections;
use LukeRodham\TrustPilot\ApiWrapper;
use LukeRodham\TrustPilot\Transformers\ReviewTransformer;
class Reviews
{
/**
* @var ApiWrapper
*/
private $client;
/**
* @param ApiWrapper $client
*/
public function __construct(ApiWrapper $client)
{
$this->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);
}
}

View File

@@ -0,0 +1,140 @@
<?php
namespace LukeRodham\TrustPilot\Transformers;
class ReviewTransformer
{
/**
* @var
*/
private $review;
/**
* @var
*/
private $companyReply;
/**
* @var
*/
private $createdAt;
/**
* @var
*/
private $reviewLink;
/**
* @var
*/
private $rating;
/**
* @return mixed
*/
public function getReview()
{
return $this->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;
}
}

29
src/TrustPilot.php Normal file
View File

@@ -0,0 +1,29 @@
<?php namespace LukeRodham\TrustPilot;
use LukeRodham\TrustPilot\Sections\Reviews;
class TrustPilot
{
private $client;
/**
* TrustPilot constructor.
*
* @param string $apiKey
* @param string $businessUnitId
* @param string $email
* @param string $password
*/
public function __construct($apiKey, $businessUnitId = '', $email = '', $password = '')
{
$this->client = new ApiWrapper($apiKey, $businessUnitId, $email, $password);
}
/**
* @return Reviews
*/
public function reviews()
{
return new Reviews($this->client);
}
}