public function validateEntity($entity)
{
if (!$entity instanceof RoleCategory) {
throw new \InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\RoleCategory class"));
}
if (!preg_match('/^' . RoleCategory::NAME_REGEXP . '$/', $entity->name)) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Invalid name of the Role Category. Name should start and end with letter or number and contain only letters, numbers, spaces and dashes.');
}
if (strlen($entity->name) > RoleCategory::NAME_LENGTH) {
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, 'Name should be less than 18 characters');
}
if (is_null($entity->id)) {
$criteria = $this->controller->getScopeCriteria($entity->getScope());
$criteria[] = ['name' => $entity->name];
if (!empty(RoleCategory::findOne($criteria))) {
throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf('Role Category with name %s already exists', $entity->name));
}
} else {
if (empty(RoleCategory::findPk($entity->id))) {
throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Role Category with id: %d", $entity->id));
}
}
}