Scalr\Api\Rest\Controller\ApiController::getBareId PHP Méthode

getBareId() public static méthode

Gets bare id from request object
public static getBareId ( object $object, string $item = null ) : mixed
$object object Request object
$item string optional Item name (image, role) or null, if object is target
Résultat mixed
    public static function getBareId($object, $item = null)
    {
        if (is_array($object)) {
            $object = (object) $object;
        }
        if (isset($item) && !empty($object->{$item})) {
            $object = $object->{$item};
        }
        if (!empty($object->id)) {
            return $object->id;
        } else {
            if (!empty($object)) {
                if (is_array($object) && !empty($object['id'])) {
                    return $object['id'];
                } else {
                    if (!(is_array($object) || is_object($object))) {
                        return $object;
                    }
                }
            }
        }
        return null;
    }

Usage Example

Exemple #1
0
 protected function _os($from, $to, $action)
 {
     if ($action == self::ACT_CONVERT_TO_OBJECT) {
         $to->os = !empty($from->osId) ? ['id' => $from->osId] : null;
     } else {
         if ($action == self::ACT_CONVERT_TO_ENTITY) {
             $osId = ApiController::getBareId($from, 'os');
             if (!empty($osId)) {
                 if (!is_string($osId) || !preg_match('/^' . Entity\Os::ID_REGEXP . '$/', $osId)) {
                     throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid identifier of the OS");
                 }
                 $to->osId = $osId;
             } else {
                 $to->osId = null;
             }
         } else {
             if ($action == self::ACT_GET_FILTER_CRITERIA) {
                 if (empty($from->os) || !preg_match('/^' . Entity\Os::ID_REGEXP . '$/', $from->os)) {
                     throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid identifier of the OS");
                 }
                 return [['osId' => $from->os]];
             }
         }
     }
 }
All Usage Examples Of Scalr\Api\Rest\Controller\ApiController::getBareId