public function testSaveSettings()
 {
     $namespace = 'some_namespace';
     $storedSettingsArray = ['color' => 'red', 'interval' => 10, 'last_update' => '2014-06-01'];
     $namespaceParameters = [];
     foreach ($storedSettingsArray as $name => $value) {
         $parameter = new Parameter();
         $parameter->setName($name);
         $parameter->setValue($value);
         $parameter->setNamespace($namespace);
         $namespaceParameters[] = $parameter;
     }
     $hydrator = new Hydrator\ArraySerializable();
     $newSettingsArray = ['color' => 'black', 'interval' => 12, 'height' => 12, 'width' => 6];
     $newSettings = new ArrayObject();
     $hydrator->hydrate($newSettingsArray, $newSettings);
     $namespaceHydratorProvider = $this->getMock('HtSettingsModule\\Service\\NamespaceHydratorProviderInerface');
     $namespaceHydratorProvider->expects($this->once())->method('getHydrator')->with($namespace)->will($this->returnValue($hydrator));
     $settingsMapper = $this->getMock('HtSettingsModule\\Mapper\\SettingsMapperInterface');
     $settingsMapper->expects($this->at(0))->method('findByNamespace')->with($namespace)->will($this->returnValue($namespaceParameters));
     //var_dump($settingsMapper->findByNamespace($namespace));exit();
     $updateParameter1 = Parameter::create($namespace, 'color', 'black');
     $settingsMapper->expects($this->at(1))->method('updateParameter')->with($updateParameter1);
     $updateParameter2 = Parameter::create($namespace, 'interval', 12);
     $settingsMapper->expects($this->at(2))->method('updateParameter')->with($updateParameter2);
     $insertParameter1 = Parameter::create($namespace, 'height', 12);
     $settingsMapper->expects($this->at(3))->method('insertParameter')->with($insertParameter1);
     $insertParameter2 = Parameter::create($namespace, 'width', 6);
     $settingsMapper->expects($this->at(4))->method('insertParameter')->with($insertParameter2);
     $options = new ModuleOptions();
     $settingsService = new SettingsService($options, $settingsMapper, $namespaceHydratorProvider);
     $settingsService->save($newSettings, $namespace);
 }
Exemple #2
0
 public function __construct($table, Adapter $db, $strategies = array())
 {
     $hydrator = new ArraySerializable();
     foreach ($strategies as $field => $strategy) {
         $hydrator->addStrategy($field, $strategy);
     }
     $resultSet = new HydratingResultSet();
     $resultSet->setHydrator($hydrator);
     parent::__construct($table, $db, null, $resultSet);
 }
 protected function buildHydrator()
 {
     // Inicialização
     $hydrator = new Hydrator\ArraySerializable();
     $datetime = new Datetime();
     // Internacionalização
     $serviceLocator = new ServiceManager();
     $datetime->setServiceLocator($serviceLocator);
     // Adicionar Estratégia de Filtro
     $hydrator->addStrategy('datetime', $datetime);
     // Apresentação
     return $hydrator;
 }
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $config = $serviceLocator->get('CrudFactory\\Service\\Factory\\Config');
     $tableConfig = $config['table_configuration'];
     $properties = array();
     foreach ($tableConfig as $key => $value) {
         $properties[$key] = '';
     }
     $entity = clone $serviceLocator->get('CrudFactory\\Entity\\ArrayEntity');
     $hydrate = new ArraySerializable();
     $hydrate->hydrate($properties, $entity);
     return $entity;
 }
 public function hydrate(array $data, $object)
 {
     $object = parent::hydrate($data, $object);
     if (is_callable([$object, 'setAsClean'])) {
         $object->setAsClean();
     }
     return $object;
 }
 /**
  * cRud - Read all rows
  * @return ViewModel
  */
 public function readAction()
 {
     $sort = $this->params()->fromQuery('sort') ?: 'id';
     $dir = $this->params()->fromQuery('dir') ?: 'asc';
     $paginator = $this->getService()->readAll($sort, $dir);
     if (!$paginator instanceof Paginator && !is_array($paginator)) {
         throw new \Exception('ReadAll must return an instance of Zend\\Paginator\\Paginator or an array');
     }
     if (is_array($paginator)) {
         $paginatorArray = array();
         foreach ($paginator as $pag) {
             $entity = clone $this->getServiceLocator()->get('CrudFactory\\Entity\\ArrayEntity');
             $hydrate = new ArraySerializable();
             $hydrate->hydrate($pag, $entity);
             $paginatorArray[] = $entity;
         }
         $paginator = new Paginator(new ArrayAdapter($paginatorArray));
     }
     $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
     $paginator->setItemCountPerPage(10);
     $viewModel = new ViewModel(array('module' => strtolower($this->params()->fromRoute('__CONTROLLER__')), 'results' => $paginator, 'title' => $this->getService()->getConfig('title'), 'headers' => $this->getService()->getHeaders(), 'sort' => $sort ?: 'id', 'direction' => $dir ?: 'asc'));
     $viewModel->setTemplate('crud-factory/crud-factory/index');
     return $viewModel;
 }
 /**
  * Get a hydrator to bind CustomField objects to the database
  *
  * Unlike other tables, the hydrator cannot be provided by the CustomFields
  * table class due to tricky dependencies. Use this method to get a suitable
  * hydrator.
  *
  * @return \Zend\Stdlib\Hydrator\ArraySerializable
  */
 public function getHydrator()
 {
     if (!$this->_hydrator) {
         $columns = $this->getColumnMap();
         $this->_hydrator = new \Zend\Stdlib\Hydrator\ArraySerializable();
         $this->_hydrator->setNamingStrategy(new \Database\Hydrator\NamingStrategy\MapNamingStrategy(array_flip($columns)));
         $dateStrategy = new \Zend\Stdlib\Hydrator\Strategy\DateTimeFormatterStrategy('Y-m-d');
         foreach ($this->getFields() as $name => $type) {
             if ($type == 'date') {
                 $this->_hydrator->addStrategy($name, $dateStrategy);
                 $this->_hydrator->addStrategy($columns[$name], $dateStrategy);
             }
         }
     }
     return $this->_hydrator;
 }
 /**
  * Hydrate $object with the provided $data.
  *
  * @param  array               $data
  * @param  UserEntityInterface $object
  * @return UserEntityInterface
  * @throws Exception\InvalidArgumentException
  */
 public function hydrate(array $data, $object)
 {
     $this->guardUserObject($object);
     $data = $this->mapField('user_id', 'id', $data);
     return parent::hydrate($data, $object);
 }
 /**
  * Execute geocoding
  * 
  * @param  Request $request
  * @return Response
  */
 public function geocode(Request $request)
 {
     if (null === $request) {
         throw new Exception\InvalidArgumentException('request');
     }
     $uri = new Uri();
     $uri->setHost(self::GOOGLE_MAPS_APIS_URL);
     $uri->setPath(self::GOOGLE_GEOCODING_API_PATH);
     $urlParameters = $request->getUrlParameters();
     if (null === $urlParameters) {
         throw new Exception\RuntimeException('Invalid URL parameters');
     }
     $uri->setQuery($urlParameters);
     $client = $this->getHttpClient();
     $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl');
     $client->resetParameters();
     $uri->setScheme("https");
     $client->setUri($uri->toString());
     $stream = $client->send();
     $body = Json::decode($stream->getBody(), Json::TYPE_ARRAY);
     $hydrator = new ArraySerializable();
     $response = new Response();
     $response->setRawBody($body);
     if (!isset($body['status'])) {
         throw new Exception\RuntimeException('Invalid status');
     }
     $response->setStatus($body['status']);
     if (!isset($body['results'])) {
         throw new Exception\RuntimeException('Invalid results');
     }
     $resultSet = new ResultSet();
     foreach ($body['results'] as $data) {
         $result = new Result();
         $hydrator->hydrate($data, $result);
         $resultSet->addElement($result);
     }
     $response->setResults($resultSet);
     return $response;
 }
 /**
  * {@inheritDoc}
  * @throws PluginException
  */
 public function extract($object)
 {
     return array_merge(parent::extract($object), $this->extractFromPlugins($object));
 }