예제 #1
0
 /**
  * Retrieves a list of ids to process
  *
  * @return array
  */
 protected function _processIds()
 {
     $ids = $this->_controller()->request->data('id');
     $all = false;
     if (is_array($ids)) {
         $all = Hash::get((array) $ids, '_all', false);
         unset($ids['_all']);
     }
     if (!is_array($ids) || !Hash::numeric(array_keys($ids))) {
         throw new BadRequestException('Bad request data');
     }
     if ($all) {
         foreach ($ids as $key => $value) {
             $ids[$key] = 1;
         }
     }
     $ids = array_filter($ids);
     return array_keys($ids);
 }
예제 #2
0
 /**
  * testNumericArrayCheck method
  *
  * @return void
  */
 public function testNumeric()
 {
     $data = ['one'];
     $this->assertTrue(Hash::numeric(array_keys($data)));
     $data = [1 => 'one'];
     $this->assertFalse(Hash::numeric($data));
     $data = ['one'];
     $this->assertFalse(Hash::numeric($data));
     $data = ['one' => 'two'];
     $this->assertFalse(Hash::numeric($data));
     $data = ['one' => 1];
     $this->assertTrue(Hash::numeric($data));
     $data = [0];
     $this->assertTrue(Hash::numeric($data));
     $data = ['one', 'two', 'three', 'four', 'five'];
     $this->assertTrue(Hash::numeric(array_keys($data)));
     $data = [1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five'];
     $this->assertTrue(Hash::numeric(array_keys($data)));
     $data = ['1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five'];
     $this->assertTrue(Hash::numeric(array_keys($data)));
     $data = ['one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five'];
     $this->assertFalse(Hash::numeric(array_keys($data)));
     $data = [2.4, 1, 0, -1, -2];
     $this->assertTrue(Hash::numeric($data));
 }
예제 #3
0
 /**
  * Serialize view vars.
  *
  * ### Special parameters
  * `_xmlOptions` You can set an array of custom options for Xml::fromArray() this way, e.g.
  *   'format' as 'attributes' instead of 'tags'.
  *
  * @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized
  * @return string The serialized data
  */
 protected function _serialize($serialize)
 {
     $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
     if ($serialize === true) {
         $serialize = array_diff(array_keys($this->viewVars), $this->_specialVars);
         if (empty($serialize)) {
             $serialize = null;
         } elseif (count($serialize) === 1) {
             $serialize = current($serialize);
         }
     }
     if (is_array($serialize)) {
         $data = [$rootNode => []];
         foreach ($serialize as $alias => $key) {
             if (is_numeric($alias)) {
                 $alias = $key;
             }
             if (array_key_exists($key, $this->viewVars)) {
                 $data[$rootNode][$alias] = $this->viewVars[$key];
             }
         }
     } else {
         $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
         if (is_array($data) && Hash::numeric(array_keys($data))) {
             $data = [$rootNode => [$serialize => $data]];
         }
     }
     $options = [];
     if (isset($this->viewVars['_xmlOptions'])) {
         $options = $this->viewVars['_xmlOptions'];
     }
     if (Configure::read('debug')) {
         $options['pretty'] = true;
     }
     if (isset($options['return']) && strtolower($options['return']) === 'domdocument') {
         return Xml::fromArray($data, $options)->saveXML();
     }
     return Xml::fromArray($data, $options)->asXML();
 }
예제 #4
0
 /**
  * Serialize view vars.
  *
  * @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized
  * @return string The serialized data
  */
 protected function _serialize($serialize)
 {
     $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
     if (is_array($serialize)) {
         $data = [$rootNode => []];
         foreach ($serialize as $alias => $key) {
             if (is_numeric($alias)) {
                 $alias = $key;
             }
             $data[$rootNode][$alias] = $this->viewVars[$key];
         }
     } else {
         $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
         if (is_array($data) && Hash::numeric(array_keys($data))) {
             $data = [$rootNode => [$serialize => $data]];
         }
     }
     $options = [];
     if (Configure::read('debug')) {
         $options['pretty'] = true;
     }
     return Xml::fromArray($data, $options)->asXML();
 }
예제 #5
0
 /**
  * testNumericArrayCheck method
  *
  * @return void
  */
 public function testNumeric()
 {
     $data = array('one');
     $this->assertTrue(Hash::numeric(array_keys($data)));
     $data = array(1 => 'one');
     $this->assertFalse(Hash::numeric($data));
     $data = array('one');
     $this->assertFalse(Hash::numeric($data));
     $data = array('one' => 'two');
     $this->assertFalse(Hash::numeric($data));
     $data = array('one' => 1);
     $this->assertTrue(Hash::numeric($data));
     $data = array(0);
     $this->assertTrue(Hash::numeric($data));
     $data = array('one', 'two', 'three', 'four', 'five');
     $this->assertTrue(Hash::numeric(array_keys($data)));
     $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
     $this->assertTrue(Hash::numeric(array_keys($data)));
     $data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
     $this->assertTrue(Hash::numeric(array_keys($data)));
     $data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
     $this->assertFalse(Hash::numeric(array_keys($data)));
     $data = array(2.4, 1, 0, -1, -2);
     $this->assertTrue(Hash::numeric($data));
 }
예제 #6
0
 /**
  * Serialize view vars.
  *
  * @param string|array $serialize The viewVars that need to be serialized.
  * @return string The serialized data
  * @throws RuntimeException When the prefix is not specified
  */
 protected function _serialize($serialize)
 {
     $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'channel';
     if (is_array($serialize)) {
         $data = [$rootNode => []];
         foreach ($serialize as $alias => $key) {
             if (is_numeric($alias)) {
                 $alias = $key;
             }
             $data[$rootNode][$alias] = $this->viewVars[$key];
         }
     } else {
         $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
         if (is_array($data) && Hash::numeric(array_keys($data))) {
             $data = [$rootNode => [$serialize => $data]];
         }
     }
     $defaults = ['document' => [], 'channel' => [], 'items' => []];
     $data += $defaults;
     if (!empty($data['document']['namespace'])) {
         foreach ($data['document']['namespace'] as $prefix => $url) {
             $this->setNamespace($prefix, $url);
         }
     }
     $channel = $this->channel($data['channel']);
     if (!empty($channel['image']) && empty($channel['image']['title'])) {
         $channel['image']['title'] = $channel['title'];
     }
     foreach ($data['items'] as $item) {
         $channel['item'][] = $this->_prepareOutput($item);
     }
     $array = ['rss' => ['@version' => $this->version, 'channel' => $channel]];
     $namespaces = [];
     foreach ($this->_usedNamespaces as $usedNamespacePrefix) {
         if (!isset($this->_namespaces[$usedNamespacePrefix])) {
             throw new \RuntimeException(sprintf('The prefix %s is not specified.', $usedNamespacePrefix));
         }
         $namespaces['xmlns:' . $usedNamespacePrefix] = $this->_namespaces[$usedNamespacePrefix];
     }
     $array['rss'] += $namespaces;
     $options = [];
     if (Configure::read('debug')) {
         $options['pretty'] = true;
     }
     $output = Xml::fromArray($array, $options)->asXML();
     $output = $this->_replaceCdata($output);
     return $output;
 }
예제 #7
0
 protected function _resolveFilters($type, $filters)
 {
     $filters = (array) $filters;
     $filterManager = $this->_getFilterManager($type);
     $keys = $filters;
     if (!Hash::numeric(array_keys($keys))) {
         $keys = array_keys($keys);
     }
     array_walk($filters, function (&$filter) use($filterManager) {
         if ('?' == $filter[0]) {
             $filter = substr($filter, 1);
         }
         if (!$filterManager->has($filter)) {
             throw new InvalidArgumentException(sprintf('Filter `%s` not configured.', $filter));
         }
         $filter = $filterManager->get($filter);
     });
     return array_combine($keys, $filters);
 }
예제 #8
0
 /**
  * @param string $property
  * @param string|array $value
  * @return array
  */
 protected function _prepareFilter($property, $value)
 {
     $filter = [];
     $comparisons = ['eq', 'seq', 'neq', 'gt', 'ge', 'gte', 'lt', 'le', 'lte', 'ct', 'sw', 'nct', 'ew', 'in', 'nin'];
     if (is_array($value) && !Hash::numeric(array_keys($value))) {
         foreach ($value as $comparison => $_value) {
             if (in_array($comparison, ['in', 'nin'])) {
                 // IN or NOT IN
                 $filter = ['property' => $property, 'value' => $this->_checkPropertyArrayValue($property, is_array($_value) ? $_value : [$_value]), 'comparison' => $comparison];
             } elseif (in_array($comparison, $comparisons)) {
                 // OTHER CLAUSE
                 if ($this->getManager()->checkProperty($property, $_value, true)) {
                     $filter = ['property' => $property, 'value' => $_value, 'comparison' => $comparison];
                 }
             } else {
                 if ($this->_client->isDebugMode()) {
                     throw new Exception("La clause \"{$comparison}\" n'existe pas");
                 }
             }
         }
     } elseif (is_array($value)) {
         // IN
         $filter = ['property' => $property, 'value' => $this->_checkPropertyArrayValue($property, $value)];
     } else {
         // EQUAL
         if ($this->getManager()->checkProperty($property, $value, true)) {
             $filter = ['property' => $property, 'value' => $value];
         }
     }
     return $filter;
 }