Invokes the function for each value in the array. Creates a new array containing the values returned by the function.
Example:
$array = array('k1', 'k2', 'k3');
$result = Arrays::map($array, function ($value) {
return 'new_' . $value;
});
Result:
Array
(
[0] => new_k1
[1] => new_k2
[2] => new_k3
)
private function _prepareParameters($uri) { preg_match_all('#:(\\w+)#', $uri, $matches); $parameters = Arrays::getValue($matches, 1, array()); return Arrays::map($parameters, function ($parameter) { return '$' . $parameter; }); }
private function whereWithUsing() { $usingClauses = $this->_query->usingClauses; $whereClauses = Arrays::map($usingClauses, function (JoinClause $usingClause) { return WhereClause::create($usingClause->getJoinColumnWithTable() . ' = ' . $usingClause->getJoinedColumnWithTable()); }); return $this->_where(array_merge($whereClauses, $this->_query->whereClauses)); }
public function contents() { $this->_setupTablePlaceholderReplacements(); $this->classStub->addPlaceholderReplacement('class', $this->className); $this->classStub->addPlaceholderReplacement('namespace', $this->classNamespace); Arrays::map($this->tableInfo->tableColumns, array($this->classStub, 'addColumn')); return $this->classStub->contents(); }
/** * Returns comparator which compares objects by using values computed using given expressions. * Expressions should comply with format accepted by <code>Functions::extractExpression</code>. * Comparator returns an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. * * @param mixed ... * @return callable */ public static function compareBy() { $expressions = func_get_args(); $comparators = Arrays::map($expressions, function ($expression) { return new EvaluatingComparator(Functions::extractExpression($expression)); }); return sizeof($comparators) == 1 ? $comparators[0] : new CompoundComparator($comparators); }
private static function extractParams($elements) { $params = array(); foreach ($elements as $element) { list($name, $value) = Arrays::map(explode('=', $element), Functions::trim()); $params[$name] = $value; } return $params; }
public function parse() { if (preg_match('/^findBy([a-zA-Z]\\w*)$/', $this->method, $names)) { $names = explode('And', $names[1]); $this->names = Arrays::map($names, function ($name) { return Strings::camelCaseToUnderscore($name); }); } }
public function getFieldsAsString() { $fields = array_keys($this->_attributes); $escapedFields = Arrays::map($fields, Functions::compose(Functions::append("'"), Functions::prepend("'"))); for ($index = self::FIELDS_COUNT_IN_LINE; $index < sizeof($escapedFields); $index += self::FIELDS_COUNT_IN_LINE) { $escapedFields[$index] = "\n\t\t\t" . $escapedFields[$index]; } return implode(', ', $escapedFields); }
private function createCheckParameters($parameters) { if ($parameters) { $indent = self::INDENT; $checkFunctionParameters = Arrays::map($parameters, function ($element) use($indent) { return $indent . "checkParameter({$element});"; }); return implode("\n", $checkFunctionParameters) . "\n" . $indent; } return self::INDENT; }
private function _fetchRelations($results, $joinsToStore) { $joinedRelations = Arrays::map($joinsToStore, Functions::extract()->destinationField()); foreach ($this->relationsToFetch as $relationToFetch) { if (!in_array($relationToFetch->destinationField, $joinedRelations)) { $relationFetcher = new RelationFetcher($relationToFetch->relation); $fieldTransformer = new FieldTransformer($relationToFetch->field, $relationFetcher); $fieldTransformer->transform($results); } } return $results; }
private function _parseArgs($args, $parameters) { $args = Arrays::getValue($args, 0, new stdClass()); $newArgs = array(); $parameterNames = Arrays::map($parameters, Functions::extract()->getName()); foreach ($parameterNames as $name) { if (isset($args->{$name})) { $newArgs[] = $args->{$name}; } } return $newArgs; }
private static function getParameterDeclaration(ReflectionFunctionAbstract $method) { return Joiner::on(', ')->join(Arrays::map($method->getParameters(), function (ReflectionParameter $param) { $result = ''; if ($param->getClass()) { $result .= $param->getClass()->getName() . ' '; } if ($param->isArray()) { $result .= 'array '; } if ($param->isPassedByReference()) { $result .= '&'; } $result .= '$' . $param->name; if ($param->isDefaultValueAvailable()) { $result .= " = null"; // methodHandler gets only the passed arguments so anything would work here } return $result; })); }
public function poll() { Stats::reset(); session_write_close(); $stop = Clock::now()->plusSeconds(self::TIMEOUT); while (true) { if (!$stop->isAfter(Clock::now()) || connection_aborted()) { $this->layout->renderAjax("[]"); return; } session_start(); $events = Event::loadNew(); session_write_close(); if ($events) { $this->layout->renderAjax(Json::encode(Arrays::map($events, function (Event $event) { return $event->toJsonArray(); }))); return; } Stats::reset(); usleep(100 * 1000); } }
/** * @test */ public function shouldExtractRecursivelyArrayColumn() { //given $array = array(array('id' => 123, 'name' => 'value1', 'test' => array('number' => 90)), array('id' => 123, 'name' => 'value1', 'test' => array('number' => 100))); //when $numbers = Arrays::map($array, Functions::extract()->test->number); //then Assert::thatArray($numbers)->hasSize(2)->containsOnly(90, 100); }
public static function arrayToString($calls) { return Joiner::on(', ')->join(Arrays::map($calls, Functions::toString())); }
private function _printExceptIfExists(RouteRule $rule, $table) { $except = $rule->getExcept(); if ($except) { $table->addRow(array('', '', ' <info>except:</info>', '')); Arrays::map($except, function ($except) use($table) { $table->addRow(array('', '', ' ' . $except, '')); return $except; }); } }
/** * @param Game $game * @return Hit[] */ public static function findForGame(Game $game) { $gameUserIds = Arrays::map($game->game_users, Functions::extractId()); return Hit::where(['game_user_id' => $gameUserIds])->with('game_user->user')->order('id desc')->limit(9)->fetchAll(); }
private function prepare() { $moduleFields = (array) $this->result->module_fields; $this->fields = array_values(Arrays::map($moduleFields, Functions::extract()->name)); }
public function extractCompaniesNames($companies) { return Arrays::map($companies, Functions::extractField('name')); }
/** * @WebMethod * @WebParam(param="int[] $numbers") * @WebParam(param="string $prefix") * @WebResult(param="string[] $numbersWithPrefix") */ public function appendPrefixToNumbers($numbers, $prefix) { return Arrays::map($numbers, Functions::prepend($prefix)); }
public function getParameters() { return Arrays::concat(Arrays::map($this->conditions, function (WhereClause $where) { return $where->getParameters(); })); }
/** * Calls Model::delete method for each matching object */ public function deleteEach() { $objects = $this->fetchAll(); return Arrays::map($objects, function (Model $object) { return !$object->delete(); }); }
/** Executes a native sql and returns an array of model objects created by passing every result row to the model constructor. * @param $nativeSql - database specific sql * @param array $params - bind parameters * @return Model[] */ public static function findBySql($nativeSql, $params = array()) { $meta = static::metaInstance(); $results = $meta->_modelDefinition->db->query($nativeSql, Arrays::toArray($params))->fetchAll(); return Arrays::map($results, function ($row) use($meta) { return $meta->newInstance($row); }); }
/** * @return string */ public function description() { preg_match('#@desc (.+)#', $this->doc, $groupMatches); $trimGroupMatches = Arrays::map($groupMatches, Functions::trim()); return Arrays::getValue($trimGroupMatches, 1, ''); }
public function delete() { $attributes = Arrays::map($this->whereClauses, Functions::extract()->getParams()); $queryInsert = new QueryInsert(Arrays::firstOrNull($attributes)); $id = $queryInsert->into($this->module->getModuleName()); return !is_null($id); }
private function classNameFromTableName() { $parts = explode('_', $this->tableName); $parts = $this->removeTablePrefix($parts); $parts[] = Inflector::singularize(array_pop($parts)); $parts = Arrays::map($parts, 'ucfirst'); return implode('', $parts); }
private function wrapAsNotices($messages, $url) { $array = Arrays::toArray($messages); return Arrays::map($array, function ($msg) use($url) { return new Notice($msg, $url); }); }
public function map($function) { $this->_array = Arrays::map($this->_array, $function); return $this; }
public function send($headers) { Arrays::map($headers, function ($header) { header($header); }); }
public function onMethod($method) { return new ArrayAssert(Arrays::map($this->actual, function ($element) use($method) { return $element->{$method}(); })); }
/** * @return Node[] */ public function getNoHeaderParametersNodes() { return Arrays::map($this->getNoHeaderParameters(), Functions::extractExpression('getNode()')); }