/** * Test dimensions. * * @return void */ public function testDimensions() { $result = Hash::dimensions(array()); $this->assertEquals($result, 0); $data = array('one', '2', 'three'); $result = Hash::dimensions($data); $this->assertEquals($result, 1); $data = array('1' => '1.1', '2', '3'); $result = Hash::dimensions($data); $this->assertEquals($result, 1); $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1')); $result = Hash::dimensions($data); $this->assertEquals($result, 2); $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); $result = Hash::dimensions($data); $this->assertEquals($result, 1); $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Hash::dimensions($data); $this->assertEquals($result, 2); }
/** * Process $_FILES * * @author Anthony Putignano <*****@*****.**> * @since 1.0 * @return void */ protected function __processFiles() { if (!empty($_FILES)) { $dimensions = Hash::dimensions($_FILES); if ($dimensions === 2) { $this->Controller->request->data = Hash::merge($this->Controller->request->data, $_FILES); } else { foreach ($_FILES as $key => $data) { $parsed = array(); foreach (array('name', 'type', 'tmp_name', 'error') as $file_key) { $flattened = Hash::flatten($_FILES[$key][$file_key]); foreach ($flattened as $flat_key => $flat_value) { $reflattened = $key . '.' . $flat_key . '.' . $file_key; $parsed[$reflattened] = $flat_value; } } $parsed = Hash::expand($parsed); $this->Controller->request->data = Hash::merge($this->Controller->request->data, $parsed); } } } }
/** * Helper method for saveAll() and friends, to add foreign key to fieldlist * * @param string $key fieldname to be added to list * @param array $options * @return array $options */ protected function _addToWhiteList($key, $options) { if (empty($options['fieldList']) && $this->whitelist && !in_array($key, $this->whitelist)) { $options['fieldList'][$this->alias] = $this->whitelist; $options['fieldList'][$this->alias][] = $key; return $options; } if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) { $options['fieldList'][$this->alias][] = $key; return $options; } if (!empty($options['fieldList']) && is_array($options['fieldList']) && Hash::dimensions($options['fieldList']) < 2) { $options['fieldList'][] = $key; } return $options; }
/** * Process a validation rule for a field and looks for a message to be added * to the translation map * * @param string $field the name of the field that is being processed * @param array $rules the set of validation rules for the field * @param string $file the file name where this validation rule was found * @param string $domain default domain to bind the validations to * @param string $category the translation category * @return void */ protected function _processValidationRules($field, $rules, $file, $domain, $category = 'LC_MESSAGES') { if (!is_array($rules)) { return; } $dims = Hash::dimensions($rules); if ($dims === 1 || $dims === 2 && isset($rules['message'])) { $rules = array($rules); } foreach ($rules as $rule => $validateProp) { $msgid = null; if (isset($validateProp['message'])) { if (is_array($validateProp['message'])) { $msgid = $validateProp['message'][0]; } else { $msgid = $validateProp['message']; } } elseif (is_string($rule)) { $msgid = $rule; } if ($msgid) { $msgid = $this->_formatString(sprintf("'%s'", $msgid)); $details = array('file' => $file, 'line' => 'validation for field ' . $field); $this->_addTranslation($category, $domain, $msgid, $details); } } }
/** * Processes the passed fieldList and returns the list of fields to be validated * * @param array $fieldList list of fields to be used for validation * @return array List of validation rules to be applied */ protected function _validationList($fieldList = array()) { if (empty($fieldList) || Hash::dimensions($fieldList) > 1) { return $this->_fields; } $validateList = array(); $this->validationErrors = array(); foreach ((array) $fieldList as $f) { if (!empty($this->_fields[$f])) { $validateList[$f] = $this->_fields[$f]; } } return $validateList; }
/** * Processes the Model's whitelist or passed fieldList and returns the list of fields * to be validated * * @param array $fieldList list of fields to be used for validation * @return array List of validation rules to be applied */ protected function _validationList($fieldList = array()) { $model = $this->getModel(); $whitelist = $model->whitelist; if (!empty($fieldList)) { if (!empty($fieldList[$model->alias]) && is_array($fieldList[$model->alias])) { $whitelist = $fieldList[$model->alias]; } else { $whitelist = $fieldList; } } unset($fieldList); if (empty($whitelist) || Hash::dimensions($whitelist) > 1) { return $this->_fields; } $validateList = array(); $this->validationErrors = array(); foreach ((array) $whitelist as $f) { if (!empty($this->_fields[$f])) { $validateList[$f] = $this->_fields[$f]; } } return $validateList; }
/** * AfterFind **/ public function afterFind(Model $model, $results = array(), $primary = false) { parent::afterFind($model, $results, $primary); if ($model->findQueryType == 'count') { return $results; } $curObj = $model->alias; foreach ($results as $key => $value) { $tmpkeys = array_keys($value); foreach ($tmpkeys as $curmodel) { $curObj = $curmodel != $model->alias ? $model->{$curmodel} : $model; $columnTypes = $curObj->getColumnTypes(); $fields = array_filter($columnTypes, array($this, 'valueIsDate')); /** * @todo Array walk might be a solution for deeper associations? **/ foreach ($fields as $field => $fieldvalue) { $curvalues = $results[$key][$curmodel]; $dimensions = Hash::dimensions($curvalues); if ($dimensions <= 1) { if (!array_key_exists($field, $results[$key][$curmodel])) { continue; } $results[$key][$curmodel][$field] = $this->formatDate($results[$key][$curmodel][$field]); continue; } } } } return $results; }