Skip to content

Instantly share code, notes, and snippets.

@manchuck
Created April 25, 2016 18:58
Show Gist options
  • Save manchuck/90faa9fffb543000c3d66593f969b1ee to your computer and use it in GitHub Desktop.
Save manchuck/90faa9fffb543000c3d66593f969b1ee to your computer and use it in GitHub Desktop.
use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\TableGateway;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* Class AbstractTableFactory
*
* Creates a default table gateway based off the requested name
*/
class AbstractTableFactory implements AbstractFactoryInterface
{
/**
* @param ServiceLocatorInterface $services
* @param $name
* @param $requestedName
* @return mixed
*/
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
return preg_match("/Table$/", $requestedName);
}
/**
* @param ServiceLocatorInterface $services
* @param $name
* @param $requestedName
* @return TableGateway
*/
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
/** @var Adapter $adapter */
$adapter = $services->get(Adapter::class);
$tableName = str_replace('Table', '', $requestedName);
$tableName = strtolower($tableName);
return new TableGateway($tableName, $adapter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment