Created
April 25, 2016 18:58
-
-
Save manchuck/90faa9fffb543000c3d66593f969b1ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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