示例#1
0
 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();
 }
示例#2
0
 /**
  * @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;
 }
示例#3
0
 private function _map($closure)
 {
     if ($this->isPresent()) {
         return Optional::fromNullable(Functions::call($closure, $this->object));
     }
     return Optional::absent();
 }
示例#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;
 }
示例#6
0
 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);
     }
 }
示例#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
 /**
  * @test
  */
 public function shouldNegate()
 {
     //given
     $function = FluentFunctions::startsWith("start")->negate();
     //when
     $result = Functions::call($function, "starts with prefix");
     //then
     $this->assertFalse($result);
 }
示例#9
0
 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 . ')';
 }
示例#10
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;
 }
 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;
 }
示例#12
0
 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;
 }
示例#13
0
 /**
  * @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));
 }
示例#16
0
 /**
  * @return string
  */
 public function description()
 {
     preg_match('#@desc (.+)#', $this->doc, $groupMatches);
     $trimGroupMatches = Arrays::map($groupMatches, Functions::trim());
     return Arrays::getValue($trimGroupMatches, 1, '');
 }
示例#17
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();
 }
示例#18
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);
 }
示例#19
0
 /**
  * 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));
 }
示例#21
0
 public function extractCompaniesNames($companies)
 {
     return Arrays::map($companies, Functions::extractField('name'));
 }
示例#22
0
 public function onProperty($property)
 {
     return new ArrayAssert(Arrays::map($this->actual, Functions::extractExpression($property, true)));
 }
示例#23
0
 /**
  * @return Node[]
  */
 public function getNoHeaderParametersNodes()
 {
     return Arrays::map($this->getNoHeaderParameters(), Functions::extractExpression('getNode()'));
 }
示例#24
0
 /**
  * @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');
 }
示例#25
0
 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()));
 }
示例#26
0
 /**
  * @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
 }
示例#27
0
 /**
  * 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)));
 }
示例#28
0
文件: Model.php 项目: letsdrink/ouzo
 /**
  * Returns model object as a nicely formatted string.
  */
 public function inspect()
 {
     return get_called_class() . Objects::toString(Arrays::filter($this->_attributes, Functions::notNull()));
 }
示例#29
0
 /**
  * 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));
 }
示例#30
0
 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)))));
     });
 }