Skip to content

Commit

Permalink
Merge pull request #23 from vfalies/companies
Browse files Browse the repository at this point in the history
Add Company methods & tests
  • Loading branch information
vfalies committed Jun 30, 2017
2 parents 89f3f25 + a8937d3 commit 2d8591d
Show file tree
Hide file tree
Showing 14 changed files with 644 additions and 11 deletions.
18 changes: 18 additions & 0 deletions src/Interfaces/Items/CompanyInterface.php
@@ -0,0 +1,18 @@
<?php

namespace vfalies\tmdb\Interfaces\Items;

interface CompanyInterface
{
public function getDescription();

public function getHeadQuarters();

public function getHomePage();

public function getId();

public function getLogoPath();

public function getName();
}
20 changes: 20 additions & 0 deletions src/Interfaces/Results/CompanyResultsInterface.php
@@ -0,0 +1,20 @@
<?php

namespace vfalies\tmdb\Interfaces\Results;

interface CompanyResultsInterface extends ResultsInterface
{

/**
* Get company name
* @return string
*/
public function getName();

/**
* Get company logo path
* @return string
*/
public function getLogoPath();

}
14 changes: 14 additions & 0 deletions src/Item.php
Expand Up @@ -6,6 +6,7 @@
use vfalies\tmdb\Items\Collection;
use vfalies\tmdb\Items\TVShow;
use vfalies\tmdb\Items\People;
use vfalies\tmdb\Items\Company;

class Item
{
Expand Down Expand Up @@ -80,4 +81,17 @@ public function getPeople($people_id, array $options = array())
return $people;
}

/**
* Get Company details
* @param int $company_id
* @param array $options
* @return \vfalies\tmdb\Items\Company
*/
public function getCompany($company_id, array $options = array())
{
$this->logger->debug('Starting getting company');
$company = new Company($this->tmdb, $company_id, $options);

return $company;
}
}
70 changes: 70 additions & 0 deletions src/Items/Company.php
@@ -0,0 +1,70 @@
<?php

namespace vfalies\tmdb\Items;

use vfalies\tmdb\Abstracts\Item;
use vfalies\tmdb\Interfaces\Items\CompanyInterface;
use vfalies\tmdb\Tmdb;

class Company extends Item implements CompanyInterface
{
/**
* Constructor
* @param \vfalies\tmdb\Tmdb $tmdb
* @param int $company_id
* @param array $options
*/
public function __construct(Tmdb $tmdb, $company_id, array $options = array())
{
parent::__construct($tmdb, $company_id, $options, 'company');
}

public function getDescription()
{
if (isset($this->data->description)) {
return $this->data->description;
}
return '';
}

public function getHeadQuarters()
{
if (isset($this->data->headquarters)) {
return $this->data->headquarters;
}
return '';
}

public function getHomePage()
{
if (isset($this->data->homepage)) {
return $this->data->homepage;
}
return '';
}

public function getId()
{
if (isset($this->data->id)) {
return $this->data->id;
}
return 0;
}

public function getLogoPath()
{
if (isset($this->data->logo_path)) {
return $this->data->logo_path;
}
return '';
}

public function getName()
{
if (isset($this->data->name)) {
return $this->data->name;
}
return '';
}

}
45 changes: 45 additions & 0 deletions src/Results/Company.php
@@ -0,0 +1,45 @@
<?php

namespace vfalies\tmdb\Results;

use vfalies\tmdb\Abstracts\Results;
use vfalies\tmdb\Tmdb;
use vfalies\tmdb\Interfaces\Results\CompanyResultsInterface;

class Company extends Results implements CompanyResultsInterface
{

protected $name = null;
protected $logo_path = null;

/**
* Constructor
* @param \vfalies\tmdb\Tmdb $tmdb
* @param \stdClass $result
*/
public function __construct(Tmdb $tmdb, \stdClass $result)
{
parent::__construct($tmdb, $result);

// Populate data
$this->id = $this->data->id;
$this->name = $this->data->name;
$this->logo_path = $this->data->logo_path;
}

public function getId()
{
return $this->id;
}

public function getLogoPath()
{
return $this->logo_path;
}

public function getName()
{
return $this->name;
}

}
54 changes: 43 additions & 11 deletions src/Search.php
Expand Up @@ -37,22 +37,26 @@ public function __construct(Tmdb $tmdb)
*/
private function searchItem($item, $query, array $options, $result_class)
{
try {
try
{
$this->logger->debug('Starting search item');
$query = trim($query);
if (empty($query)) {
if (empty($query))
{
$this->logger->error('Query param cannot be empty', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class));
throw new IncorrectParamException;
}
$params = $this->tmdb->checkOptions($options);
$response = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), 'search/' . $item, $query, $params);
$response = $this->tmdb->sendRequest(new HttpClient(new \GuzzleHttp\Client()), 'search/'.$item, $query, $params);

$this->page = (int) $response->page;
$this->total_pages = (int) $response->total_pages;
$this->total_results = (int) $response->total_results;

return $this->searchItemGenerator($response->results, $result_class);
} catch (TmdbException $ex) {
}
catch (TmdbException $ex)
{
throw $ex;
}
}
Expand All @@ -65,7 +69,8 @@ private function searchItem($item, $query, array $options, $result_class)
private function searchItemGenerator(array $results, $class)
{
$this->logger->debug('Starting search item generator');
foreach ($results as $result) {
foreach ($results as $result)
{
$element = new $class($this->tmdb, $result);

yield $element;
Expand All @@ -81,7 +86,8 @@ private function searchItemGenerator(array $results, $class)
*/
public function searchMovie($query, array $options = array())
{
try {
try
{
$this->logger->debug('Starting search movie');
return $this->searchItem('movie', $query, $options, Results\Movie::class);
} catch (TmdbException $ex) {
Expand All @@ -98,7 +104,8 @@ public function searchMovie($query, array $options = array())
*/
public function searchTVShow($query, array $options = array())
{
try {
try
{
$this->logger->debug('Starting search tv show');
return $this->searchItem('tv', $query, $options, Results\TVShow::class);
} catch (TmdbException $ex) {
Expand All @@ -115,7 +122,8 @@ public function searchTVShow($query, array $options = array())
*/
public function searchCollection($query, array $options = array())
{
try {
try
{
$this->logger->debug('Starting search collection');
return $this->searchItem('collection', $query, $options, Results\Collection::class);
} catch (TmdbException $ex) {
Expand All @@ -132,10 +140,33 @@ public function searchCollection($query, array $options = array())
*/
public function searchPeople($query, array $options = array())
{
try {
try
{
$this->logger->debug('Starting search people');
return $this->searchItem('people', $query, $options, Results\People::class);
} catch (TmdbException $ex) {
return $this->searchItem('people', $query, $options, Results\People::class);
}
catch (TmdbException $ex)
{
throw $ex;
}
}

/**
* Search a company
* @param string $query Query string to search like a company
* @param array $options Array of option for the request
* @return \Generator|Results\Company
* @throws TmdbException
*/
public function searchCompany($query, array $options = array())
{
try
{
$this->logger->debug('Starting search company');
return $this->searchItem('people', $query, $options, Results\Company::class);
}
catch (TmdbException $ex)
{
throw $ex;
}
}
Expand Down Expand Up @@ -166,4 +197,5 @@ public function getTotalResults()
{
return $this->total_results;
}

}

0 comments on commit 2d8591d

Please sign in to comment.