Skip to content

Instantly share code, notes, and snippets.

@pasamio
Created January 24, 2014 06:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pasamio/8593224 to your computer and use it in GitHub Desktop.
Save pasamio/8593224 to your computer and use it in GitHub Desktop.
<?php
/**
* @package Gris-Gris.Skeleton
* @subpackage Builder
*
* @copyright Copyright (C) 2014 Respective authors. All rights reserved.
* @license Licensed under the MIT License; see LICENSE.md
*/
namespace Grisgris\Builder;
use Closure;
use Grisgris\Provider\Provider;
/**
* JTable Object Builder.
*
* Use JTable to build objects or lists of objects.
*
* @package Gris-Gris.Skeleton
* @subpackage Builder
* @since 14.1
*/
class Table implements Builder
{
/**
* @var Provider Provider to supply to built objects.
* @since 14.1
*/
protected $provider;
/**
* @var array Set of mappings of type name to JTable metadata.
* @since 14.1
*/
protected $mappings = array();
/**
* Build many JTables by doing a select from the table.
*
* @param string $type The registered type.
* @param array $keys The ID's to load.
*
* @return array A set of JTable's for that type.
*
* @since 14.1
*/
public function buildMany($type, array $keys)
{
$mapping = $this->getMapping($type);
$rows = array();
$db = $this->provider->get('database');
$query = $db->getQuery(1);
$query->select('*')->from($mapping->name)->where($mapping->key . ' IN (' . implode(',', array_map('intval', $keys)) . ')');
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result)
{
$row = \JTable::getInstance($mapping->type, $mapping->prefix);
$row->setProperties($result);
$rows[$result->id] = $row;
}
return $rows;
}
/**
* Build a single type of JTable and load it with the given key.
*
* @param string $type The registered type of object.
* @param string $key The key of the object.
*
* @return JTable Instance of the given table.
*
* @since 14.1
*/
public function buildOne($type, $key)
{
$mapping = $this->getMapping($type);
$item = \JTable::getInstance($mapping->type, $mapping->prefix);
$item->load($key);
return $item;
}
/**
* Function to check if this builder can build a given type.
*
* @param string $type The type name to check.
*
* @return boolean If the given type can be built.
*
* @since 14.1
*/
public function canBuild($type)
{
return isset($this->mappings[$type]);
}
/**
* Retrieve a pre-configured mapping.
*
* @param string $type The type to retrieve.
*
* @return object The mapping details.
* @throws InvalidArgumentException Triggered for invalid types.
*
* @since 14.1
*/
public function getMapping($type)
{
if (!isset($this->mappings[$type]))
{
throw new InvalidArgumentException('Type not registered for construction: ' . $type);
}
return $this->mappings[$type];
}
/**
* Remove a pre-configured mapping.
*
* @param string $type The type mapping to remove.
*
* @since 14.1
*/
public function removeMapping($type)
{
if (isset($this->mappings[$type]))
{
unset($this->mappings[$type]);
}
}
/**
* Set a mapping for a given type name to JTable metadata.
*
* @param string $type The type to register.
* @param string $tabletype The type for JTable.
* @param string $tableprefix The prefix for JTable.
* @param string $tablename The database table name (including #__) for list building.
* @param string $tablekey The key field in the table for list building.
*
* @since 14.1
*/
public function setMapping($type, $tabletype, $tableprefix, $tablename, $tablekey)
{
$this->mappings[$type] = (object) array('type' => $tabletype, 'prefix' => $tableprefix, 'name' => $tablename, 'key' => $tablekey);
}
/**
* Set provider object.
*
* @param Provider $provider The provider to supply to built objects.
*
* @since 14.1
*/
public function setProvider(Provider $provider)
{
$this->provider = $provider;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment