Methods
_.noConflict
,_.mixin
and_.template
are not relevant in the context of PHP
Methods
_.escape
and_.unescape
belong toDsheiko\Extras\Strings
Method
_.times
belongs toDsheiko\Extras\Functions
Returns the same value that is used as the argument. In math: f(x) = x
This function looks useless, but is used throughout Underscore as a default iteratee.
identity(): callable
<?php
$res = Utils::identity();
$res(42); // 42
Creates a function that returns the same value that is used as the argument of the method
{mixed} $value
- arbitrary argument
constant($value): callable
<?php
$res = Utils::constant(42);
$res(1, 2, 3); // 42
Returns null irrespective of the arguments passed to it. Useful as the default for optional callback arguments.
{array} ...$args
(optional)
noop(...$args)
<?php
$res = Utils::noop(1,2,3); // null
Returns a random integer between min and max, inclusive. If you only pass one argument, it will return a number between 0 and that number
{int} $min
{int} $max
(optional)
random(int $min , int $max = null)
<?php
$res = Utils::random(100); // 42
$res = Utils::random(5, 10); // 7
Generates a callback that can be applied to each element in a collection.
{mixed} $value
{object} $context
(optional) context object to bind to
iteratee($value, $context = null): callable
<?php
// return identity() for null
$res = Utils::iteratee(null);
$res(1); // 1
<?php
// return matcher() for an array
$macther = Utils::iteratee(["foo" => "FOO"]);
$res = Arrays::find([["foo" => "FOO"]], $macther); // ["foo" => "FOO"]
<?php
// return normalized callablefor a callable
$res = Utils::iteratee(function(){ return 42; });
$res(); // 42
<?php
// bind callable for a context
$obj = (object)["value" => 42];
$res = Utils::iteratee(function(){ return $this->value; }, $obj);
$res(); // 42
<?php
// return property() for other types
$res = Utils::iteratee("foo");
$res(["foo" => "FOO"]); // "FOO"
Generate a globally-unique id for client-side models or DOM elements that need one. If prefix is passed, the id will be appended to it.
{string} $prefix
(optional)
uniqueId(string $prefix = null): string
<?php
$res = Utils::uniqueId(); // "5acb4ab426fc9"
<?php
$res = Utils::uniqueId("contact_"); // "contact_5acb4ab427262"
If the value of the named property is a function then invoke it with the object as context; otherwise, return it. If a default value is provided and the property doesn't exist or is undefined then the default will be returned. If defaultValue is a function its result will be returned.
{array} $array
- source key-value array{string} $prop
- property name
result(array $array, string $prop)
<?php
$options = [
"foo" => "FOO",
"bar" => function() {
return "BAR";
},
];
echo Utils::result($options, "foo"); // "FOO"
echo Utils::result($options, "bar"); // "BAR"
Returns an integer timestamp for the current time
now(): int
<?php
echo Utils::now(); // 1392066795351