Skip to content

Commit

Permalink
Implementing Jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 5, 2014
1 parent 0790840 commit 73d9ce5
Show file tree
Hide file tree
Showing 8 changed files with 861 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/jobs/model/all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
require_once('../../../vendor/autoload.php');
require_once('../../../apikey.php');

$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$repository = new \Tmdb\Repository\JobsRepository($client);
$jobs = $repository->loadCollection();

foreach($jobs as $job) {
var_dump($job);
}
45 changes: 45 additions & 0 deletions lib/Tmdb/Factory/JobsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Factory;

use Tmdb\Model\Collection\Jobs;
use Tmdb\Model\Job;

class JobsFactory extends AbstractFactory
{
/**
* {@inheritdoc}
*/
public function create(array $data = array())
{
return $this->hydrate(new Job(), $data);
}

/**
* {@inheritdoc}
*/
public function createCollection(array $data = array())
{
$collection = new Jobs();

if (array_key_exists('jobs', $data)) {
$data = $data['jobs'];
}

foreach($data as $item) {
$collection->add(null, $this->create($item));
}

return $collection;
}
}
87 changes: 87 additions & 0 deletions lib/Tmdb/Model/Collection/Jobs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Collection;

use Tmdb\Model\Common\GenericCollection;

class Jobs extends GenericCollection {

/**
* Filter by department
*
* @param string $department
* @return $this
*/
public function filterByDepartment($department)
{
$result = $this->filter(
function($key, $value) use ($department) {
if ($value->getDepartment() == $department) { return true; }
}
);

if ($result && 1 === count($result)) {
return array_shift($result->toArray());
}

return $result;
}

/**
* Filter by department and return the jobs collection
*
* @param string $department
* @return $this
*/
public function filterByDepartmentAndReturnJobsList($department)
{
$result = $this->filter(
function($key, $value) use ($department) {
if ($value->getDepartment() == $department) { return true; }
}
);

if ($result && 1 === count($result)) {
$data = array_shift($result->toArray());

return $data->getJobList();
}

return $result;
}

/**
* Filter by job
*
* @param string $filterByJob
* @return $this
*/
public function filterByJob($filterByJob)
{
$result = $this->filter(
function($key, $value) use ($filterByJob) {
$jobList = $value->getJobList();

foreach($jobList as $job) {
if ($filterByJob == $job) { return true; }
}
}
);

if ($result && 1 === count($result)) {
return array_shift($result->toArray());
}

return $result;
}
}
65 changes: 65 additions & 0 deletions lib/Tmdb/Model/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model;

class Job extends AbstractModel {

public static $_properties = array(
'department',
'job_list'
);

/**
* @var string
*/
private $department;

/**
* @var array
*/
private $jobList;

/**
* @param string $department
* @return $this
*/
public function setDepartment($department)
{
$this->department = $department;
}

/**
* @return string
*/
public function getDepartment()
{
return $this->department;
}

/**
* @param array $jobList
* @return $this
*/
public function setJobList(array $jobList)
{
$this->jobList = $jobList;
}

/**
* @return array
*/
public function getJobList()
{
return $this->jobList;
}
}
70 changes: 70 additions & 0 deletions lib/Tmdb/Repository/JobsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Repository;

use Tmdb\Factory\JobsFactory;
use Tmdb\Model\Collection\Jobs;
use Tmdb\Model\Job;

class JobsRepository extends AbstractRepository {
/**
* @param array $parameters
* @param array $headers
* @return Job
*/
public function load(array $parameters = array(), array $headers = array()) {
return $this->loadCollection($parameters, $headers);
}

/**
* Get the list of jobs.
*
* @param array $parameters
* @param array $headers
* @return Jobs|Job[]
*/
public function loadCollection(array $parameters = array(), array $headers = array())
{
return $this->createCollection(
$this->getApi()->getJobs($parameters, $headers)
);
}

/**
* Create an collection of an array
*
* @param $data
* @return Jobs|Job[]
*/
private function createCollection($data){
return $this->getFactory()->createCollection($data);
}

/**
* Return the related API class
*
* @return \Tmdb\Api\Jobs
*/
public function getApi()
{
return $this->getClient()->getJobsApi();
}

/**
* @return JobsFactory
*/
public function getFactory()
{
return new JobsFactory();
}
}
Loading

0 comments on commit 73d9ce5

Please sign in to comment.