Scalr\Api\DataType\ApiEntityAdapter::validateObject PHP Method

validateObject() public method

It is applicable only to either POST or PATCH methods. It should be used on GET methods.
public validateObject ( object $object, string $method = null )
$object object An object provided with the request
$method string optional HTTP METHOD
    public function validateObject($object, $method = null)
    {
        if (!is_object($object)) {
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Invalid body');
        }
        $rules = $this->getRules();
        if (!empty($rules[static::RULE_TYPE_TO_DATA])) {
            $validFields = $rules[static::RULE_TYPE_TO_DATA];
        } else {
            //All fields from the Entity are allowed to be in the data object
            $entityClass = $this->getEntityClass();
            $entity = new $entityClass();
            $validFields = [];
            foreach ($entity->getIterator()->fields() as $field) {
                /* @var $field \Scalr\Model\Loader\Field */
                $validFields[$field->name] = $field->name;
            }
        }
        $objectVars = get_object_vars($object);
        $doesNotExist = array_diff(array_keys($objectVars), $validFields);
        if (!empty($rules[static::RULE_TYPE_SETTINGS])) {
            $doesNotExist = array_diff($doesNotExist, array_values($this->getSettingsRules()));
        }
        if (!empty($doesNotExist)) {
            if (count($doesNotExist) > 1) {
                $message = "You are trying to set properties %s that do not exist.";
            } else {
                $message = "You are trying to set the property %s which does not exist.";
            }
            throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, sprintf($message, implode(', ', $doesNotExist)));
        }
        foreach ($objectVars as $property => $val) {
            if (is_string($val)) {
                if (($key = array_search($property, $validFields)) && $key[0] === '_' && method_exists($this, $key)) {
                    //It is callable
                    continue;
                } elseif ($val != strip_tags($val)) {
                    throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Property %s contains invalid characters.", $property));
                }
            }
        }
    }

Usage Example

示例#1
0
 /**
  * {@inheritdoc}
  * @see \Scalr\Api\DataType\ApiEntityAdapter::validateObject()
  */
 public function validateObject($object, $method = null)
 {
     parent::validateObject($object, $method);
     if (isset($object->scope) && $object->scope !== ScopeInterface::SCOPE_ENVIRONMENT) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid scope");
     }
 }
All Usage Examples Of Scalr\Api\DataType\ApiEntityAdapter::validateObject