Skip to content
Permalink
Browse files

Using variadic on some methods

  • Loading branch information...
jrbasso committed Dec 14, 2016
1 parent 02f8000 commit 1ab2effe9b53f130be0e8752cf581dde560a7d41
@@ -891,8 +891,8 @@ public function zip($items);
*
* ```
* $collection = new Collection([1, 2]);
* $zipped = $collection->zipWith([3, 4], [5, 6], function () {
* return array_sum(func_get_args());
* $zipped = $collection->zipWith([3, 4], [5, 6], function (...$args) {
* return array_sum($args);
* });
* $zipped->toList(); // returns [9, 12]; [(1 + 3 + 5), (2 + 4 + 6)]
* ```
@@ -570,13 +570,12 @@ public function redirect($url, $status = 302)
*
* @param string $action The new action to be 'redirected' to.
* Any other parameters passed to this method will be passed as parameters to the new action.
* @param array ...$args Arguments passed to the action
* @return mixed Returns the return value of the called action
*/
public function setAction($action)
public function setAction($action, ...$args)
{
$this->request = $this->request->withParam('action', $action);
$args = func_get_args();
unset($args[0]);
return call_user_func_array([&$this, $action], $args);
}
@@ -634,17 +634,16 @@ public function __isset($name)
*
* @param string|array $type The type of request you want to check. If an array
* this method will return true if the request matches any type.
* @param array ...$args List of arguments
* @return bool Whether or not the request is the type you are checking.
*/
public function is($type)
public function is($type, ...$args)
{
if (is_array($type)) {
$result = array_map([$this, 'is'], $type);
return count(array_filter($result)) > 0;
}
$args = func_get_args();
array_shift($args);
$type = strtolower($type);
if (!isset(static::$_detectors[$type])) {
@@ -19,17 +19,17 @@
* Returns a translated string if one is found; Otherwise, the submitted message.
*
* @param string $singular Text to translate.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null The translated text, or null if invalid.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
*/
function __($singular, $args = null)
function __($singular, ...$args)
{
if (!$singular) {
return null;
}
$arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
$arguments = func_num_args() === 2 ? (array)$args[0] : $args;
return I18n::translator()->translate($singular, $arguments);
}
@@ -44,17 +44,17 @@ function __($singular, $args = null)
* @param string $singular Singular text to translate.
* @param string $plural Plural text.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string, or null if invalid.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__n
*/
function __n($singular, $plural, $count, $args = null)
function __n($singular, $plural, $count, ...$args)
{
if (!$singular) {
return null;
}
$arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 3);
$arguments = func_num_args() === 4 ? (array)$args[0] : $args;
return I18n::translator()->translate(
$plural,
@@ -70,16 +70,16 @@ function __n($singular, $plural, $count, $args = null)
*
* @param string $domain Domain.
* @param string $msg String to translate.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__d
*/
function __d($domain, $msg, $args = null)
function __d($domain, $msg, ...$args)
{
if (!$msg) {
return null;
}
$arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
$arguments = func_num_args() === 3 ? (array)$args[0] : $args;
return I18n::translator($domain)->translate($msg, $arguments);
}
@@ -96,17 +96,17 @@ function __d($domain, $msg, $args = null)
* @param string $singular Singular string to translate.
* @param string $plural Plural.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dn
*/
function __dn($domain, $singular, $plural, $count, $args = null)
function __dn($domain, $singular, $plural, $count, ...$args)
{
if (!$singular) {
return null;
}
$arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 4);
$arguments = func_num_args() === 5 ? (array)$args[0] : $args;
return I18n::translator($domain)->translate(
$plural,
@@ -124,17 +124,17 @@ function __dn($domain, $singular, $plural, $count, $args = null)
*
* @param string $context Context of the text.
* @param string $singular Text to translate.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__x
*/
function __x($context, $singular, $args = null)
function __x($context, $singular, ...$args)
{
if (!$singular) {
return null;
}
$arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
$arguments = func_num_args() === 3 ? (array)$args[0] : $args;
return I18n::translator()->translate($singular, ['_context' => $context] + $arguments);
}
@@ -152,17 +152,17 @@ function __x($context, $singular, $args = null)
* @param string $singular Singular text to translate.
* @param string $plural Plural text.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__xn
*/
function __xn($context, $singular, $plural, $count, $args = null)
function __xn($context, $singular, $plural, $count, ...$args)
{
if (!$singular) {
return null;
}
$arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 4);
$arguments = func_num_args() === 5 ? (array)$args[0] : $args;
return I18n::translator()->translate(
$plural,
@@ -181,17 +181,17 @@ function __xn($context, $singular, $plural, $count, $args = null)
* @param string $domain Domain.
* @param string $context Context of the text.
* @param string $msg String to translate.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dx
*/
function __dx($domain, $context, $msg, $args = null)
function __dx($domain, $context, $msg, ...$args)
{
if (!$msg) {
return null;
}
$arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 3);
$arguments = func_num_args() === 4 ? (array)$args[0] : $args;
return I18n::translator($domain)->translate(
$msg,
@@ -213,17 +213,17 @@ function __dx($domain, $context, $msg, $args = null)
* @param string $singular Singular text to translate.
* @param string $plural Plural text.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dxn
*/
function __dxn($domain, $context, $singular, $plural, $count, $args = null)
function __dxn($domain, $context, $singular, $plural, $count, ...$args)
{
if (!$singular) {
return null;
}
$arguments = func_num_args() === 6 ? (array)$args : array_slice(func_get_args(), 5);
$arguments = func_num_args() === 6 ? (array)$args[0] : $args;
return I18n::translator($domain)->translate(
$plural,
@@ -1553,8 +1553,8 @@ public function testZipWith()
});
$this->assertEquals([3, 8], $zipped->toList());
$zipped = $collection->zipWith([3, 4], [5, 6, 7], function () {
return array_sum(func_get_args());
$zipped = $collection->zipWith([3, 4], [5, 6, 7], function (...$args) {
return array_sum($args);
});
$this->assertEquals([9, 12], $zipped->toList());
}

0 comments on commit 1ab2eff

Please sign in to comment.
You can’t perform that action at this time.