Skip to content

Instantly share code, notes, and snippets.

@craigtaub
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigtaub/9639342 to your computer and use it in GitHub Desktop.
Save craigtaub/9639342 to your computer and use it in GitHub Desktop.
<?php
/***** CONTROLLER ACTIONS ******/
class Tviplayer_HomepageController
{
public function homeHighlightsAction() {
$homeHighlightsObject = new BBC_Tviplayer_Models_Highlights_Home();
$homeHighlights = $homeHighlightsObject->fetch($params = array());
foreach($homeHighlights as $item) {
if($item instanceof BBC_Tviplayer_Models_Episode) {
var_dump($item->getMasterbrand());
}
}
}
public function episodesAction() {
$episodeObject = new BBC_Tviplayer_Models_Episode();
$episodeArray = $episodeObject->fetch($params = array());
foreach($episodeArray as $episode) {
var_dump($episode->getMasterbrand() );
}
}
public function channelAction() {
$channelObject = new BBC_Tviplayer_Models_Channel();
$channelArray = $channelObject->fetch($params = array());
foreach($channelArray as $channel) {
var_dump($channel->getTitle() );
}
}
}
/***** BAMBOO MODELS ******/
class Bamboo_Models_Base
{
public $version;
public $schema;
public $timestamp;
public function buildMeta($object) {
$this->version = $object->version;
$this->schema = $object->schema;
$this->timestamp = $object->timestamp;
}
public function buildElements($elements) {
$array = array();
foreach ($elements as $element) {
$className = get_class($this);
$object = new $className;
$object->buildModel($element);
$array[] = $object;
}
return $array;
}
}
interface Bamboo_Models_Interface
{
/*
* grabs feed run
*/
public function get($params);
/*
* builds model based on feed
*/
public function buildModel($object);
}
class Bamboo_Models_Highlights_Home extends Bamboo_Models_Base implements Bamboo_Models_Interface
{
public $elements = array();
/*
* Hands std object to be built
*/
public function get($params) {
//CLIENT code here
$response = file_get_contents('http://open.live.bbc.co.uk/ibl/v1/home/highlights.json?api_key=hquruzwgmtc5hx9hmwvbvz2v&availability=available&lang=en&rights=web');
$json = json_decode($response);
$elements = $json->home_highlights->elements;
$this->buildModel($elements);
}
/*
* Create Bamboo model of each item, uses type
*/
public function buildModel($objects) {
foreach($objects as $object) {
$className = 'Bamboo_Models_'.ucfirst($object->type);
$class = new $className();
$class->buildModel($object);
$this->elements[] = $class;
}
}
}
class Bamboo_Models_Group_large
{
public function get() {}
public function buildModel() {}
}
class Bamboo_Models_Episode extends Bamboo_Models_Base implements Bamboo_Models_Interface
{
public $synopses = array();
public $master_brand = array();
public function get($params) {
//CLIENT code here
$response = file_get_contents('http://open.live.bbc.co.uk/ibl/v1/episodes/b03x19tb,b03wg799.json?api_key=hquruzwgmtc5hx9hmwvbvz2v&availability=all&lang=en&rights=web');
$json = json_decode($response);
$elements = $json->episodes;
return $this->buildElements($elements);
}
public function buildModel($object) {
foreach (get_object_vars($object) as $key => $value) {
$this->$key = $value;
}
}
}
class Bamboo_Models_Channel extends Bamboo_Models_Base implements Bamboo_Models_Interface
{
public $id;
public $title;
public function get($params) {
//CLIENT code here
$response = file_get_contents('http://open.int.bbc.co.uk/ibl/v1/stubs/channels.json?api_key=349cacfd');
$json = json_decode($response);
$elements = $json->channels;
return $this->buildElements($elements);
}
public function buildModel($object) {
foreach (get_object_vars($object) as $key => $value) {
$this->$key = $value;
}
}
}
/***** TVIPLAYER MODELS ******/
interface BBC_Tviplayer_Models_Interface
{
/*
* fetch, decorates and returns array for controller
*/
public function fetch($params);
/*
* decorates Tviplayer models with Bamboo models properties
*/
public function decorate($object);
}
class BBC_Tviplayer_Models_Highlights_Home extends Bamboo_Models_Highlights_Home implements BBC_Tviplayer_Models_Interface
{
public function fetch($params) {
$this->get($params);
$homeHighlightsTviplayer = array();
foreach($this->elements as $episode) {
$homeHighlightsTviplayer[] = $this->decorate($episode);
}
return $homeHighlightsTviplayer;
}
/*
* Produces a class of Tviplayer_Models_ decorates and returns
*/
public function decorate($bambooEp) {
$className = str_replace('Bamboo_Models_', 'BBC_Tviplayer_Models_', get_class($bambooEp));
$class = new $className();
$class->decorate($bambooEp);
return $class;
}
}
class BBC_Tviplayer_Models_Episode extends Bamboo_Models_Episode implements BBC_Tviplayer_Models_Interface
{
public function fetch($params) {
$episodes = $this->get($params);
$episodesArray = array();
foreach ($episodes as $bambooEp) {
$iplayerEp = new self();
$iplayerEp->decorate($bambooEp);
$episodesArray[] = $iplayerEp;
}
return $episodesArray;
}
public function decorate($bambooEp) {
$this->buildModel($bambooEp);
}
public function getMasterbrand() {
return $this->master_brand;
}
}
class BBC_Tviplayer_Models_Channel extends Bamboo_Models_Channel implements BBC_Tviplayer_Models_Interface
{
public function fetch($params) {
$channels = $this->get($params);
$ChannelsArray = array();
foreach ($channels as $bambooCh) {
$iplayerCh = new self();
$iplayerCh->decorate($bambooCh);
$ChannelsArray[] = $iplayerCh;
}
return $ChannelsArray;
}
public function decorate($object) {
$this->buildModel($object);
}
public function getTitle() {
return $this->title;
}
}
class BBC_Tviplayer_Models_Group_large extends Bamboo_Models_Channel implements BBC_Tviplayer_Models_Interface
{
public function fetch($params) {}
public function decorate($object) {}
}
/***** RUN ******/
$cont = new Tviplayer_HomepageController();
$cont->homeHighlightsAction();
$cont->episodesAction();
$cont->channelAction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment