public static function createSelectColumnsWithAliases($prefix, $columns, $alias) { return FluentArray::from($columns)->toMap(function ($field) use($prefix) { return "{$prefix}{$field}"; }, function ($field) use($alias) { return "{$alias}.{$field}"; })->toArray(); }
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 static function buildUsingQuery($usingClauses, $glue, $table, $alias) { $elements = FluentArray::from($usingClauses)->map('\\Ouzo\\Db\\Dialect\\DialectUtil::buildUsingQueryPart')->toArray(); if ($usingClauses && $table) { $tableElement = $table . ($alias ? " AS {$alias}" : ""); $elements = array_merge(array($tableElement), $elements); } return implode($glue, $elements); }
public function convert($results) { $aliasToOffset = $this->_createAliasToOffsetMap(); $joinsToStore = FluentArray::from($this->joinedModels)->filter(Functions::extract()->storeField())->uniqueBy(Functions::extract()->destinationField())->toArray(); $models = array(); foreach ($results as $row) { $models[] = $this->convertRowToModel($row, $aliasToOffset, $joinsToStore); } return $this->_fetchRelations($models, $joinsToStore); }
public function transform(&$results) { $localKeyName = $this->_relation->getLocalKey(); $localKeys = FluentArray::from($results)->map(Functions::extractField($localKeyName))->filterNotBlank()->unique()->toArray(); $relationObjectsById = $localKeys ? $this->_loadRelationObjectsIndexedById($localKeys) : array(); foreach ($results as $result) { $values = $this->_findRelationObject($relationObjectsById, $result->{$localKeyName}); $destinationField = $this->_relation->getName(); $result->{$destinationField} = $this->_relation->extractValue($values); } }
/** * @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 }
public function setParameters($uri) { $ruleUri = explode('/', $this->getUri()); $requestUri = explode('/', $uri); $filterParameters = FluentArray::from($ruleUri)->filter(function ($parameter) { return preg_match('#:\\w+#', $parameter); })->map(function ($parameter) { return str_replace(':', '', $parameter); })->toArray(); $filterValues = array_intersect_key($requestUri, $filterParameters); $this->parameters = Arrays::combine($filterParameters, $filterValues); }
/** * @test */ public function shouldSortElements() { //given $array = array(4, 1, 3, 2); //when $result = FluentArray::from($array)->sort(Comparator::natural())->toArray(); //then $this->assertEquals(array(1, 2, 3, 4), $result); }