本文整理汇总了PHP中Set::apply方法的典型用法代码示例。如果您正苦于以下问题:PHP Set::apply方法的具体用法?PHP Set::apply怎么用?PHP Set::apply使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Set
的用法示例。
在下文中一共展示了Set::apply方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: gerarListagem
public function gerarListagem()
{
$data = $this->find('list');
if ($data) {
return Set::apply("/", $data, function ($val) {
return $val . " dias";
}, array("type" => "map"));
}
return array();
}
示例2: retornaGraficoJson
public function retornaGraficoJson(array $default = null, array $data_inicio = null, array $data_fim = null)
{
$status = $this->consultaVolumeStatus($default, $data_inicio, $data_fim);
$total_eventos = Set::apply('/GestaoEvento_Status/quantidade', $status, 'array_sum');
foreach ($status as &$row) {
$row['Graph']['id'] = $row[$this->alias][$this->primaryKey];
$row['Graph']['value'] = (int) $row[$this->alias]['quantidade'];
$row['Graph']['color'] = $row[$this->alias]['color'];
$row['Graph']['highlight'] = $row[$this->alias]['color'];
$row['Graph']['label'] = $row[$this->alias]['status'];
$row['Graph']['perc'] = $row[$this->alias]['quantidade'] * 100 / $total_eventos;
}
return Hash::extract($status, '{n}.Graph');
}
示例3: formatDateFields
public function formatDateFields($results, $dates, $format = "%d/%m/%Y")
{
if (!empty($dates)) {
$setResult = function ($val, $key) use(&$results, $format) {
$innerKey = explode("/", $key);
$results[$innerKey[0]][$innerKey[1]][$innerKey[2]] = CakeTime::format($val, $format);
};
$extracted = Set::extract($results, "/{$this->alias}");
$dateExtracted = Set::classicExtract($extracted, '{\\w+}.{\\w+}.{' . implode("|", $dates) . '}');
$applied = Set::apply("/", $dateExtracted, function ($val) {
return count(Set::filter($val)) > 0 ? $val : null;
});
if ($applied) {
$flatten = Set::flatten($applied, "/");
array_walk($flatten, $setResult);
}
}
return $results;
}
示例4: testApply
/**
* testSetApply method
*
* @return void
*/
public function testApply()
{
$data = array(array('Movie' => array('id' => 1, 'title' => 'movie 3', 'rating' => 5)), array('Movie' => array('id' => 1, 'title' => 'movie 1', 'rating' => 1)), array('Movie' => array('id' => 1, 'title' => 'movie 2', 'rating' => 3)));
$result = Set::apply('/Movie/rating', $data, 'array_sum');
$expected = 9;
$this->assertEquals($expected, $result);
$result = Set::apply('/Movie/rating', $data, 'array_product');
$expected = 15;
$this->assertEquals($expected, $result);
$result = Set::apply('/Movie/title', $data, 'ucfirst', array('type' => 'map'));
$expected = array('Movie 3', 'Movie 1', 'Movie 2');
$this->assertEquals($expected, $result);
$result = Set::apply('/Movie/title', $data, 'strtoupper', array('type' => 'map'));
$expected = array('MOVIE 3', 'MOVIE 1', 'MOVIE 2');
$this->assertEquals($expected, $result);
$result = Set::apply('/Movie/rating', $data, array('SetTest', 'method'), array('type' => 'reduce'));
$expected = 9;
$this->assertEquals($expected, $result);
$result = Set::apply('/Movie/rating', $data, 'strtoupper', array('type' => 'non existing type'));
$expected = NULL;
$this->assertEquals($expected, $result);
}
示例5: add
//.........这里部分代码省略.........
if (empty($parent)) {
$cValidates = $pValidates = false;
}
$child = $this->Roster->setDefaultData(array('roster' => $child, 'involvement' => $involvement, 'defaults' => $this->data['Default'], 'creditCard' => $this->data, 'payer' => $this->activeUser, 'parent' => $parent));
// save validate success only if we haven't failed yet (so not to overwrite a failure)
if ($cValidates) {
$cValidates = $this->Roster->saveAll($child, array('validate' => 'only'));
} else {
// still validate this roster to generate errors
$this->Roster->saveAll($child, array('validate' => 'only'));
}
}
}
// check to make sure this doesn't exceed the roster limit
$lValidates = true;
$currentCount = $this->Roster->find('count', array('conditions' => array('Roster.involvement_id' => $involvement['Involvement']['id'], 'Roster.roster_status_id' => 1), 'contain' => false));
$rosterCount = count($this->data['Adult']);
$childCount = isset($this->data['Child']) ? count($this->data['Child']) : 0;
if (!empty($involvement['Involvement']['roster_limit'])) {
$lValidates = $rosterCount + $childCount + $currentCount <= $involvement['Involvement']['roster_limit'];
} else {
$lValidates = true;
}
$this->set('involvement', $involvement);
// combine roster validation errors
$this->Roster->validationErrors = $this->Roster->_validationErrors;
$Adult = new Model(array('table' => false, 'name' => 'Adult'));
$Adult->validationErrors = $this->Roster->_validationErrors;
// check all validation before continuing with save
if ($rosterCount > 0 && $lValidates && $rValidates && $cValidates && $pValidates) {
// Now that we know that the data will save, let's run the credit card
// get all signed up users (for their name)
// calculate amount (use array_values to reset keys)
$amount = Set::apply('/Payment/amount', array_values($this->data['Adult']), 'array_sum');
if (isset($this->data['Child'])) {
$amount += Set::apply('/Payment/amount', array_values($this->data['Child']), 'array_sum');
}
$signedUpIds = array_merge(Set::extract('/Adult/Roster/user_id', $this->data), Set::extract('/Child/Roster/user_id', $this->data));
$signedupUsers = $this->Roster->User->Profile->find('all', array('conditions' => array('user_id' => $signedUpIds), 'contain' => false));
$verb = count($signedupUsers) > 1 ? 'have' : 'has';
foreach ($signedupUsers as $key => $signedupUser) {
$signedupUsers[$key]['answers'] = Set::extract('/Adult/Roster[user_id=' . $signedupUser['Profile']['user_id'] . ']/../Answer', $this->data);
}
$this->set(compact('verb', 'signedupUsers'));
$signupSuccess = false;
$paymentSuccess = false;
$redirect = null;
if ($involvement['Involvement']['take_payment'] && $this->data['Default']['payment_option_id'] > 0 && !$this->data['Default']['pay_later'] && $amount > 0) {
$description = implode(' and ', Set::extract('/Profile/name', $signedupUsers)) . ' ' . $verb . ' been signed up for ' . $involvement['InvolvementType']['name'] . ' ' . $involvement['Involvement']['name'];
$paymentOption = $this->Roster->PaymentOption->read(null, $this->data['Default']['payment_option_id']);
$this->data['CreditCard']['invoice_number'] = $paymentOption['PaymentOption']['account_code'];
$this->data['CreditCard']['description'] = $description;
$this->data['CreditCard']['email'] = $user['Profile']['primary_email'];
$this->data['CreditCard']['amount'] = $amount;
if ($CreditCard->save($this->data['CreditCard'])) {
// save main rosters
foreach ($this->data['Adult'] as $signuproster) {
$this->Roster->create();
// include transaction id
$signuproster['Payment'][0]['transaction_id'] = $CreditCard->transactionId;
$signuproster['Payment'][0]['number'] = substr($this->data['CreditCard']['credit_card_number'], -4);
$this->Roster->saveAll($signuproster, array('validate' => false));
$this->Notifier->notify(array('to' => $signuproster['Roster']['user_id'], 'template' => 'involvements_signup', 'subject' => 'Signed up for ' . $involvement['Involvement']['name']));
}
// save childcares
if (isset($this->data['Child']) && count($this->data['Child'])) {
示例6: removeIndisponiveisContagem
private function removeIndisponiveisContagem($data)
{
return Set::filter(Set::apply("/", $data, function ($val) {
return !in_array($val['Sobressalente_Equip_Serial']['cm_atual'], ['REPARO', 'FURTO', 'BAIXA']) ? $val : null;
}, ['type' => 'map']));
}
示例7: _center
/**
* Returns a numerically indexed array of the latitude and longitude, based on
* user-defined center or the average of the buffered addresses
*
* @return array
*/
protected function _center()
{
if (!$this->center) {
// set center to average points
$this->center = array(Set::apply('/lat', $this->_addresses, 'array_sum') / count($this->_addresses), Set::apply('/lng', $this->_addresses, 'array_sum') / count($this->_addresses));
}
return $this->center;
}
示例8: add
/**
* Adds a payment to a roster record
*
* @param string $id The roster id to attach the payment to
*/
public function add($id)
{
// persist selected items through POST requests
$this->here .= '/mspersist:1';
if ($id) {
$ids = array($id);
} else {
$ids = $this->_extractIds();
}
// get selected
$users = $this->Payment->Roster->find('all', array('conditions' => array('Roster.id' => $ids), 'contain' => array('User' => array('Profile'))));
$involvement = $this->Payment->Roster->Involvement->find('first', array('conditions' => array('Involvement.id' => $users[0]['Roster']['involvement_id']), 'contain' => array('InvolvementType')));
// bind CreditCard to process the card
$this->Payment->bindModel(array('hasOne' => array('CreditCard' => array('foreignKey' => false))));
if (!empty($this->data)) {
$paymentType = $this->Payment->PaymentType->read(array('name'), $this->data['Payment']['payment_type_id']);
if (count($users) > 1) {
// don't allow adding multiple credits
$payForUsers = Set::extract('/Roster[balance>0]/..', $users);
} else {
$payForUsers = $users;
}
// get balance
$balance = Set::apply('/Roster/balance', $payForUsers, 'array_sum');
$amount = $this->data['Payment']['amount'] = preg_replace('/[^\\d\\.\\-]/', '', $this->data['Payment']['amount']);
// set `amount` validation rule to reflect balance range and validate as it's
// own field because we'll be splitting the payments up
if ($amount > $balance) {
$this->Payment->invalidate('amount', 'Your chosen amount must be at or under $' . $balance . '.');
}
// assuming all users still have a balance
$avg = round($amount / count($payForUsers), 2);
// build payment records (transaction id to be added later)
$payments = array();
foreach ($payForUsers as $user) {
// get amount they can receive
$amt = $avg <= $user['Roster']['balance'] ? $avg : $user['Roster']['balance'];
// if less than the average, get the amount left to distribute elsewhere
$amount -= $amt;
// saving balance in here as well, to be removed later
$payments[] = array('balance' => $user['Roster']['balance'] - $amt, 'user_id' => $user['Roster']['user_id'], 'roster_id' => $user['Roster']['id'], 'amount' => $amt, 'payment_type_id' => $this->data['Payment']['payment_type_id'], 'payment_placed_by' => $this->activeUser['User']['id']);
// to associate with invoice number
$paymentOption = $this->Payment->Roster->PaymentOption->read(null, $user['Roster']['payment_option_id']);
}
// if there was any left over, distribute to other users, otherwise, remove unwanted field
foreach ($payments as &$payment) {
if ($amount > 0) {
// get and then set the amount they can receive
$amt = $amount <= $payment['balance'] ? $amount : $payment['balance'];
$payment['amount'] += $amt;
$amount -= $amt;
}
unset($payment['balance']);
}
// create extra fields for credit card
$verb = count($payForUsers) > 1 ? 'have' : 'has';
$pVerb = count($payForUsers) > 1 ? 'had payments' : 'made a payment';
// comma's and the like are not permitted by Authorize.net
$description = implode(' and ', Set::extract('/User/Profile/name', $payForUsers)) . ' ' . $verb . ' ' . $pVerb . ' made for ' . $involvement['InvolvementType']['name'] . ' ' . $involvement['Involvement']['name'];
$this->data['CreditCard']['invoice_number'] = $paymentOption['PaymentOption']['account_code'];
$this->data['CreditCard']['description'] = $description;
$this->data['CreditCard']['email'] = $this->activeUser['Profile']['primary_email'];
$this->data['CreditCard']['amount'] = $this->data['Payment']['amount'];
// make sure all the fields validate before charging the card, if there is one
if (empty($this->Payment->validationErrors) && $this->Payment->saveAll($payments, array('validate' => 'only'))) {
$isCreditCard = isset($this->data['CreditCard']) && isset($this->data['CreditCard']['credit_card_number']);
// next, make sure the credit card gets authorized
if ($isCreditCard) {
$pValidates = $this->Payment->CreditCard->save($this->data['CreditCard']);
} else {
// no extra validation for other payment types
$pValidates = true;
}
if ($pValidates) {
// credit card as been charged, save the payment record
$this->Payment->create();
foreach ($payments as &$completePayment) {
if ($isCreditCard) {
// credit card
$completePayment['number'] = substr($this->data['CreditCard']['credit_card_number'], -4);
$completePayment['transaction_id'] = $this->Payment->CreditCard->transactionId;
} elseif (isset($this->data['Payment']['number'])) {
// other
$completePayment['number'] = $this->data['Payment']['number'];
}
}
$this->Payment->saveAll($payments, array('validate' => false));
$this->set('involvement', $involvement);
$this->set('payer', $this->activeUser);
$this->set('amount', $this->data['Payment']['amount']);
$leaders = $this->Payment->Roster->Involvement->getLeaders($involvement['Involvement']['id']);
$subject = $this->activeUser['Profile']['name'] . ' made a payment for ' . $involvement['Involvement']['name'];
foreach ($leaders as $leader) {
$this->Notifier->notify(array('to' => $leader, 'template' => 'payments_add_leader', 'subject' => $subject));
}
//.........这里部分代码省略.........
示例9: testAcceptAll
/**
* Tests acceptAll method
*
*/
public function testAcceptAll()
{
$data = $this->record;
unset($data['Problem']['id']);
$this->Problem->add('ProblematicArticle', 'article-1', 3, $data);
$data['Problem']['type'] = 'other';
$this->Problem->add('ProblematicArticle', 'article-1', 4, $data);
$result = $this->Problem->acceptAll('ProblematicArticle', 'article-1');
$this->assertTrue($result);
$result = $this->Problem->find('all', array('fields' => 'accepted', 'conditions' => array('foreign_key' => 'article-1')));
$this->assertEqual(3, Set::apply('/Problem/accepted', $result, 'array_sum'));
$result = $this->Problem->acceptAll('ProblematicArticle', 'article-1', false);
$this->assertTrue($result);
$result = $this->Problem->find('all', array('fields' => 'accepted', 'conditions' => array('foreign_key' => 'article-1')));
$this->assertEqual(0, Set::apply('/Problem/accepted', $result, 'array_sum'));
}
示例10: gera_tabela_cm
protected function gera_tabela_cm()
{
$data = array_map(function ($val) {
return empty($val) ? null : $val;
}, $this->request->data);
$count = count(array_filter(array_slice($data, 2)));
if ($count === 0 || $count === 1 && !empty($data['empresa'])) {
return null;
}
$listagem = in_array($data['cm_origem'], ['FURTO', 'REPARO', 'BAIXA']) ? $this->Sobressalente_Equip_Serial->listaItensIndisponiveis($data['empresa'], $data['fornecedor'], $data['cm_origem'], $data['estacao_origem']) : $this->Sobressalente_Equip_Serial->listaItensPorCMEstacao($data['cm_origem'], $data['estacao_origem'], $data['empresa'], $data['fornecedor']);
array_walk($listagem, function ($val, $key) use(&$listagem, &$data) {
$listagem[$key]['itens'] = $this->Sobressalente_Equip_Serial->listaProdutoPorCM($val['Sobressalente_Equip_Descricao']['id_equip_descricao'], $data['cm_origem'], $data['estacao_origem'], $data['empresa']);
$listagem[$key][0]['quantidade_indisponivel'] = count(Set::filter(Set::apply('/', $listagem[$key]['itens'], function ($val) {
return in_array($val['Sobressalente_Equip_Serial']['cm_atual'], ['REPARO', 'BAIXA']) || isset($val['Sobressalente_Equip_Serial']['est_atual']) ? $val : null;
}, ['type' => 'map'])));
$listagem[$key][0]['quantidade'] = count($listagem[$key]['itens']);
$listagem[$key][0]['quantidade_disponivel'] = $listagem[$key][0]['quantidade'] - $listagem[$key][0]['quantidade_indisponivel'];
});
return $listagem;
}
示例11: testAdd
public function testAdd()
{
ClassRegistry::getObject('CreditCard')->getGateway()->setReturnValue('_request', '1||||||123456');
$this->su(array('User' => array('id' => 1), 'Group' => array('id' => 1), 'Profile' => array('primary_email' => 'test@test.com')));
$this->Payments->Session->write('MultiSelect.test', array('selected' => array(2, 1)));
$data = array('Payment' => array('payment_type_id' => 1, 'amount' => 100), 'CreditCard' => array('address_line_1' => '123 Main St.', 'address_line_2' => null, 'city' => 'Anytown', 'state' => 'CA', 'zip' => '12345', 'first_name' => 'Joe', 'last_name' => 'Schmoe', 'credit_card_number' => '4012888818888', 'cvv' => '123', 'expiration_date' => array('month' => '04', 'year' => '2080')));
// too much
$vars = $this->testAction('/payments/add/0/Involvement:1/mstoken:test', array('return' => 'vars', 'data' => $data));
$results = $this->Payments->Payment->find('all', array('conditions' => array('Roster.involvement_id' => 1), 'contain' => array('Roster')));
$this->assertEqual($results, array());
// split between 2 people
$notificationCountBefore = $this->Payments->Payment->User->Notification->find('count');
$data['Payment']['amount'] = 10;
$vars = $this->testAction('/payments/add/0/Involvement:1/mstoken:test', array('return' => 'vars', 'data' => $data));
$results = $this->Payments->Payment->find('all', array('conditions' => array('Roster.involvement_id' => 1), 'contain' => array('Roster')));
$amounts = Set::extract('/Payment/amount', $results);
$expected = array(5, 5);
$this->assertEqual($amounts, $expected);
$numbers = Set::extract('/Payment/number', $results);
$this->assertEqual($numbers, array(8888, 8888));
$notificationCountAfter = $this->Payments->Payment->User->Notification->find('count');
$this->assertEqual($notificationCountAfter - $notificationCountBefore, 1);
// pay the rest of 1 person who only has 5 left, then the other 20 on the other
$data['Payment']['amount'] = 25;
$vars = $this->testAction('/payments/add/0/Involvement:1/mstoken:test', array('return' => 'vars', 'data' => $data));
$results = $this->Payments->Payment->find('all', array('conditions' => array('Roster.involvement_id' => 1), 'contain' => array('Roster')));
$total = Set::apply('/Payment/amount', $results, 'array_sum');
$this->assertIdentical($total, 35.0);
$results = Set::extract('/Payment/amount', $results);
$expected = array(5, 5, 5, 20);
$this->assertEqual($results, $expected);
// add a cash credit
$data = array('Payment' => array('amount' => -5, 'payment_type_id' => 2));
$vars = $this->testAction('/payments/add/1/Involvement:1', array('return' => 'vars', 'data' => $data));
$results = $this->Payments->Payment->find('all', array('conditions' => array('Payment.roster_id' => 1)));
$total = Set::apply('/Payment/amount', $results, 'array_sum');
$this->assertIdentical($total, 5.0);
// check money sanitization
$data = array('Payment' => array('amount' => '$5.00', 'payment_type_id' => 2));
$vars = $this->testAction('/payments/add/1/Involvement:1', array('return' => 'vars', 'data' => $data));
$result = $this->Payments->Payment->validationErrors;
$expected = array();
$this->assertEqual($result, $expected);
$results = $this->Payments->Payment->find('all', array('conditions' => array('Payment.roster_id' => 1)));
$total = Set::apply('/Payment/amount', $results, 'array_sum');
$this->assertIdentical($total, 10.0);
}