本文整理汇总了PHP中Hash::filter方法的典型用法代码示例。如果您正苦于以下问题:PHP Hash::filter方法的具体用法?PHP Hash::filter怎么用?PHP Hash::filter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Hash
的用法示例。
在下文中一共展示了Hash::filter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: checkPage
/**
* checkPage
*
* @param array $questionnaire questionnaire
* @return array error message
*/
public function checkPage($questionnaire)
{
$this->QuestionnairePage = ClassRegistry::init('Questionnaires.QuestionnairePage', true);
// 少なくとも1ページは存在すること
if (empty($questionnaire['QuestionnairePage'])) {
$this->validationErrors['Questionnaire'][] = __d('questionnaires', 'please set at least one page.');
return false;
}
$pageSeq = 0;
foreach ($questionnaire['QuestionnairePage'] as $pageIndex => $page) {
// それぞれのページのフィールド確認
$this->QuestionnairePage->set($page);
$this->QuestionnairePage->validates(array('fieldList' => array('questionnaire_id', 'page_title', 'page_sequence', 'route_number')));
if ($this->QuestionnairePage->validationErrors) {
$this->validationErrors['QuestionnairePage'][$pageIndex] = $this->QuestionnairePage->validationErrors;
}
// ページシーケンスが0始まりで連番になっているか
if ($pageSeq != $page['page_sequence']) {
$this->validationErrors['QuestionnairePage'][$pageIndex][] = __d('questionnaires', 'Invalid page sequence set. Please try again from the beginning.');
return false;
}
// ページの中の質問についてチェック
$this->checkQuestion($questionnaire, $page, $this->validationErrors['QuestionnairePage'][$pageIndex]);
$pageSeq++;
}
$this->validationErrors = Hash::filter($this->validationErrors);
if (!empty($this->validationErrors)) {
return false;
} else {
return true;
}
}
示例2: initialize
public function initialize(Controller $Controller)
{
parent::initialize($Controller);
if (!self::_isSiftable($Controller)) {
return;
}
if (empty($Controller->request->data['Sifter'])) {
if (empty($Controller->request->params['named'])) {
return true;
}
$Controller->request->params['named'] = Hash::expand($Controller->request->params['named']);
return self::_sift($Controller);
}
$Model = $Controller->{$Controller->modelClass};
$field = array_filter((array) $Model->sifterConfig('fields'));
if (self::_isAjax($Controller)) {
if (!array_key_exists($Controller->request->data['Sifter']['search_field'], $field) || empty($Controller->request->data['Sifter']['search_value'])) {
return true;
}
return self::_setAutoComplete($Controller, array($Controller->request->data['Sifter']['search_field'] => $field[$Controller->request->data['Sifter']['search_field']]));
} elseif ($Controller->request->is('post')) {
unset($Controller->request->data['Sifter']);
$url = array();
foreach (Hash::filter($Controller->request->data) as $model => $fields) {
foreach ($fields as $field => $data) {
$url[$model . '.' . $field] = $data;
}
}
return $Controller->redirect(Hash::merge($url, array_diff_key($Controller->request->params, array('plugin' => null, 'controller' => null, 'action' => null, 'named' => null, 'pass' => null))));
}
}
示例3: replaceCircularNoticeChoices
/**
* Delete-insert circular notice choices
*
* @param array $data input data
* @return bool
* @throws InternalErrorException
*/
public function replaceCircularNoticeChoices($data)
{
$contentId = $data['CircularNoticeContent']['id'];
// 残す選択肢の条件を生成
$deleteConditions = array('CircularNoticeChoice.circular_notice_content_id' => $contentId);
$extractIds = Hash::filter(Hash::extract($data['CircularNoticeChoices'], '{n}.CircularNoticeChoice.id'));
if (count($extractIds) > 0) {
$deleteConditions['CircularNoticeChoice.id NOT'] = $extractIds;
}
// 選択肢を一旦削除
if (!$this->deleteAll($deleteConditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// 1件ずつ保存
foreach ($data['CircularNoticeChoices'] as $choice) {
$choice['CircularNoticeChoice']['circular_notice_content_id'] = $contentId;
if (!$this->validateCircularChoice($choice)) {
return false;
}
if (!$this->save(null, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
return true;
}
示例4: prepareData
private function prepareData()
{
$zeroToNull = function ($val) {
return $val != 0 ? $val : null;
};
$this->request->data['Contatos'] = $this->Session->consume("inserir_ponto_focal.{$this->user['Acesso_Portal_Usuario']['id_usuarios']}");
$this->request->data['Categorias']['Categorias'] = Hash::filter(array_map($zeroToNull, $this->request->data['Categorias']['Categorias']));
return $this;
}
示例5: display
/**
* display method
*
* @param $page
* @param $subpage
* @return void
*/
public function display($page = null, $subpage = null)
{
if (empty($page)) {
$path = func_get_args();
$count = count($path);
if (!$count) {
$this->redirect('/');
}
$page = $subpage = $title_for_layout = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path[$count - 1])) {
$title_for_layout = Inflector::humanize($path[$count - 1]);
}
} else {
$path = Hash::filter(array($page, $subpage));
}
switch ($page) {
case 'home':
$content = $this->Page->find('first', array('conditions' => array('Page.id' => 4)));
$this->set(compact('content'));
// Fetch projects
// $projects = $this->Project->find('all');
// $this->set(compact('projects'));
break;
case 'bio':
$this->page_data['page_title'][] = __('Biographie');
$content = $this->Page->find('first', array('conditions' => array('Page.id' => 1)));
$this->set(compact('content'));
break;
case 'about':
$this->page_data['page_title'][] = __('À propos');
$content = $this->Page->find('first', array('conditions' => array('Page.id' => 3)));
$this->set(compact('content'));
break;
case 'contact':
$this->page_data['page_title'][] = __('Contact');
$content = $this->Page->find('first', array('conditions' => array('Page.id' => 2)));
$this->set(compact('content'));
break;
}
$this->path = $path;
$this->set(compact('page', 'subpage', 'title_for_layout'));
$this->render(implode('/', $path));
}
示例6: setEntity
/**
* Sets this helper's model and field properties to the dot-separated value-pair in $entity.
*
* @param string $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"
* @param boolean $setScope Sets the view scope to the model specified in $tagValue
* @return void
*/
public function setEntity($entity, $setScope = false)
{
if ($entity === null) {
$this->_modelScope = false;
}
if ($setScope === true) {
$this->_modelScope = $entity;
}
$parts = array_values(Hash::filter(explode('.', $entity)));
if (empty($parts)) {
return;
}
if ($parts[0] != '_Token') {
$entity = $this->_modelScope . '.' . $entity;
}
$this->_association = null;
$this->_entityPath = $entity;
}
示例7: cadastrar
public function cadastrar()
{
$this->Acesso_Portal_Perfil->recursive = -1;
$controllers = $this->Acesso_Portal_Tela->geraListagemController();
$this->set(compact('controllers'));
if ($this->request->is(array('post', 'put'))) {
$zeroToNull = function ($val) {
return $val != 0 ? $val : null;
};
$this->request->data['Telas']['Telas'] = Hash::filter(array_map($zeroToNull, $this->request->data['Telas']['Telas']));
unset($this->request->data['Base']);
$this->Acesso_Portal_Perfil->create();
$data = $this->request->data;
$this->request->data = "";
if ($this->Acesso_Portal_Perfil->save($data)) {
$this->Session->setFlash($this->useLogger('Perfil Cadastrado com Sucesso'));
return $this->redirect('/perfis/index');
}
$errors = $this->Acesso_Portal_Perfil->validationErrors;
$this->Session->setFlash($this->useLogger($errors));
}
}
示例8: getPath
public static function getPath($url)
{
$parse = parse_url($url);
$url = $parse['path'];
$pathSegments = explode('.', $url);
$ext = array_pop($pathSegments);
$url = implode('.', $pathSegments);
$parts = explode('/', $url);
$parts = Hash::filter($parts);
$route = Router::parse($url . '/lang:' . Configure::read('Config.language'));
// Thème
if (isset($route['controller']) && $route['controller'] === 'theme') {
$parts = array_merge(array('View', 'Themed', $route['action'], 'webroot'), $route['pass']);
} elseif (isset($route['plugin']) && $route['plugin'] != '') {
foreach ($parts as $k => $v) {
if ($v == $route['plugin']) {
unset($parts[$k]);
break;
}
unset($parts[$k]);
}
}
$endPath = implode(DS, $parts);
if (isset($route['controller']) && $route['controller'] === 'theme') {
$basePath = APP;
} else {
if (isset($route['plugin']) && $route['plugin'] != '') {
$basePath = CakePlugin::path(Inflector::camelize($route['plugin'])) . WEBROOT_DIR . DS;
} else {
$basePath = APP . WEBROOT_DIR . DS;
}
}
// Remove APP_DIR path
$basePath = str_replace(APP, '', $basePath);
return $basePath . $endPath . '.' . $ext;
}
示例9: testFilter
/**
* testFilter method
*
* @return void
*/
public function testFilter()
{
$result = Hash::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
$expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be'));
$this->assertSame($expected, $result);
$result = Hash::filter(array(1, array(false)));
$expected = array(1);
$this->assertEquals($expected, $result);
$result = Hash::filter(array(1, array(false, false)));
$expected = array(1);
$this->assertEquals($expected, $result);
$result = Hash::filter(array(1, array('empty', false)));
$expected = array(1, array('empty'));
$this->assertEquals($expected, $result);
$result = Hash::filter(array(1, array('2', false, array(3, null))));
$expected = array(1, array('2', 2 => array(3)));
$this->assertEquals($expected, $result);
$this->assertSame(array(), Hash::filter(array()));
}
示例10: filter
/**
* Filter handler for Pluck methods - strips out empty values, or anything
* passed in via $filterCallback
*
* See Hash::filter() for more information
*
* @param array $data
* @param mixed $filterCallback
* - false will not run Hash::filter()
* removes noting, untouched
* - true will run Hash::filter($data, array('Pluck', '_filterExcludeZero'))
* to remove all empties including 0
* - null will run Hash::filter($data)
* to remove all empties except 0
* - {else} will run Hash::filter($data, $filterCallback)
* to remove whatever you tell it to remove
* $filterCallback should be: array($className, $methodName)
* @return array $data
*/
public static function filter($data, $filterCallback = null)
{
if ($filterCallback === false) {
// not filtering
return $data;
}
if ($filterCallback === true) {
// custom callback
// remove all empties AND 0
return Hash::filter($data, array('Pluck', '_filterExcludeZero'));
}
if (empty($filterCallback)) {
// default Hash::filter()
// removes all empties EXCEPT 0
return Hash::filter($data);
}
// custom callback
return Hash::filter($data, $filterCallback);
}
示例11: parseContain
/**
* Parse the `contain` option of the query recursively
*
* @param Model $parent Parent model of the contained model
* @param string $alias Alias of the contained model
* @param array $contain Reformatted `contain` option for the deep associations
* @param array|null $context Context
* @return array
* @throws InvalidArgumentException
*/
private function parseContain(Model $parent, $alias, array $contain, $context = null)
{
// @codingStandardsIgnoreLine
if ($context === null) {
$context = array('root' => $parent->alias, 'aliasPath' => $parent->alias, 'propertyPath' => '', 'forceExternal' => false);
}
$aliasPath = $context['aliasPath'] . '.' . $alias;
$propertyPath = ($context['propertyPath'] ? $context['propertyPath'] . '.' : '') . $alias;
$types = $parent->getAssociated();
if (!isset($types[$alias])) {
throw new InvalidArgumentException(sprintf('Model "%s" is not associated with model "%s"', $parent->alias, $alias), E_USER_WARNING);
}
$parentAlias = $parent->alias;
$target = $parent->{$alias};
$type = $types[$alias];
$relation = $parent->{$type}[$alias];
$options = $contain['options'] + array_intersect_key(Hash::filter($relation), $this->containOptions);
$has = stripos($type, 'has') !== false;
$many = stripos($type, 'many') !== false;
$belong = stripos($type, 'belong') !== false;
if ($has && $belong) {
$parentKey = $parent->primaryKey;
$targetKey = $target->primaryKey;
$habtmAlias = $relation['with'];
$habtm = $parent->{$habtmAlias};
$habtmParentKey = $relation['foreignKey'];
$habtmTargetKey = $relation['associationForeignKey'];
} elseif ($has) {
$parentKey = $parent->primaryKey;
$targetKey = $relation['foreignKey'];
} else {
$parentKey = $relation['foreignKey'];
$targetKey = $target->primaryKey;
}
if (!empty($relation['external'])) {
$external = true;
}
if (!empty($relation['finderQuery'])) {
$finderQuery = $relation['finderQuery'];
}
$meta = compact('alias', 'parent', 'target', 'parentAlias', 'parentKey', 'targetKey', 'aliasPath', 'propertyPath', 'options', 'has', 'many', 'belong', 'external', 'finderQuery', 'habtm', 'habtmAlias', 'habtmParentKey', 'habtmTargetKey');
if ($this->isExternal($context, $meta)) {
$meta['external'] = true;
$context['root'] = $aliasPath;
$context['propertyPath'] = $alias;
$path = $context['aliasPath'];
} else {
$meta['external'] = false;
if ($context['root'] !== $context['aliasPath']) {
$meta['eager'] = true;
}
$context['propertyPath'] = $propertyPath;
$path = $context['root'];
}
$this->metas[$path][] = $meta;
$context['aliasPath'] = $aliasPath;
$context['forceExternal'] = !empty($finderQuery);
foreach ($contain['contain'] as $key => $val) {
$this->parseContain($target, $key, $val, $context);
}
return $this->metas;
}
示例12: testFilter
/**
* testFilter method
*
* @return void
*/
public function testFilter()
{
$result = Hash::filter(array('0', FALSE, TRUE, 0, array('one thing', 'I can tell you', 'is you got to be', FALSE)));
$expected = array('0', 2 => TRUE, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be'));
$this->assertSame($expected, $result);
$result = Hash::filter(array(1, array(FALSE)));
$expected = array(1);
$this->assertEquals($expected, $result);
$result = Hash::filter(array(1, array(FALSE, FALSE)));
$expected = array(1);
$this->assertEquals($expected, $result);
$result = Hash::filter(array(1, array('empty', FALSE)));
$expected = array(1, array('empty'));
$this->assertEquals($expected, $result);
$result = Hash::filter(array(1, array('2', FALSE, array(3, NULL))));
$expected = array(1, array('2', 2 => array(3)));
$this->assertEquals($expected, $result);
$this->assertSame(array(), Hash::filter(array()));
}
示例13: addLibs
public function addLibs($libs, $type = 'css', $prepend = false)
{
if (!is_array($libs)) {
$libs = explode(',', $libs);
$libs = array_map('trim', $libs);
$libs = Hash::filter($libs);
}
if (empty($libs)) {
return;
}
foreach ($libs as $key => $value) {
if (is_numeric($value)) {
$lib = $key;
$priority = $value;
} else {
$lib = $value;
$priority = 1001;
}
if ($this->libExists($lib, $type)) {
continue;
}
while (isset($this->libs[$type]['files'][$priority])) {
$priority++;
}
$this->libs[$type]['files'][$priority] = $lib;
}
ksort($this->libs[$type]['files'], SORT_NUMERIC);
}
示例14: setEntity
/**
* Sets this helper's model and field properties to the dot-separated value-pair in $entity.
*
* @param string $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"
* @param boolean $setScope Sets the view scope to the model specified in $tagValue
* @return void
*/
public function setEntity($entity, $setScope = false)
{
if ($entity === null) {
$this->_modelScope = false;
}
if ($setScope === true) {
$this->_modelScope = $entity;
}
$parts = array_values(Hash::filter(explode('.', $entity)));
if (empty($parts)) {
return;
}
$count = count($parts);
$lastPart = isset($parts[$count - 1]) ? $parts[$count - 1] : null;
// Either 'body' or 'date.month' type inputs.
if ($count === 1 && $this->_modelScope && !$setScope || $count === 2 && in_array($lastPart, $this->_fieldSuffixes) && $this->_modelScope && $parts[0] !== $this->_modelScope) {
$entity = $this->_modelScope . '.' . $entity;
}
// 0.name, 0.created.month style inputs. Excludes inputs with the modelScope in them.
if ($count >= 2 && is_numeric($parts[0]) && !is_numeric($parts[1]) && $this->_modelScope && strpos($entity, $this->_modelScope) === false) {
$entity = $this->_modelScope . '.' . $entity;
}
$this->_association = null;
$isHabtm = isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) && $this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple';
// habtm models are special
if ($count === 1 && $isHabtm) {
$this->_association = $parts[0];
$entity = $parts[0] . '.' . $parts[0];
} else {
// check for associated model.
$reversed = array_reverse($parts);
foreach ($reversed as $i => $part) {
if ($i > 0 && preg_match('/^[A-Z]/', $part)) {
$this->_association = $part;
break;
}
}
}
$this->_entityPath = $entity;
}
示例15: _handleNoRoute
/**
* A special fallback method that handles URL arrays that cannot match
* any defined routes.
*
* @param array $url A URL that didn't match any routes
* @return string A generated URL for the array
* @see Router::url()
*/
protected static function _handleNoRoute($url)
{
$named = $args = array();
$skip = array_merge(array('bare', 'action', 'controller', 'plugin', 'prefix'), self::$_prefixes);
$keys = array_values(array_diff(array_keys($url), $skip));
$count = count($keys);
// Remove this once parsed URL parameters can be inserted into 'pass'
for ($i = 0; $i < $count; $i++) {
$key = $keys[$i];
if (is_numeric($keys[$i])) {
$args[] = $url[$key];
} else {
$named[$key] = $url[$key];
}
}
list($args, $named) = array(Hash::filter($args), Hash::filter($named));
foreach (self::$_prefixes as $prefix) {
$prefixed = $prefix . '_';
if (!empty($url[$prefix]) && strpos($url['action'], $prefixed) === 0) {
$url['action'] = substr($url['action'], strlen($prefixed) * -1);
break;
}
}
if (empty($named) && empty($args) && (!isset($url['action']) || $url['action'] === 'index')) {
$url['action'] = null;
}
$urlOut = array_filter(array($url['controller'], $url['action']));
if (isset($url['plugin'])) {
array_unshift($urlOut, $url['plugin']);
}
foreach (self::$_prefixes as $prefix) {
if (isset($url[$prefix])) {
array_unshift($urlOut, $prefix);
break;
}
}
$output = implode('/', $urlOut);
if (!empty($args)) {
$output .= '/' . implode('/', array_map('rawurlencode', $args));
}
if (!empty($named)) {
foreach ($named as $name => $value) {
if (is_array($value)) {
$flattend = Hash::flatten($value, '%5D%5B');
foreach ($flattend as $namedKey => $namedValue) {
$output .= '/' . $name . "%5B{$namedKey}%5D" . self::$_namedConfig['separator'] . rawurlencode($namedValue);
}
} else {
$output .= '/' . $name . self::$_namedConfig['separator'] . rawurlencode($value);
}
}
}
return $output;
}