Source of file CompositeBuilder.php
Size: 1,862 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Db/Query/CompositeBuilder.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Db\Query; /** * Class Expression Builder * * @package Bluz\Db\Query */ class CompositeBuilder implements \Countable { /** * @var string type AND|OR */ private $type; /** * @var array parts of the composite expression */ private $parts = []; /** * Constructor * * @param array $parts parts of the composite expression * @param string $type AND|OR */ public function __construct(array $parts = [], $type = 'AND') { $type = strtoupper($type); $this->type = $type === 'OR' ? 'OR' : 'AND'; $this->add($parts); } /** * Adds an expression to composite expression. * * @param mixed $parts * * @return CompositeBuilder */ public function add($parts) { foreach ((array)$parts as $part) { if (!empty($part) || ($part instanceof self && $part->count() > 0)) { $this->parts[] = $part; } } return $this; } /** * Return type of this composite * * @return string */ public function getType() { return $this->type; } /** * Retrieves the amount of expressions on composite expression. * * @return integer */ public function count() { return count($this->parts); } /** * Retrieve the string representation of this composite expression. * * @return string */ public function __toString() { if (count($this->parts) === 1) { return (string)$this->parts[0]; } return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; } } |