| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\System\EAV; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class Query |
| 18: | { |
| 19: | protected $sType = null; |
| 20: | |
| 21: | protected $aViewAttributes = []; |
| 22: | |
| 23: | protected $sCustomViewSql = ''; |
| 24: | |
| 25: | protected $aWhere = []; |
| 26: | |
| 27: | protected $aIdOrUuids = []; |
| 28: | |
| 29: | protected $iLimit = 0; |
| 30: | |
| 31: | protected $iOffset = 0; |
| 32: | |
| 33: | protected $mOrderAttributes = []; |
| 34: | |
| 35: | protected $iSortOrder = \Aurora\System\Enums\SortOrder::ASC; |
| 36: | |
| 37: | protected $bCount = false; |
| 38: | |
| 39: | protected $bOnlyUUIDs = false; |
| 40: | |
| 41: | protected $bOne = false; |
| 42: | |
| 43: | protected $bAsArray = false; |
| 44: | |
| 45: | public function __construct($sType = null) |
| 46: | { |
| 47: | $this->sType = $sType; |
| 48: | } |
| 49: | |
| 50: | public function select($aViewAttributes = []) |
| 51: | { |
| 52: | $this->aViewAttributes = $aViewAttributes; |
| 53: | |
| 54: | return $this; |
| 55: | } |
| 56: | |
| 57: | public function customSelect($sSql) |
| 58: | { |
| 59: | $this->sCustomViewSql = $sSql; |
| 60: | |
| 61: | return $this; |
| 62: | } |
| 63: | |
| 64: | |
| 65: | public function whereType($sType) |
| 66: | { |
| 67: | $this->sType = $sType; |
| 68: | |
| 69: | return $this; |
| 70: | } |
| 71: | |
| 72: | public function where($aWhere) |
| 73: | { |
| 74: | $this->aWhere = $aWhere; |
| 75: | |
| 76: | return $this; |
| 77: | } |
| 78: | |
| 79: | public function whereIdOrUuidIn($aIdOrUuids) |
| 80: | { |
| 81: | $this->aIdOrUuids = $aIdOrUuids; |
| 82: | |
| 83: | return $this; |
| 84: | } |
| 85: | |
| 86: | public function limit($iLimit) |
| 87: | { |
| 88: | $this->iLimit = $iLimit; |
| 89: | |
| 90: | return $this; |
| 91: | } |
| 92: | |
| 93: | public function offset($iOffset) |
| 94: | { |
| 95: | $this->iOffset = $iOffset; |
| 96: | |
| 97: | return $this; |
| 98: | } |
| 99: | |
| 100: | public function orderBy($mOrderAttributes) |
| 101: | { |
| 102: | $this->mOrderAttributes = $mOrderAttributes; |
| 103: | |
| 104: | return $this; |
| 105: | } |
| 106: | |
| 107: | public function sortOrder($iSortOrder) |
| 108: | { |
| 109: | $this->iSortOrder = $iSortOrder; |
| 110: | |
| 111: | return $this; |
| 112: | } |
| 113: | |
| 114: | public function asc() |
| 115: | { |
| 116: | $this->iSortOrder = \Aurora\System\Enums\SortOrder::ASC; |
| 117: | |
| 118: | return $this; |
| 119: | } |
| 120: | |
| 121: | public function desc() |
| 122: | { |
| 123: | $this->iSortOrder = \Aurora\System\Enums\SortOrder::DESC; |
| 124: | |
| 125: | return $this; |
| 126: | } |
| 127: | |
| 128: | public function count() |
| 129: | { |
| 130: | $this->bCount = true; |
| 131: | |
| 132: | return $this; |
| 133: | } |
| 134: | |
| 135: | public function onlyUUIDs() |
| 136: | { |
| 137: | $this->bOnlyUUIDs = true; |
| 138: | |
| 139: | return $this; |
| 140: | } |
| 141: | |
| 142: | public function one() |
| 143: | { |
| 144: | $this->bOne = true; |
| 145: | |
| 146: | return $this; |
| 147: | } |
| 148: | |
| 149: | public function asArray() |
| 150: | { |
| 151: | $this->bAsArray = true; |
| 152: | |
| 153: | return $this; |
| 154: | } |
| 155: | |
| 156: | public function exec() |
| 157: | { |
| 158: | $mResult = false; |
| 159: | |
| 160: | if (!$this->bCount) { |
| 161: | if ($this->bOnlyUUIDs) { |
| 162: | $mResult = \Aurora\System\Managers\Eav::getInstance()->getEntitiesUids( |
| 163: | $this->sType, |
| 164: | $this->iOffset, |
| 165: | $this->iLimit, |
| 166: | $this->aWhere, |
| 167: | $this->mOrderAttributes, |
| 168: | $this->iSortOrder, |
| 169: | $this->sCustomViewSql |
| 170: | ); |
| 171: | } else { |
| 172: | if (!$this->bAsArray) { |
| 173: | $mResult = \Aurora\System\Managers\Eav::getInstance()->getEntities( |
| 174: | $this->sType, |
| 175: | $this->aViewAttributes, |
| 176: | $this->iOffset, |
| 177: | $this->iLimit, |
| 178: | $this->aWhere, |
| 179: | $this->mOrderAttributes, |
| 180: | $this->iSortOrder, |
| 181: | $this->aIdOrUuids, |
| 182: | $this->sCustomViewSql |
| 183: | ); |
| 184: | } else { |
| 185: | $mResult = \Aurora\System\Managers\Eav::getInstance()->getEntitiesAsArray( |
| 186: | $this->sType, |
| 187: | $this->aViewAttributes, |
| 188: | $this->iOffset, |
| 189: | $this->iLimit, |
| 190: | $this->aWhere, |
| 191: | $this->mOrderAttributes, |
| 192: | $this->iSortOrder, |
| 193: | $this->aIdOrUuids, |
| 194: | $this->sCustomViewSql |
| 195: | ); |
| 196: | } |
| 197: | } |
| 198: | |
| 199: | if ($this->bOne && is_array($mResult) && count($mResult) > 0) { |
| 200: | $mResult = $mResult[0]; |
| 201: | } |
| 202: | } else { |
| 203: | $mResult = \Aurora\System\Managers\Eav::getInstance()->getEntitiesCount( |
| 204: | $this->sType, |
| 205: | $this->aWhere, |
| 206: | $this->aIdOrUuids |
| 207: | ); |
| 208: | } |
| 209: | |
| 210: | return $mResult; |
| 211: | } |
| 212: | |
| 213: | public static function prepareWhere($aRawWhere) |
| 214: | { |
| 215: | $aWhere = []; |
| 216: | |
| 217: | if (is_array($aRawWhere) && count($aRawWhere) > 0) { |
| 218: | $iAndIndex = 1; |
| 219: | $iOrIndex = 1; |
| 220: | foreach ($aRawWhere as $aSubWhere) { |
| 221: | if (is_array($aSubWhere)) { |
| 222: | foreach ($aSubWhere as $sKey => $a2ndSubWhere) { |
| 223: | if (is_array($a2ndSubWhere)) { |
| 224: | $sNewKey = $sKey; |
| 225: | if ($sKey === '$AND') { |
| 226: | $sNewKey = $iAndIndex.'$AND'; |
| 227: | $iAndIndex++; |
| 228: | } |
| 229: | if ($sKey === '$OR') { |
| 230: | $sNewKey = $iOrIndex.'$OR'; |
| 231: | $iOrIndex++; |
| 232: | } |
| 233: | $aWhere[$sNewKey] = $a2ndSubWhere; |
| 234: | } |
| 235: | } |
| 236: | } |
| 237: | } |
| 238: | } else { |
| 239: | |
| 240: | |
| 241: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::InvalidInputParameter); |
| 242: | } |
| 243: | |
| 244: | return $aWhere; |
| 245: | } |
| 246: | } |
| 247: | |