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));
}
}
}
}