public function test__constructor() { $genericResultSet = new GenericResultSet(); $resultSet = new BufferedResultSet($genericResultSet); $this->assertSame($genericResultSet, $resultSet->getResultSet()); $this->assertInstanceOf('\\Matryoshka\\Model\\ResultSet\\BufferedResultSetInterface', $resultSet); $this->setExpectedException('\\Matryoshka\\Model\\Exception\\InvalidArgumentException'); new BufferedResultSet(new BufferedResultSet($genericResultSet)); }
/** * Create service with name * @param ServiceLocatorInterface $serviceLocator * @param $name * @param $requestedName * @return mixed * @throws \Matryoshka\Model\Exception\UnexpectedValueException */ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { if ($serviceLocator instanceof AbstractPluginManager && $serviceLocator->getServiceLocator()) { $serviceLocator = $serviceLocator->getServiceLocator(); } $config = $this->getConfig($serviceLocator)[$requestedName]; $dataGataway = $serviceLocator->get($config['datagateway']); /* @var $resultSetPrototype \Matryoshka\Model\ResultSet\ResultSetInterface */ $resultSetPrototype = $serviceLocator->get($config['resultset']); if (isset($config['buffered_resultset']) && $config['buffered_resultset']) { /* @var $resultSetPrototype \Matryoshka\Model\ResultSet\AbstractResultSet */ $resultSetPrototype = new BufferedResultSet($resultSetPrototype); } //Create a model instance $class = $this->modelClass; if (!empty($config['type']) && is_string($config['type'])) { if (!is_subclass_of($config['type'], $class)) { throw new Exception\UnexpectedValueException(sprintf('"type" in model configuration must be a subclass of "%s": "%s" given', $class, $config['type'])); } $class = $config['type']; } /* @var $model \Matryoshka\Model\Model */ $model = new $class($dataGataway, $resultSetPrototype); //Setup Hydrator $hydrator = null; if (!empty($config['hydrator']) && is_string($config['hydrator'])) { $hydrator = $this->getHydratorByName($serviceLocator, $config['hydrator']); $model->setHydrator($hydrator); } if ($hydrator && $resultSetPrototype instanceof HydratorAwareInterface) { $resultSetPrototype->setHydrator($hydrator); } //Setup InputFilter if (!empty($config['input_filter']) && is_string($config['input_filter'])) { $model->setInputFilter($this->getInputFilterByName($serviceLocator, $config['input_filter'])); } //Setup Paginator if (!empty($config['paginator_criteria']) && is_string($config['paginator_criteria'])) { $model->setPaginatorCriteria($this->getPaginatorCriteriaByName($serviceLocator, $config['paginator_criteria'])); } //Setup Object Prototype if (!empty($config['object']) && is_string($config['object'])) { $resultSetPrototype->setObjectPrototype($this->getObjectByName($serviceLocator, $config['object'])); } //Setup Prototype strategy if (!empty($config['prototype_strategy']) && is_string($config['prototype_strategy'])) { if ($resultSetPrototype instanceof PrototypeStrategyAwareInterface) { $resultSetPrototype->setPrototypeStrategy($this->getPrototypeStrategyByName($serviceLocator, $config['prototype_strategy'])); } } //Setup listeners if (!empty($config['listeners']) && is_array($config['listeners'])) { if ($model instanceof ObservableModel) { /** @var $model ObservableModel */ $this->injectListeners($serviceLocator, $config['listeners'], $model); } else { throw new Exception\ServiceNotCreatedException(sprintf('Instance of model must be a subclass of "%s" in order to attach listeners', ObservableModel::class)); } } return $model; }