. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU Affero General Public License version 3. * * In accordance with Section 7(b) of the GNU Affero General Public License version 3, * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ namespace Espo\ORM\Query\Part\Where; use Espo\ORM\Query\Part\WhereItem; class OrGroupBuilder { /** @var array */ private array $raw = []; public function build(): OrGroup { return OrGroup::fromRaw($this->raw); } public function add(WhereItem $item): self { $key = $item->getRawKey(); $value = $item->getRawValue(); if ($item instanceof AndGroup) { $this->raw = self::normalizeRaw($this->raw); $this->raw[] = $value; return $this; } if (count($this->raw) === 0) { $this->raw[$key] = $value; return $this; } $this->raw = self::normalizeRaw($this->raw); $this->raw[] = [$key => $value]; return $this; } /** * Merge with another OrGroup. */ public function merge(OrGroup $orGroup): self { $this->raw = array_merge( self::normalizeRaw($this->raw), self::normalizeRaw($orGroup->getRawValue()) ); return $this; } /** * @param array $raw * @return array */ private static function normalizeRaw(array $raw): array { if (count($raw) === 1 && array_keys($raw)[0] !== 0) { return [$raw]; } return $raw; } }