public function testSetDataHandlerMappings() { $request = m::mock('Cartalyst\\DataGrid\\RequestProviders\\ProviderInterface'); $mappings = ['Cartalyst\\DataGrid\\DataHandlers\\CollectionHandler', function ($data) { return $data instanceof Collection or is_array($data); }]; $environment = new Environment($request); $environment->setDataHandlerMappings($mappings); $this->assertSame($mappings, $environment->getDataHandlerMappings()); }
/** * Creates a data handler instance from the given data type by * matching it to a mapping that's registered with the * environment instance. * * @return \Cartalyst\Datagrid\DataHandlers\HandlerInterface * @throws \RuntimeException */ public function createDataHandler() { foreach ($this->env->getDataHandlerMappings() as $handler => $class) { if ($class($this->data) === true) { // By calling the setter method we can be sure the // resolved class implements the correct interface. $instance = new $handler($this); $this->setDataHandler($instance); return $instance; } } $descriptor = gettype($this->data); if (is_object($this->data)) { $descriptor = get_class($this->data); } throw new RuntimeException("Could not determine an appropriate data source for data of type [{$descriptor}]."); }
/** * Returns the data handler mappings. * * @return array * @static */ public static function getDataHandlerMappings() { return \Cartalyst\DataGrid\Environment::getDataHandlerMappings(); }