Source of file QueryBuilder.php
Size: 9,048 Bytes - Last Modified: 2021-12-24T06:51:20+00:00
/var/www/docs.ssmods.com/process/src/src/QueryBuilder.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 | <?php namespace Heyday\QueryBuilder; use Heyday\QueryBuilder\Interfaces\QueryBuilderInterface; use Heyday\QueryBuilder\Interfaces\QueryModifierInterface; /** * @package Heyday\QueryBuilder */ class QueryBuilder implements QueryBuilderInterface { /** @var \Heyday\QueryBuilder\Interfaces\QueryModifierInterface[] */ protected $queryModifiers = []; /** @var array */ protected $data = []; /** @var bool|\ArrayList */ protected $listCache = false; /** @var bool|\SQLQuery */ protected $queryCache = false; /** @var null */ protected $dataClass; /** @var string */ protected $stage; /** * @param string|void $dataClass * @param array $queryModifiers * @param array $modifierData */ public function __construct( $dataClass = null, array $queryModifiers = [], array $modifierData = [] ) { $this->dataClass = $dataClass; $this->setQueryModifiers($queryModifiers); $this->setData($modifierData); } /** * @param string $stage * @return \Heyday\QueryBuilder\QueryBuilder */ public function setStage($stage) { $this->stage = $stage; return $this; } /** * @return string */ public function getStage() { return $this->stage; } /** * */ protected function invalidateCache() { $this->listCache = false; $this->queryCache = false; } /** * @return \SQLQuery */ public function getQuery() { if (!$this->queryCache) { if ($this->dataClass) { $dataQuery = new \DataQuery($this->dataClass); if ($this->stage) { $dataQuery->setQueryParam('Versioned.mode', 'stage'); $dataQuery->setQueryParam('Versioned.stage', $this->stage); } $this->queryCache = $dataQuery->getFinalisedQuery(); } else { $this->queryCache = new \SQLQuery(); } if (is_array($this->queryModifiers)) { foreach ($this->queryModifiers as $queryModifier) { if ($queryModifier instanceof QueryModifierInterface) { $queryModifier->modify($this->queryCache, $this->data, $this); } elseif (is_callable($queryModifier)) { $queryModifier($this->queryCache, $this->data, $this); } } } } return $this->queryCache; } /** * @return \ArrayList */ public function getList() { if ($this->listCache === false) { $rows = $this->getQuery()->execute(); $results = []; foreach ($rows as $row) { if ($do = createDataObject($this->dataClass, $row)) { $results[] = $do; } } $this->listCache = new \ArrayList($results); } return $this->listCache; } /** * @param callable|QueryModifierInterface $queryModifier * @return \Heyday\QueryBuilder\QueryBuilder * @throws \Exception */ public function addQueryModifier($queryModifier) { if ($queryModifier instanceof QueryModifierInterface || is_callable($queryModifier)) { $this->queryModifiers[] = $queryModifier; $this->invalidateCache(); } else { throw new \Exception( 'a QueryModifier must implement the Heyday\ListQueryOrganiser\Interfaces\QueryModifierInterface interface' ); } return $this; } /** * @param array $queryModifiers * @return \Heyday\QueryBuilder\QueryBuilder */ public function setQueryModifiers(array $queryModifiers) { foreach ($queryModifiers as $queryModifier) { $this->addQueryModifier($queryModifier); } return $this; } /** * @return array of query modifiers */ public function getQueryModifiers() { return $this->queryModifiers; } /** * @param array $data * @return \Heyday\QueryBuilder\QueryBuilder */ public function setData(array $data) { $this->data = \Convert::raw2sql($data); $this->invalidateCache(); return $this; } /** SS_List interface implementation */ /** * (PHP 5 >= 5.0.0)<br/> * Retrieve an external iterator * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return \Traversable An instance of an object implementing <b>Iterator</b> or * <b>Traversable</b> */ public function getIterator() { return $this->getList()->getIterator(); } /** * (PHP 5 >= 5.0.0)<br/> * Whether a offset exists * @link http://php.net/manual/en/arrayaccess.offsetexists.php * @param mixed $offset <p> * An offset to check for. * </p> * @return boolean true on success or false on failure. * </p> * <p> * The return value will be casted to boolean if non-boolean was returned. */ public function offsetExists($offset) { return $this->getList()->offsetExists($offset); } /** * (PHP 5 >= 5.0.0)<br/> * Offset to retrieve * @link http://php.net/manual/en/arrayaccess.offsetget.php * @param mixed $offset <p> * The offset to retrieve. * </p> * @return mixed Can return all value types. */ public function offsetGet($offset) { return $this->getList()->offsetGet($offset); } /** * (PHP 5 >= 5.0.0)<br/> * Offset to set * @link http://php.net/manual/en/arrayaccess.offsetset.php * @param mixed $offset <p> * The offset to assign the value to. * </p> * @param mixed $value <p> * The value to set. * </p> * @return void */ public function offsetSet($offset, $value) { $this->getList()->offsetSet($offset, $value); } /** * (PHP 5 >= 5.0.0)<br/> * Offset to unset * @link http://php.net/manual/en/arrayaccess.offsetunset.php * @param mixed $offset <p> * The offset to unset. * </p> * @return void */ public function offsetUnset($offset) { $this->getList()->offsetUnset($offset); } /** * Returns all the items in the list in an array. * * @return array */ public function toArray() { return $this->getList()->toArray(); } /** * Returns the contents of the list as an array of maps. * * @return array */ public function toNestedArray() { return $this->getList()->toNestedArray(); } /** * Adds an item to the list, making no guarantees about where it will * appear. * * @param mixed $item */ public function add($item) { $this->getList()->add($item); } /** * Removes an item from the list. * * @param mixed $item */ public function remove($item) { $this->getList()->remove($item); } /** * Returns the first item in the list. * * @return mixed */ public function first() { return $this->getList()->first(); } /** * Returns the last item in the list. * * @return mixed */ public function last() { return $this->getList()->last(); } /** * Returns a map of a key field to a value field of all the items in the * list. * * @param string $keyfield * @param string $titlefield * @return array */ public function map($keyfield = 'ID', $titlefield = 'Title') { return $this->getList()->map($keyfield, $titlefield); } /** * Returns the first item in the list where the key field is equal to the * value. * * @param string $key * @param mixed $value * @return mixed */ public function find($key, $value) { return $this->getList()->find($key, $value); } /** * Returns an array of a single field value for all items in the list. * * @param string $colName * @return array */ public function column($colName = "ID") { return $this->getList()->column($colName); } /** * Walks the list using the specified callback * * @param callable $callback * @return mixed */ public function each($callback) { return $this->getList()->each($callback); } /** * (PHP 5 >= 5.1.0)<br/> * Count elements of an object * @link http://php.net/manual/en/countable.count.php * @return int The custom count as an integer. * </p> * <p> * The return value is cast to an integer. */ public function count() { return $this->getList()->count(); } } |