/** * Startup * * @param object $controller * @return void */ function initialize(&$controller) { $controller->isAmf = false; if (class_exists('amfdispatcher') && AmfDispatcher::active()) { $controller->isAmf = true; $controller->disableCache(); $controller->view = 'Amf.Amf'; $data = AmfDispatcher::data(); if (!empty($data)) { if (is_object($data)) { $controller->data = Set::reverse($data); } elseif (Set::countDim($data) == 1) { $controller->data = array_pop($data); } else { $controller->data = $data; } } } }
public function uniques($check) { $conditions = array(); $args = func_get_args(); foreach ($args as $field) { if (is_array($field)) { if (Set::countDim($field, true) === 1) { foreach ($field as $k => $v) { $conditions += array($this->escapeField($k) => $v); } } } else { if (!isset($this->data[$this->alias][$field])) { trigger_error($field . ' is not propery set in data on ' . $this->alias); return false; } $conditions += array($this->escapeField($field) => $this->data[$this->alias][$field]); } } return !$this->hasAny($conditions); }
/** * Returns an ORDER BY clause as a string. * * @param string $key Field reference, as a key (i.e. Post.title) * @param string $direction Direction (ASC or DESC) * @return string ORDER BY clause */ function order($keys, $direction = 'ASC') { if (is_string($keys) && strpos($keys, ',') && !preg_match('/\\(.+\\,.+\\)/', $keys)) { $keys = explode(',', $keys); array_map('trim', $keys); } if (is_array($keys)) { foreach ($keys as $key => $val) { if (is_numeric($key) && empty($val)) { unset($keys[$key]); } } } if (empty($keys) || is_array($keys) && count($keys) && isset($keys[0]) && empty($keys[0])) { return ''; } if (is_array($keys)) { if (Set::countDim($keys) > 1) { $new = array(); foreach ($keys as $val) { $val = $this->order($val); $new[] = $val; } $keys = $new; } foreach ($keys as $key => $value) { if (is_numeric($key)) { $value = ltrim(r('ORDER BY ', '', $this->order($value))); $key = $value; if (!preg_match('/\\x20ASC|\\x20DESC/i', $key)) { $value = ' ' . $direction; } else { $value = ''; } } else { $value = ' ' . $value; } if (!preg_match('/^.+\\(.*\\)/', $key) && !strpos($key, ',')) { $dir = ''; $hasDir = preg_match('/\\x20ASC|\\x20DESC/i', $key, $dir); if ($hasDir) { $dir = $dir[0]; $key = preg_replace('/\\x20ASC|\\x20DESC/i', '', $key); } else { $dir = ''; } $key = trim($this->name(trim($key)) . ' ' . trim($dir)); } $order[] = $this->order($key . $value); } return ' ORDER BY ' . trim(r('ORDER BY', '', join(',', $order))); } else { $keys = preg_replace('/ORDER\\x20BY/i', '', $keys); if (strpos($keys, '.')) { preg_match_all('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', $keys, $result, PREG_PATTERN_ORDER); $pregCount = count($result['0']); for ($i = 0; $i < $pregCount; $i++) { $keys = preg_replace('/' . $result['0'][$i] . '/', $this->name($result['0'][$i]), $keys); } if (preg_match('/\\x20ASC|\\x20DESC/i', $keys)) { return ' ORDER BY ' . $keys; } else { return ' ORDER BY ' . $keys . ' ' . $direction; } } elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) { $direction = $match['1']; $keys = preg_replace('/' . $match['1'] . '/', '', $keys); return ' ORDER BY ' . $keys . $direction; } else { $direction = ' ' . $direction; } return ' ORDER BY ' . $keys . $direction; } }
/** * Prepares REST data for cake interaction * * @param <type> $data * @return <type> */ protected function _modelizePost (&$data) { if (!is_array($data)) { return $data; } // Don't throw errors if data is already modelized // f.e. sending a serialized FormHelper form via ajax if (isset($data[$this->Controller->modelClass])) { $data = $data[$this->Controller->modelClass]; } // Protected against Saving multiple models in one post // while still allowing mass-updates in the form of: // $this->data[1][field] = value; if (Set::countDim($data) === 2) { if (!$this->numeric($data)) { return $this->error('2 dimensional can only begin with numeric index'); } } else if (Set::countDim($data) !== 1) { return $this->error('You may only send 1 dimensional posts'); } // Encapsulate in Controller Model $data = array( $this->Controller->modelClass => $data, ); return $data; }
/** * @param mixed $data * @return bool */ public function save($data = '') { $data = $this->beforeSave($data); if (Configure::read('themeSupport.skins') === true) { if ($this->id == GUMM_THEME_PREFIX . '_styles') { $skin = GummRegistry::get('Model', 'Skin')->getActiveSkin(); $data = array($skin => $data); if (!$this->mergeOnSave) { $existingStylesData = get_option($this->id); if ($existingStylesData && is_array($existingStylesData)) { $data = array_merge($existingStylesData, $data); } } } } if ($this->mergeOnSave) { $existingData = get_option($this->id); $originData = $data; $dimensionCount = Set::countDim($existingData, true); if ($dimensionCount === 1 && isset($data[0])) { } elseif ($existingData && is_array($existingData) && is_array($data)) { $data = Set::merge($existingData, $data); } } $success = update_option($this->id, $data); $this->afterSave($data); return $success; }
/** * Returns if a field is required to be filled based on validation properties from the validating object * * @return boolean true if field is required to be filled, false otherwise */ protected function _isRequiredField($validateProperties) { $required = false; if (is_array($validateProperties)) { $dims = Set::countDim($validateProperties); if ($dims == 1 || $dims == 2 && isset($validateProperties['rule'])) { $validateProperties = array($validateProperties); } foreach ($validateProperties as $rule => $validateProp) { if (isset($validateProp['allowEmpty']) && $validateProp['allowEmpty'] === true) { return false; } $rule = isset($validateProp['rule']) ? $validateProp['rule'] : false; $required = $rule || empty($validateProp); if ($required) { break; } } } return $required; }
/** * Counts the dimensions of an array. If $all is set to false (which is the default) it will * only consider the dimension of the first element in the array. * * @param array $array Array to count dimensions on * @param boolean $all Set to true to count the dimension considering all elements in array * @param integer $count Start the dimension count at this number * @return integer The number of dimensions in $array * @access public */ function countDim($array = null, $all = false, $count = 0) { if ($array === null) { $array = $this->get(); } elseif (is_object($array) && is_a($array, 'set')) { $array = $array->get(); } if ($all) { $depth = array($count); if (is_array($array) && reset($array) !== false) { foreach ($array as $value) { $depth[] = Set::countDim($value, true, $count + 1); } } $return = max($depth); } else { if (is_array(reset($array))) { $return = Set::countDim(reset($array)) + 1; } else { $return = 1; } } return $return; }
/** * Counts the dimensions of an array. If $all is set to false (which is the default) it will * only consider the dimension of the first element in the array. * * @param array $array Array to count dimensions on * @param boolean $all Set to true to count the dimension considering all elements in array * @param integer $count Start the dimension count at this number * @return integer The number of dimensions in $array * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::countDim */ public static function countDim($array = null, $all = false, $count = 0) { if ($all) { $depth = array($count); if (is_array($array) && reset($array) !== false) { foreach ($array as $value) { $depth[] = Set::countDim($value, true, $count + 1); } } $return = max($depth); } else { if (is_array(reset($array))) { $return = Set::countDim(reset($array)) + 1; } else { $return = 1; } } return $return; }
function order($order, $context) { if (is_string($order) && strpos($order, ',') && !preg_match('/\\(.+\\,.+\\)/', $order)) { $order = array_map('trim', explode(',', $order)); } $order = is_array($order) ? array_filter($order) : $order; if (empty($order)) { return ''; } if (is_array($keys)) { $keys = Set::countDim($keys) > 1 ? array_map(array(&$this, 'order'), $keys) : $keys; foreach ($keys as $key => $value) { if (is_numeric($key)) { $key = $value = ltrim(str_replace('ORDER BY ', '', $this->order($value))); $value = !preg_match('/\\x20ASC|\\x20DESC/i', $key) ? ' ' . $direction : ''; } else { $value = ' ' . $value; } if (!preg_match('/^.+\\(.*\\)/', $key) && !strpos($key, ',')) { if (preg_match('/\\x20ASC|\\x20DESC/i', $key, $dir)) { $dir = $dir[0]; $key = preg_replace('/\\x20ASC|\\x20DESC/i', '', $key); } else { $dir = ''; } $key = trim($key); if (!preg_match('/\\s/', $key)) { $key = $this->name($key); } $key .= ' ' . trim($dir); } $order[] = $this->order($key . $value); } return ' ORDER BY ' . trim(str_replace('ORDER BY', '', join(',', $order))); } $keys = preg_replace('/ORDER\\x20BY/i', '', $keys); if (strpos($keys, '.')) { preg_match_all('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', $keys, $result, PREG_PATTERN_ORDER); $pregCount = count($result[0]); for ($i = 0; $i < $pregCount; $i++) { if (!is_numeric($result[0][$i])) { $keys = preg_replace('/' . $result[0][$i] . '/', $this->name($result[0][$i]), $keys); } } $result = ' ORDER BY ' . $keys; return $result . (!preg_match('/\\x20ASC|\\x20DESC/i', $keys) ? ' ' . $direction : ''); } elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) { $direction = $match[1]; return ' ORDER BY ' . preg_replace('/' . $match[1] . '/', '', $keys) . $direction; } return ' ORDER BY ' . $keys . ' ' . $direction; }
/** * Automatically determine active collections based on the passed $item * * @param mixed $item */ public static function setCollectionsMagic($item = null) { $controller = Sl::getInstance()->controller; $collections = array(); if (is_string($item)) { $collections[] = $item; } else { if ($controller) { if (isset($item[$controller->modelClass]['id'])) { $collections["{$controller->modelClass}{$item[$controller->modelClass]['id']}"] = 899; } if (empty($item['nodes']) && !empty($item['path'][0][$controller->modelClass]['id'])) { $ids = Set::extract("{n}.{$controller->modelClass}.id", $data['path']); foreach ($ids as $i => $id) { $collections["{$controller->modelClass}{$id}"] = 800 + $i; } } } if (!empty($item['nodes'])) { foreach ($item['nodes'] as $i => $nodeId) { $collections["Node{$nodeId}"] = 600 + $i; } } if (!empty($item['tags'])) { foreach ($item['tags'] as $i => $tagId) { $collections["Tag{$tagId}"] = 400 + $i; } } if (empty($collections) && is_array($item) && Set::numeric($item) && Set::countDim($item, true) == 1) { $collections = $item; } } return self::setCollections($collections); }
/** * Generates XML content based on the type of variable or object passed * * @param mixed $content The content to be converted to XML * @return string XML */ function __composeContent($content) { if (is_string($content)) { return $content; } elseif (is_array($content)) { $out = ''; $keys = array_keys($content); $count = count($keys); for ($i = 0; $i < $count; $i++) { if (is_numeric($keys[$i])) { $out .= $this->__composeContent($content[$keys[$i]]); } elseif (is_array($content[$keys[$i]])) { $attr = $child = array(); if (Set::countDim($content[$keys[$i]]) >= 2) { } else { } //$out .= $this->elem($keys[$i] } } return $out; } elseif (is_object($content) && (is_a($content, 'XMLNode') || is_a($content, 'xmlnode'))) { return $content->toString(); } elseif (is_object($content) && method_exists($content, 'toString')) { return $content->toString(); } elseif (is_object($content) && method_exists($content, 'toString')) { return $content->toString(); } else { return $content; } }
function _startFixture($name, $data) { $model = $this->{$name}; if ($model->actsAs && in_array('Tree', $model->actsAs)) { $model->Behaviors->detach('Tree'); } foreach ($data as $key => $value) { if (preg_match("/^repeat-([0-9]+)\$/", $key, $matches)) { for ($i = 1; $i <= $matches[1]; $i++) { $data[] = $value; } unset($data[$key]); continue; } } $id = 0; $created = array(); foreach ($data as $ri => $r) { $id++; $records = array(); if (!array_key_exists('id', $r)) { $schema = $model->schema('id'); if ($schema && $schema['type'] == 'string' && $schema['length'] == 36) { $records['id'] = String::uuid(); } else { $records['id'] = $id; } } foreach ($r as $fi => $f) { $class = Inflector::classify($fi); if (isset($model->{$class})) { if (is_array($f)) { if (Set::countDim($f) > 1) { foreach ($f as $i => $v) { $f[$i][$model->hasMany[$class]['foreignKey']] = $records['id']; } } else { $f[$model->hasMany[$class]['foreignKey']] = $records['id']; $f = array($f); } foreach ($this->_startFixture($class, $f) as $i => $v) { unset($v['id']); $this->data[$class][] = $v; } } else { $records[$fi] = '.RANDOM'; } } else { $records[$fi] = $this->_formatColumn($fi, $f); } } if (isset($model->_schema['created']) && !array_key_exists('created', $records)) { $records['created'] = date('Y-m-d H:i:s'); } $created[$ri] = $records; } return $created; }
/** * This function does two things: 1) it scans the array $one for the primary key, * and if that's found, it sets the current id to the value of $one[id]. * For all other keys than 'id' the keys and values of $one are copied to the 'data' property of this object. * 2) Returns an array with all of $one's keys and values. * (Alternative indata: two strings, which are mangled to * a one-item, two-dimensional array using $one for a key and $two as its value.) * * @param mixed $one Array or string of data * @param string $two Value string for the alternative indata method * @return unknown */ function set($one, $two = null) { if (is_object($one)) { $one = Set::reverse($one); } if (is_array($one)) { if (Set::countDim($one) == 1) { $data = array($this->name => $one); } else { $data = $one; } } else { $data = array($this->name => array($one => $two)); } foreach ($data as $n => $v) { if (is_array($v)) { foreach ($v as $x => $y) { if (empty($this->whitelist) || (in_array($x, $this->whitelist) || $n !== $this->name)) { if (isset($this->validationErrors[$x])) { unset($this->validationErrors[$x]); } if ($n == $this->name || is_array($y)) { if ($x === $this->primaryKey) { $this->id = $y; } $this->data[$n][$x] = $y; } } } } } return $data; }
/** * This function does two things: 1) it scans the array $one for the primary key, * and if that's found, it sets the current id to the value of $one[id]. * For all other keys than 'id' the keys and values of $one are copied to the 'data' property of this object. * 2) Returns an array with all of $one's keys and values. * (Alternative indata: two strings, which are mangled to * a one-item, two-dimensional array using $one for a key and $two as its value.) * * @param mixed $one Array or string of data * @param string $two Value string for the alternative indata method * @return array Data with all of $one's keys and values * @access public */ function set($one, $two = null) { if (!$one) { return; } if (is_object($one)) { $one = Set::reverse($one); } if (is_array($one)) { if (Set::countDim($one) == 1) { $data = array($this->alias => $one); } else { $data = $one; } } else { $data = array($this->alias => array($one => $two)); } foreach ($data as $n => $v) { if (is_array($v)) { foreach ($v as $x => $y) { if (isset($this->validationErrors[$x])) { unset($this->validationErrors[$x]); } if ($n === $this->alias) { if ($x === $this->primaryKey) { $this->id = $y; } } if (is_array($y) || is_object($y)) { $y = $this->deconstruct($x, $y); } $this->data[$n][$x] = $y; } } } return $data; }
/** * 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 * @return void */ protected function _processValidationRules($field, $rules, $file, $domain) { if (is_array($rules)) { $dims = Set::countDim($rules); if ($dims == 1 || $dims == 2 && isset($rules['message'])) { $rules = array($rules); } foreach ($rules as $rule => $validateProp) { $message = null; if (isset($validateProp['message'])) { if (is_array($validateProp['message'])) { $message = $validateProp['message'][0]; } else { $message = $validateProp['message']; } } elseif (is_string($rule)) { $message = $rule; } if ($message) { $this->_strings[$domain][$message][$file][] = 'validation for field ' . $field; } } } }
/** * Returns an HTML FORM element. * * Options: * * - 'type' Form method defaults to POST * - 'action' The Action the form submits to. Can be a string or array, * - 'url' The url the form submits to. Can be a string or a url array, * - 'default' Allows for the creation of Ajax forms. * - 'onsubmit' Used in conjunction with 'default' to create ajax forms. * * @access public * @param string $model The model object which the form is being defined for * @param array $options An array of html attributes and options. * @return string An formatted opening FORM tag. */ function create($model = null, $options = array()) { $defaultModel = null; $view =& ClassRegistry::getObject('view'); if (is_array($model) && empty($options)) { $options = $model; $model = null; } if (empty($model) && $model !== false && !empty($this->params['models'])) { $model = $this->params['models'][0]; $defaultModel = $this->params['models'][0]; } elseif (empty($model) && empty($this->params['models'])) { $model = false; } elseif (is_string($model) && strpos($model, '.') !== false) { $path = explode('.', $model); $model = $path[count($path) - 1]; } if (ClassRegistry::isKeySet($model)) { $object =& ClassRegistry::getObject($model); } $models = ClassRegistry::keys(); foreach ($models as $currentModel) { if (ClassRegistry::isKeySet($currentModel)) { $currentObject =& ClassRegistry::getObject($currentModel); if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) { $this->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors; } } } $this->setEntity($model . '.', true); $append = ''; $created = $id = false; if (isset($object)) { $fields = $object->schema(); foreach ($fields as $key => $value) { unset($fields[$key]); $fields[$model . '.' . $key] = $value; } if (!empty($object->hasAndBelongsToMany)) { foreach ($object->hasAndBelongsToMany as $alias => $assocData) { $fields[$alias] = array('type' => 'multiple'); } } $validates = array(); if (!empty($object->validate)) { foreach ($object->validate as $validateField => $validateProperties) { if (is_array($validateProperties)) { $dims = Set::countDim($validateProperties); if ($dims == 1 && !isset($validateProperties['required']) || array_key_exists('required', $validateProperties) && $validateProperties['required'] !== false) { $validates[] = $validateField; } elseif ($dims > 1) { foreach ($validateProperties as $rule => $validateProp) { if (is_array($validateProp) && (array_key_exists('required', $validateProp) && $validateProp['required'] !== false)) { $validates[] = $validateField; } } } } } } $key = $object->primaryKey; $this->fieldset = compact('fields', 'key', 'validates'); } $data = $this->fieldset; $recordExists = isset($this->data[$model]) && isset($this->data[$model][$data['key']]) && !empty($this->data[$model][$data['key']]); if ($recordExists) { $created = true; $id = $this->data[$model][$data['key']]; } $options = array_merge(array('type' => $created && empty($options['action']) ? 'put' : 'post', 'action' => null, 'url' => null, 'default' => true), $options); if (empty($options['url']) || is_array($options['url'])) { if (empty($options['url']['controller'])) { if (!empty($model) && $model != $defaultModel) { $options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model)); } elseif (!empty($this->params['controller'])) { $options['url']['controller'] = Inflector::underscore($this->params['controller']); } } if (empty($options['action'])) { $options['action'] = $created ? 'edit' : 'add'; } $actionDefaults = array('plugin' => $this->plugin, 'controller' => $view->viewPath, 'action' => $options['action'], 'id' => $id); if (!empty($options['action']) && !isset($options['id'])) { $options['id'] = $model . Inflector::camelize($options['action']) . 'Form'; } $options['action'] = array_merge($actionDefaults, (array) $options['url']); } elseif (is_string($options['url'])) { $options['action'] = $options['url']; } unset($options['url']); switch (strtolower($options['type'])) { case 'get': $htmlAttributes['method'] = 'get'; break; case 'file': $htmlAttributes['enctype'] = 'multipart/form-data'; $options['type'] = $created ? 'put' : 'post'; case 'post': case 'put': case 'delete': $append .= $this->hidden('_method', array('name' => '_method', 'value' => strtoupper($options['type']), 'id' => null)); default: $htmlAttributes['method'] = 'post'; break; } $this->requestType = strtolower($options['type']); $htmlAttributes['action'] = $this->url($options['action']); unset($options['type'], $options['action']); if ($options['default'] == false) { if (isset($htmlAttributes['onSubmit']) || isset($htmlAttributes['onsubmit'])) { $htmlAttributes['onsubmit'] .= ' event.returnValue = false; return false;'; } else { $htmlAttributes['onsubmit'] = 'event.returnValue = false; return false;'; } } unset($options['default']); $htmlAttributes = array_merge($options, $htmlAttributes); if (isset($this->params['_Token']) && !empty($this->params['_Token'])) { $append .= $this->hidden('_Token.key', array('value' => $this->params['_Token']['key'], 'id' => 'Token' . mt_rand())); } if (!empty($append)) { $append = sprintf($this->Html->tags['fieldset'], ' style="display:none;"', $append); } $this->setEntity($model . '.', true); $attributes = $this->_parseAttributes($htmlAttributes, null, ''); return $this->output(sprintf($this->Html->tags['form'], $attributes)) . $append; }
/** * Returns an ORDER BY clause as a string. * * @param string $key Field reference, as a key (i.e. Post.title) * @param string $direction Direction (ASC or DESC) * @return string ORDER BY clause */ function order($keys, $direction = 'ASC') { if (is_string($keys) && strpos($keys, ',') && !preg_match('/\\(.+\\,.+\\)/', $keys)) { $keys = array_map('trim', explode(',', $keys)); } if (is_array($keys)) { foreach ($keys as $key => $val) { if (is_numeric($key) && empty($val)) { unset($keys[$key]); } } } if (empty($keys) || is_array($keys) && count($keys) && isset($keys[0]) && empty($keys[0])) { return ''; } $flag = isset($keys[0]) && $keys[0] == '(Model.field > 100) DESC'; if (is_array($keys)) { $keys = Set::countDim($keys) > 1 ? array_map(array(&$this, 'order'), $keys) : $keys; foreach ($keys as $key => $value) { if (is_numeric($key)) { $key = $value = ltrim(str_replace('ORDER BY ', '', $this->order($value))); $value = !preg_match('/\\x20ASC|\\x20DESC/i', $key) ? ' ' . $direction : ''; } else { $value = ' ' . $value; } if (!preg_match('/^.+\\(.*\\)/', $key) && !strpos($key, ',')) { if (preg_match('/\\x20ASC|\\x20DESC/i', $key, $dir)) { $dir = $dir[0]; $key = preg_replace('/\\x20ASC|\\x20DESC/i', '', $key); } else { $dir = ''; } Configure::write('flag', $flag); $key = trim($this->name(trim($key)) . ' ' . trim($dir)); Configure::write('flag', false); } $order[] = $this->order($key . $value); } return ' ORDER BY ' . trim(str_replace('ORDER BY', '', join(',', $order))); } $keys = preg_replace('/ORDER\\x20BY/i', '', $keys); if (strpos($keys, '.')) { preg_match_all('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', $keys, $result, PREG_PATTERN_ORDER); $pregCount = count($result['0']); for ($i = 0; $i < $pregCount; $i++) { $keys = preg_replace('/' . $result['0'][$i] . '/', $this->name($result['0'][$i]), $keys); } $result = ' ORDER BY ' . $keys; return $result . (!preg_match('/\\x20ASC|\\x20DESC/i', $keys) ? ' ' . $direction : ''); } elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) { $direction = $match['1']; return ' ORDER BY ' . preg_replace('/' . $match['1'] . '/', '', $keys) . $direction; } return ' ORDER BY ' . $keys . ' ' . $direction; }
/** * testCountDim method * * @access public * @return void */ function testCountDim() { $data = array('one', '2', 'three'); $result = Set::countDim($data); $this->assertEqual($result, 1); $data = array('1' => '1.1', '2', '3'); $result = Set::countDim($data); $this->assertEqual($result, 1); $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data); $this->assertEqual($result, 2); $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data); $this->assertEqual($result, 1); $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data, true); $this->assertEqual($result, 2); $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data); $this->assertEqual($result, 2); $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, true); $this->assertEqual($result, 3); $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, true); $this->assertEqual($result, 4); $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, true); $this->assertEqual($result, 5); $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, true); $this->assertEqual($result, 5); $set = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($set, false, 0); $this->assertEqual($result, 2); $result = Set::countDim($set, true); $this->assertEqual($result, 5); }
/** * Prepares REST data for cake interaction * * @param <type> $data * @return <type> */ protected function _modelizePost(&$data) { if (!is_array($data)) { return $data; } // Protected against Saving multiple models in one post // while still allowing mass-updates in the form of: // $this->data[1][field] = value; if (Set::countDim($data) === 2) { if (!$this->numeric($data)) { return $this->error('2 dimensional can only begin with numeric index'); } } else { if (Set::countDim($data) !== 1) { return $this->error('You may only send 1 dimensional posts'); } } // Encapsulate in Controller Model $data = array($this->Controller->modelClass => $data); return $data; }
/** * 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 * @return void */ protected function _processValidationRules($field, $rules, $file, $domain) { if (!is_array($rules)) { return; } $dims = Set::countDim($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) { $details = array('file' => $file, 'line' => 'validation for field ' . $field); $this->_addTranslation($domain, $msgid, $details); } } }
/** * Introspects model information and extracts information related * to validation, field length and field type. Appends information into * $this->fieldset. * * @return Model Returns a model instance * @access protected */ function &_introspectModel($model) { $object = null; if (is_string($model) && strpos($model, '.') !== false) { $path = explode('.', $model); $model = end($path); } if (ClassRegistry::isKeySet($model)) { $object =& ClassRegistry::getObject($model); } if (!empty($object)) { $fields = $object->schema(); foreach ($fields as $key => $value) { unset($fields[$key]); $fields[$key] = $value; } if (!empty($object->hasAndBelongsToMany)) { foreach ($object->hasAndBelongsToMany as $alias => $assocData) { $fields[$alias] = array('type' => 'multiple'); } } $validates = array(); if (!empty($object->validate)) { foreach ($object->validate as $validateField => $validateProperties) { if (is_array($validateProperties)) { $dims = Set::countDim($validateProperties); if ($dims == 1 && !isset($validateProperties['required']) || array_key_exists('required', $validateProperties) && $validateProperties['required'] !== false) { $validates[] = $validateField; } elseif ($dims > 1) { foreach ($validateProperties as $rule => $validateProp) { if (is_array($validateProp) && (array_key_exists('required', $validateProp) && $validateProp['required'] !== false)) { $validates[] = $validateField; } } } } } } $defaults = array('fields' => array(), 'key' => 'id', 'validates' => array()); $key = $object->primaryKey; $this->fieldset[$object->name] = array_merge($defaults, compact('fields', 'key', 'validates')); } return $object; }
/** * Counts the dimensions of an array. * * @param array $array Array to count dimensions on * @return int The number of dimensions in $array * @access public */ function countDim($array = null) { if ($array === null) { $array = $this->get(); } elseif (is_object($array) && is_a($array, 'set')) { $array = $array->get(); } if (is_array(reset($array))) { $return = Set::countDim(reset($array)) + 1; } else { $return = 1; } return $return; }
/** * Returns a unique, dot separated path to use as the cache key. Copied from CachedAcl. * * @param string $aro ARO * @param string $aco ACO * @param boolean $acoPath Boolean to return only the path to the ACO or the full path to the permission. * @access private */ function __cachePath($aro, $aco, $action, $acoPath = false) { if ($action != "*") { $aco .= '/' . $action; } $path = Inflector::slug($aco); if (!$acoPath) { $_aro = array(); if (!is_array($aro)) { $_aro = explode(':', $aro); } elseif (Set::countDim($aro) > 1) { $_aro = array(key($aro), current(current($aro))); } else { $_aro = array_values($aro); } $path .= '.' . Inflector::slug(implode('.', $_aro)); } return $this->settings['sessionKey'] . '.' . $path; }
/** * testCountDim method * * @return void */ public function testCountDim() { $data = array('one', '2', 'three'); $result = Set::countDim($data); $this->assertEquals(1, $result); $data = array('1' => '1.1', '2', '3'); $result = Set::countDim($data); $this->assertEquals(1, $result); $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data); $this->assertEquals(2, $result); $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data); $this->assertEquals(1, $result); $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data, TRUE); $this->assertEquals(2, $result); $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data); $this->assertEquals(2, $result); $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, TRUE); $this->assertEquals(3, $result); $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, TRUE); $this->assertEquals(4, $result); $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, TRUE); $this->assertEquals(5, $result); $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, TRUE); $this->assertEquals(5, $result); $set = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($set, FALSE, 0); $this->assertEquals(2, $result); $result = Set::countDim($set, TRUE); $this->assertEquals(5, $result); }
function intersectValidate(&$model, $arg) { if (method_exists($model, 'loadValidate')) { $model->loadValidate(); $this->loaded = TRUE; } if (is_scalar($arg)) { // for 'colA,colB' $okVali = array_flip(explode(',', $arg)); } else { if (isset($arg[$model->name])) { // for normal $data[model][colA]="xxx" $okVali = $arg[$model->name]; } else { $cnt = Set::countDim($arg); // for saveAll $data[23][colA]="xxx" if ($cnt == 2) { $okVali = array_shift($arg); } else { list($col1, $col2) = each($arg); if (is_integer($col1)) { // for columnArray array('colA', 'colB') $okVali = array_flip($arg); } else { // for columnKeyArray array('colA'=>"xxx", 'colB'=>"yyy") $okVali = $arg; } } } } $model->validate = array_intersect_key($model->validate, $okVali); }