/** * Gets a single element either by id or by conditions/order combination * @param string $elementClass The element class searched * @param int $elementId The element id searched, NULL if a specific condition is applied * @param string $conditions The conditions string to apply * @param string $orderBy The order string to apply * @return Element The found element or an exception if any error occures */ public static function getElement($elementClass, $elementId, $conditions = NULL, $orderBy = NULL, $join = NULL) { $logInstance = LogTool::getInstance(); $logInstance->logDebug('Gets ' . $elementClass . ' with id #' . $elementId . ' element'); // Gets element by id if ($elementId !== NULL) { // check that element id is an integer if (!StringTool::isInt($elementId)) { throw new Exception('ElementFactory::getElement: element id have to be an integer (' . $elementId . ')'); } $elementTable = DatabaseFactory::getElementTableName($elementClass); // Gets element from cache if ($join === NULL) { try { return CacheFactory::getElement($elementClass, $elementId); } catch (ElementFactoryException $e) { } } // Builds element id condition (table.table_id) $elementIdConditions = $elementTable . '.' . $elementTable . '_id = \'' . $elementId . '\''; // Merges id search and specific conditions $conditions = $conditions !== NULL ? $elementIdConditions . ' AND (' . $conditions . ')' : $elementIdConditions; } // Gets element by conditions/order combination from database $elementList = ElementFactory::getElementList($elementClass, $conditions, $orderBy, $join); // Wrong element list size $elementCount = count($elementList); if ($elementCount != 1) { if ($elementId !== NULL) { throw new ElementDoesNotExistException('Cannot find #' . $elementId . ' ' . $elementTable . ' (' . $elementCount . ' results)'); } else { if ($elementCount == 0) { throw new ElementNoResultException('Cannot get a single Element : nothing found ("' . $conditions . '", "' . $orderBy . '")'); } else { throw new ElementManyResultsException('Cannot get a single Element : ' . $elementCount . ' element(s) retrieved ("' . $conditions . '", "' . $orderBy . '")'); } } } // Extracts the element from list return reset($elementList); }