map() публичный статический метод

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 )
public static map ( array $elements, callable $function ) : array
$elements array
$function callable
Результат array
Пример #1
0
 private function _prepareParameters($uri)
 {
     preg_match_all('#:(\\w+)#', $uri, $matches);
     $parameters = Arrays::getValue($matches, 1, array());
     return Arrays::map($parameters, function ($parameter) {
         return '$' . $parameter;
     });
 }
Пример #2
0
 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();
 }
Пример #4
0
 /**
  * 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);
 }
Пример #5
0
 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);
         });
     }
 }
Пример #7
0
 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);
 }
Пример #8
0
 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;
 }
Пример #9
0
 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;
 }
Пример #10
0
 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;
 }
Пример #11
0
 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;
     }));
 }
Пример #12
0
 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);
     }
 }
Пример #13
0
 /**
  * @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);
 }
Пример #14
0
 public static function arrayToString($calls)
 {
     return Joiner::on(', ')->join(Arrays::map($calls, Functions::toString()));
 }
Пример #15
0
 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;
         });
     }
 }
Пример #16
0
 /**
  * @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();
 }
Пример #17
0
 private function prepare()
 {
     $moduleFields = (array) $this->result->module_fields;
     $this->fields = array_values(Arrays::map($moduleFields, Functions::extract()->name));
 }
Пример #18
0
 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));
 }
Пример #20
0
 public function getParameters()
 {
     return Arrays::concat(Arrays::map($this->conditions, function (WhereClause $where) {
         return $where->getParameters();
     }));
 }
Пример #21
0
 /**
  * Calls Model::delete method for each matching object
  */
 public function deleteEach()
 {
     $objects = $this->fetchAll();
     return Arrays::map($objects, function (Model $object) {
         return !$object->delete();
     });
 }
Пример #22
0
 /** 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);
     });
 }
Пример #23
0
 /**
  * @return string
  */
 public function description()
 {
     preg_match('#@desc (.+)#', $this->doc, $groupMatches);
     $trimGroupMatches = Arrays::map($groupMatches, Functions::trim());
     return Arrays::getValue($trimGroupMatches, 1, '');
 }
Пример #24
0
 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);
 }
Пример #25
0
 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);
 }
Пример #26
0
 private function wrapAsNotices($messages, $url)
 {
     $array = Arrays::toArray($messages);
     return Arrays::map($array, function ($msg) use($url) {
         return new Notice($msg, $url);
     });
 }
Пример #27
0
 public function map($function)
 {
     $this->_array = Arrays::map($this->_array, $function);
     return $this;
 }
Пример #28
0
 public function send($headers)
 {
     Arrays::map($headers, function ($header) {
         header($header);
     });
 }
Пример #29
0
 public function onMethod($method)
 {
     return new ArrayAssert(Arrays::map($this->actual, function ($element) use($method) {
         return $element->{$method}();
     }));
 }
Пример #30
0
 /**
  * @return Node[]
  */
 public function getNoHeaderParametersNodes()
 {
     return Arrays::map($this->getNoHeaderParameters(), Functions::extractExpression('getNode()'));
 }