本文整理汇总了PHP中Cake\Utility\Hash::map方法的典型用法代码示例。如果您正苦于以下问题:PHP Hash::map方法的具体用法?PHP Hash::map怎么用?PHP Hash::map使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Utility\Hash
的用法示例。
在下文中一共展示了Hash::map方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validationDefault
/**
* Default validation rules.
*
* @param Validator $validator The validator to customize.
* @return Validator
*/
public function validationDefault(Validator $validator)
{
return $validator->notEmpty('username', __d('wasabi_core', 'Please enter a username.'))->notEmpty('email', __d('wasabi_core', 'Please enter an email address.'))->add('email', ['email' => ['rule' => 'email', 'message' => __d('wasabi_core', 'Please enter a valid email address.')]])->notEmpty('group_id', __d('wasabi_core', 'Please select a group this user belongs to.'))->notEmpty('password', __d('wasabi_core', 'Please enter a password.'), 'create')->add('password', ['length' => ['rule' => ['minLength', 6], 'message' => __d('wasabi_core', 'Ensure your password consists of at least 6 characters.')]])->notEmpty('password_confirmation', __d('wasabi_core', 'Please repeat your Password.'), function ($context) {
if ($context['newRecord'] === true) {
return true;
}
if (isset($context['data']['password']) && !empty($context['data']['password'])) {
return true;
}
return false;
})->add('password_confirmation', 'equalsPassword', ['rule' => function ($passwordConfirmation, $provider) {
if ($passwordConfirmation !== $provider['data']['password']) {
return __d('wasabi_core', 'The Password Confirmation does not match the Password field.');
}
return true;
}])->add('language_id', 'isValid', ['rule' => function ($languageId) {
$languageIds = Hash::map(Configure::read('languages.backend'), '{n}', function ($language) {
return $language->id;
});
if (!in_array($languageId, $languageIds)) {
return __d('wasabi_core', 'Invalid language selected.');
}
return true;
}])->add('timezone', 'isValid', ['rule' => function ($timezone) {
if (!in_array($timezone, DateTimeZone::listIdentifiers())) {
return __d('wasabi_core', 'Invalid timezone selected.');
}
return true;
}]);
}
示例2: testMap
/**
* Test map()
*
* @return void
*/
public function testMap()
{
$data = static::articleData();
$result = Hash::map($data, '{n}.Article.id', [$this, 'mapCallback']);
$expected = [2, 4, 6, 8, 10];
$this->assertEquals($expected, $result);
}
示例3: profile
/**
* Profile action
* GET | PUT
*
* @return void
*/
public function profile()
{
$user = $this->Users->get($this->Auth->user('id'));
if ($this->request->is('put') && !empty($this->request->data)) {
/** @var User $user */
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->request->session()->write('Auth.User.language_id', $user->language_id);
$this->Flash->success(__d('wasabi_core', 'Your profile has been updated.'));
$this->redirect(['action' => 'profile']);
return;
} else {
$this->Flash->error($this->formErrorMessage);
}
}
$this->set(['user' => $user, 'languages' => Hash::map(Configure::read('languages.backend'), '{n}', function ($language) {
return ['value' => $language->id, 'text' => $language->name];
})]);
}
示例4: strToArray
/**
* Transforma uma string em array com base no $separator.
* @param string $string A string a ser transformada em array.
* @param string $separator O separador para definir o que cortará a string e definir cada elemento do array.
* @return Array O array.
*/
public function strToArray($string, $separator = ',')
{
$theArray = explode($separator, $string);
$theArray = Hash::map($theArray, '{n}', 'trim');
return $theArray;
}
示例5: reorderPages
/**
* reorderPages action
* AJAX POST
*/
public function reorderPages()
{
if (!$this->request->isAll(['ajax', 'post'])) {
throw new MethodNotAllowedException();
}
if (empty($this->request->data)) {
throw new BadRequestException();
}
$this->request->data = Hash::map($this->request->data, '{n}', function ($page) {
if ($page['parent_id'] === 'null') {
$page['parent_id'] = null;
}
return $page;
});
$pages = $this->Pages->patchEntities($this->Pages->find('all'), $this->request->data, ['associated' => false]);
/** @var Connection $connection */
$connection = $this->Pages->connection();
$connection->begin();
foreach ($pages as $page) {
$this->Pages->behaviors()->unload('Tree');
if (!$this->Pages->save($page)) {
$connection->rollback();
break;
}
}
if ($connection->inTransaction()) {
$connection->commit();
$status = 'success';
$flashMessage = __d('wasabi_cms', 'The page positions have been updated.');
} else {
$status = 'error';
$flashMessage = $this->dbErrorMessage;
}
$this->set(['status' => $status, 'flashMessage' => $flashMessage, '_serialize' => ['status', 'flashMessage']]);
$this->RequestHandler->renderAs($this, 'json');
}
示例6: reorderItems
/**
* reorderItems action
* AJAX POST
*/
public function reorderItems()
{
if (!$this->request->isAll(['ajax', 'post'])) {
throw new MethodNotAllowedException();
}
if (empty($this->request->data)) {
throw new BadRequestException();
}
$this->request->data = Hash::map($this->request->data, '{n}', function ($item) {
if ($item['parent_id'] === 'null') {
$item['parent_id'] = null;
}
return $item;
});
// save the new language positions
$menuItems = $this->MenuItems->patchEntities($this->MenuItems->find('threaded'), $this->request->data);
$this->MenuItems->connection()->begin();
foreach ($menuItems as $menuItem) {
$this->MenuItems->behaviors()->unload('Tree');
if (!$this->MenuItems->save($menuItem)) {
$this->MenuItems->connection()->rollback();
break;
}
}
if ($this->MenuItems->connection()->inTransaction()) {
$this->MenuItems->connection()->commit();
$status = 'success';
$flashMessage = __d('wasabi_core', 'The menu item positions have been updated.');
} else {
$status = 'error';
$flashMessage = $this->dbErrorMessage;
}
$this->set(['status' => $status, 'flashMessage' => $flashMessage, '_serialize' => ['status', 'flashMessage']]);
}