protected function singleEntity($entity)
    {
        if (empty($entity)) {
            return null;
        }

        $singleEntity = $entity;

        if (is_array($entity)) {
            if (count($entity) > 1) {
                throw RuntimeException::multipleResultsFound();
            }

            $singleEntity = reset($entity);
        }

        $entityClass = $this->getEntityName();

        if (!$singleEntity instanceof $entityClass) {
            throw InvalidArgumentException::invalidEntityType(
                $this->getEntityName(),
                $singleEntity
            );
        }

        return $singleEntity;
    }