Skip to content

Commit

Permalink
Merge branch 'developer'
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Jun 9, 2016
2 parents 6bd9950 + 06c83e4 commit 3db1f9f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 49 deletions.
109 changes: 64 additions & 45 deletions src/Query/Sql/Select.php
Expand Up @@ -53,7 +53,7 @@ class Select extends SelectBase
*
* @var false|string
*/
protected $groupResult = false;
protected $groupResults = false;

/**
* Forward a value as key
Expand Down Expand Up @@ -381,14 +381,14 @@ public function outerJoin($table, $localKey, $operator = null, $referenceKey = n
* @param string|bool $key
* @return self
*/
public function forward_key($key = true)
public function forwardKey($key = true)
{
if ($key === false) {
$this->forward_key = false;
$this->forwardKey = false;
} elseif ($key === true) {
$this->forward_key = \ClanCats::$config->get('database.default_primary_key', 'id');
$this->forwardKey = \ClanCats::$config->get('database.default_primary_key', 'id');
} else {
$this->forward_key = $key;
$this->forwardKey = $key;
}

return $this;
Expand All @@ -413,66 +413,69 @@ public function forward_key($key = true)
* @param string|bool $key
* @return self
*/
public function group_result($key)
public function groupResults($key)
{
if ($key === false) {
$this->group_result = false;
$this->groupResults = false;
} else {
$this->group_result = $key;
$this->groupResults = $key;
}

return $this;
}

/**
* Executes the 'executeResultFetcher' callback and handles the results
*
* @param string $handler
* @return mixed
*/
public function run()
public function get()
{
// run the callbacks to retirve the results
// run the callbacks to retirve the results
$results = $this->executeResultFetcher();

// we always exprect an array here!
if (!is_array($results))
{
$results = array();
$results = array();
}

// In case we should forward a key means using a value
// from every result as array key.
// if ($this->forward_key !== false) {
// $raw_results = $results;
// $results = array();

// if (in_array('assoc', $this->fetch_arguments)) {
// foreach ($raw_results as $result) {
// $results[$result[$this->forward_key]] = $result;
// }
// } else {
// foreach ($raw_results as $result) {
// $results[$result->{$this->forward_key}] = $result;
// }
// }
// }

// // Group the results by a value
// if ($this->group_result !== false) {
// $raw_results = $results;
// $results = array();

// if (in_array('assoc', $this->fetch_arguments)) {
// foreach ($raw_results as $key => $result) {
// $results[$result[$this->group_result]][$key] = $result;
// }
// } else {
// foreach ($raw_results as $key => $result) {
// $results[$result->{$this->group_result}][$key] = $result;
// }
// }
// }
if ($this->forwardKey !== false && is_string($this->forwardKey))
{
$rawResults = $results;
$results = array();

// check if the collection is beeing fetched
// as an associated array
if (!is_array(reset($rawResults)))
{
throw new Exception('Cannot forward key, the result is no associated array.');
}

foreach ($rawResults as $result)
{
$results[$result[$this->forwardKey]] = $result;
}
}

// Group the resuls by a items value
if ($this->groupResults !== false && is_string($this->groupResults))
{
$rawResults = $results;
$results = array();

// check if the collection is beeing fetched
// as an associated array
if (!is_array(reset($rawResults)))
{
throw new Exception('Cannot forward key, the result is no associated array.');
}

foreach ($rawResults as $key => $result)
{
$results[$result[$this->groupResults]][$key] = $result;
}
}

// when the limit is specified to exactly one result we
// return directly that one result instead of the entire array
Expand All @@ -484,14 +487,30 @@ public function run()
return $results;
}

/**
* Executes the 'executeResultFetcher' callback and handles the results
*
* @param string $handler
* @return mixed
*/
public function run()
{
// run is basically ported from CCF, laravels `get` just feels
// much better so lets move on...
trigger_error('The `run` method is deprecated, `get` method instead.', E_USER_DEPRECATED);

// run the get method
return $this->get();
}

/**
* Get one result sets limit to 1 and executes
*
* @return mixed
*/
public function one()
{
return $this->limit(0, 1)->run();
return $this->limit(0, 1)->get();
}

/**
Expand Down
65 changes: 61 additions & 4 deletions tests/Query/Sql/Select.php
Expand Up @@ -214,18 +214,75 @@ public function testRun()

// simple
$select = $this->createQuery($data);
$this->assertEquals($data, $select->run());
$this->assertEquals($data, $select->get());

// just one
$select->limit(1);
$this->assertEquals(reset($data), $select->run());
$this->assertEquals(reset($data), $select->get());

// invalid data array
$select = $this->createQuery('nope');
$this->assertEquals(array(), $select->run());
$this->assertEquals(array(), $select->get());

// no item found
$select = $this->createQuery(array())->limit(1);
$this->assertEquals(false, $select->run());
$this->assertEquals(false, $select->get());
}

/**
* Select::forwardKey
*/
public function testForwardKey()
{
$data = array(
array('id' => 1, 'name' => 'Mario', 'age' => 23),
array('id' => 2, 'name' => 'Johnna', 'age' => 20),
);

$select = $this->createQuery($data);

// control
$this->assertEquals($data, $select->get());

// now group by age
$select->forwardKey('name');
$result = $select->get();

$this->assertEquals(23, $result['Mario']['age']);
$this->assertEquals(20, $result['Johnna']['age']);
}

/**
* Select::groupResults
*/
public function testGroupResults()
{
$data = array(
array('id' => 1, 'name' => 'Mario', 'age' => 23),
array('id' => 2, 'name' => 'Johnna', 'age' => 20),
array('id' => 3, 'name' => 'Tarek', 'age' => 22),
array('id' => 4, 'name' => 'Michel', 'age' => 22),
array('id' => 5, 'name' => 'Johnna', 'age' => 22),
);

$select = $this->createQuery($data);

// control
$this->assertEquals($data, $select->get());

// now group by age
$select->groupResults('age');
$grouped = $select->get();

$this->assertCount(1, $grouped[23]);
$this->assertCount(1, $grouped[20]);
$this->assertCount(3, $grouped[22]);

// now also forward the keys
$select->forwardKey('id');
$grouped = $select->get();

$this->assertEquals('Johnna', $grouped[22][5]['name']);
$this->assertEquals('Johnna', $grouped[20][2]['name']);
}
}

0 comments on commit 3db1f9f

Please sign in to comment.