public function init() { $uri = new Uri(); $router = new Router($uri); $routeRule = $router->findRoute(); $this->currentController = $routeRule->getController(); $this->requestContext->setCurrentController($this->currentController); $this->currentAction = $routeRule->isActionRequired() ? $routeRule->getAction() : $uri->getAction(); $this->sessionInitializer->startSession(); $this->currentControllerObject = $this->controllerFactory->createController($routeRule); $this->requestContext->setCurrentControllerObject($this->currentControllerObject); $this->_logRequest(); $afterInitCallback = Config::getValue('callback', 'afterControllerInit'); if ($afterInitCallback) { Functions::call($afterInitCallback, array()); } try { ob_start(); $this->_invokeControllerMethods(); } catch (Exception $e) { ob_end_clean(); throw $e; } ob_end_flush(); }
/** * @param mixed ... * @return $this */ public function thenThrow($exception) { foreach (func_get_args() as $exception) { $this->mock->_stubbed_calls[] = new CallStub($this->methodCall, Functions::throwException($exception)); } return $this; }
private function _map($closure) { if ($this->isPresent()) { return Optional::fromNullable(Functions::call($closure, $this->object)); } return Optional::absent(); }
/** * 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 transform(&$results) { if ($this->field) { $fields = FluentArray::from($results)->map(Functions::extractExpression($this->field))->flatten()->filterNotBlank()->toArray(); $this->transformer->transform($fields); } else { $this->transformer->transform($results); } }
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); }
/** * @test */ public function shouldNegate() { //given $function = FluentFunctions::startsWith("start")->negate(); //when $result = Functions::call($function, "starts with prefix"); //then $this->assertFalse($result); }
private static function _buildWhereKeyIn($column, array $array) { $useRestrictions = Arrays::any($array, Functions::isInstanceOf('\\Ouzo\\Restriction\\Restriction')); if ($useRestrictions) { return DialectUtil::joinClauses($array, 'OR', function (Restriction $restriction) use($column) { return $restriction->toSql($column); }); } $in = implode(', ', array_fill(0, count($array), '?')); return $column . ' IN (' . $in . ')'; }
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; }
public function getClosedByRoundForCricket() { $hits = Hit::join('game_user->user')->where(['game_user_id' => $this->id])->order('hits.id asc')->fetchAll(); $byRound = Arrays::groupBy($hits, Functions::extract()->round); $closedInRound = []; $closed = [15 => 0, 16 => 0, 17 => 0, 18 => 0, 19 => 0, 20 => 0, 25 => 0]; foreach ($byRound as $round => $hits) { $closedInCurrentRound = 0; foreach ($hits as $hit) { if (isset($closed[$hit->field])) { $closedField = min($hit->multiplier, 3 - $closed[$hit->field]); $closedInCurrentRound += $closedField; $closed[$hit->field] += $closedField; } } $closedInRound[$round] = $closedInCurrentRound; } return $closedInRound; }
/** * @param $selector * @throws Exception * @return FluentArray */ public function uniqueBy($selector) { return $this->toMap(Functions::extractExpression($selector))->values(); }
public static function prepare(array $whereClauses = array()) { $where = Joiner::on(' AND ')->mapValues(Functions::extract()->getWhere())->join($whereClauses); return new self($where); }
private function prepare() { $moduleFields = (array) $this->result->module_fields; $this->fields = array_values(Arrays::map($moduleFields, Functions::extract()->name)); }
/** * @return string */ public function description() { preg_match('#@desc (.+)#', $this->doc, $groupMatches); $trimGroupMatches = Arrays::map($groupMatches, Functions::trim()); return Arrays::getValue($trimGroupMatches, 1, ''); }
/** * @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(); }
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); }
/** * Returns the composition of two functions. * composition is defined as the function h such that h(a) == A(B(a)) for each a. * @param $functionA * @param $functionB * @return callable */ public static function compose($functionA, $functionB) { return function ($input) use($functionA, $functionB) { return Functions::call($functionA, Functions::call($functionB, $input)); }; }
/** * @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 extractCompaniesNames($companies) { return Arrays::map($companies, Functions::extractField('name')); }
public function onProperty($property) { return new ArrayAssert(Arrays::map($this->actual, Functions::extractExpression($property, true))); }
/** * @return Node[] */ public function getNoHeaderParametersNodes() { return Arrays::map($this->getNoHeaderParameters(), Functions::extractExpression('getNode()')); }
/** * @test */ public function shouldExtractArrayValue() { //given $object = array('key' => 'value'); //$function = Functions::extract()['key']; only i php 5.4 $function = Functions::extract()->offsetGet('key'); //when $result = Functions::call($function, $object); //then Assert::thatString($result)->isEqualTo('value'); }
private function _loadRelationObjectsIndexedById($localKeys) { $relationObject = $this->_relation->getRelationModelObject(); $relationObjects = $relationObject::where(array($this->_relation->getForeignKey() => $localKeys))->where($this->_relation->getCondition())->order($this->_relation->getOrder())->fetchAll(); return Arrays::groupBy($relationObjects, Functions::extractField($this->_relation->getForeignKey())); }
/** * @test */ public function shouldFetchRelationThroughOneToManyRelation() { //given $phones = Category::create(array('name' => 'phones')); $sony = Manufacturer::create(array('name' => 'sony')); Product::create(array('name' => 'best ever sony', 'id_category' => $phones->id, 'id_manufacturer' => $sony->id)); $tablets = Category::create(array('name' => 'tablets')); $samsung = Manufacturer::create(array('name' => 'samsung')); Product::create(array('name' => 'best ever samsung', 'id_category' => $tablets->id, 'id_manufacturer' => $samsung->id)); //when $categories = Category::where()->with('products->manufacturer')->fetchAll(); Stats::reset(); //then Assert::thatArray(FluentArray::from($categories)->map(Functions::extract()->products)->flatten()->map(Functions::extract()->manufacturer->name)->toArray())->containsOnly('sony', 'samsung'); $this->assertEquals(0, Stats::getNumberOfQueries()); //no lazily loaded relations }
/** * Removes duplicate values from an array. It uses the given expression to extract value that is compared. * * Example: * <code> * $a = new stdClass(); * $a->name = 'bob'; * * $b = new stdClass(); * $b->name = 'bob'; * * $array = [$a, $b]; * $result = Arrays::uniqueBy($array, 'name'); * </code> * Result: * <code> * Array * ( * [0] => $b * ) * </code> * * @param array $elements * @param $selector * @return array * @throws Exception */ public static function uniqueBy(array $elements, $selector) { return array_values(self::toMap($elements, Functions::extractExpression($selector))); }
/** * Returns model object as a nicely formatted string. */ public function inspect() { return get_called_class() . Objects::toString(Arrays::filter($this->_attributes, Functions::notNull())); }
/** * Returns comparator which compares objects by using values computed using given expression. * Expression 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 $expression * @return callable */ public static function compareBy($expression) { return new EvaluatingComparator(Functions::extractExpression($expression)); }
public function getRequestHeaders() { $headers = Arrays::filterByKeys($_SERVER, Functions::startsWith('HTTP_')); return Arrays::mapKeys($headers, function ($key) { return str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5))))); }); }