diff --git a/src/BaseQuery.php b/src/BaseQuery.php index cb9abcc..06f2344 100644 --- a/src/BaseQuery.php +++ b/src/BaseQuery.php @@ -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 * @@ -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; } @@ -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 * diff --git a/tests/BaseQueryTest.php b/tests/BaseQueryTest.php new file mode 100644 index 0000000..0a84545 --- /dev/null +++ b/tests/BaseQueryTest.php @@ -0,0 +1,41 @@ +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')); + } +} \ No newline at end of file