mirror of
https://github.com/cubixle/trustpilot-api-wrapper.git
synced 2026-04-29 13:08:39 +01:00
initial commit
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php namespace LukeRodham\TrustPilot\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class InvalidApiCredentialsException extends Exception
|
||||
{
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user