Skip to content

Commit

Permalink
Added query flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Feb 23, 2018
1 parent 9826340 commit bfa974d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class BaseQuery
*/
protected $macros = array();

/**
* Query flags
* These allow you to store data inside the query object.
* This data has no influence on the generated query string or parameters directly.
* But allow you to use the query a state mashine.
*
* @var array
*/
protected $flags = array();

/**
* The callback where we fetch the results from
*
Expand Down Expand Up @@ -48,6 +58,7 @@ final public function __construct(BaseQuery $parent = null)
protected function inheritFromParent(BaseQuery $parent)
{
$this->macros = $parent->macros;
$this->flags = $parent->flags;
$this->resultFetcher = $parent->resultFetcher;
}

Expand All @@ -62,6 +73,34 @@ public function setResultFetcher($resultFetcher = null)
$this->resultFetcher = $resultFetcher;
}

/**
* Set a flag on the query object
*
* @param string $key
* @param mixed $value
* @return void
*/
final public function setFlag($key, $value)
{
$this->flags[$key] = $value;
}

/**
* Gets a flag from the query object
*
* @param string $key
* @param mixed $default
* @return void
*/
final public function getFlag($key, $default = null)
{
if (!isset($this->flags[$key])) {
return $default;
}

return $this->flags[$key];
}

/**
* Register a macro on the current query object
*
Expand Down
41 changes: 41 additions & 0 deletions tests/BaseQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php namespace ClanCats\Hydrahon\Test;
/**
* Hydrahon base query test
**
*
* @package Hydrahon
* @copyright Mario Döring
*
* @group Hydrahon
* @group Hydrahon_BaseQuery
*/

use ClanCats\Hydrahon\BaseQuery;
use ClanCats\Hydrahon\Query\Sql\Table;
use ClanCats\Hydrahon\Query\Sql\Select;

class BaseQueryTest extends \PHPUnit_Framework_TestCase
{
public function testFlags()
{
$query = new BaseQuery;

$this->assertNull($query->getFlag('foo'));
$this->assertEquals('bar', $query->getFlag('foo', 'bar'));

$query->setFlag('number', 42);
$this->assertEquals(42, $query->getFlag('number'));
$this->assertEquals(42, $query->getFlag('number', 'nope'));
}

public function testFlagInheritence()
{
$query = new Table;
$query->setFlag('foo', 'bar');

$select = $query->select();
$this->assertInstanceOf(Select::class, $select);

$this->assertEquals('bar', $select->getFlag('foo'));
}
}

0 comments on commit bfa974d

Please sign in to comment.