/**
  * {@inheritdoc}
  */
 public function validate(ElggEntity $entity)
 {
     $shortname = $this->getShortname();
     $validation = new ValidationStatus();
     $value = elgg_extract($shortname, $_FILES, array());
     $error_type = elgg_extract('error', $value);
     $has_uploaded_file = $error_type != UPLOAD_ERR_NO_FILE;
     if (!$has_uploaded_file) {
         if ($this->isRequired() && empty($this->getValues($entity))) {
             $validation->setFail(elgg_echo('prototyper:validate:error:required', array($this->getLabel())));
         }
     } else {
         $error = hypeApps()->uploader->getFriendlyUploadError($error_type);
         if ($error) {
             $validation->setFail($error);
         } else {
             $validation = $this->applyValidationRules($value, $validation, $entity);
         }
     }
     return $validation;
 }
/**
 * Validates that input matches a regex
 *
 * @param string           $hook       "validate:img_min_width"
 * @param string           $type       "prototyper"
 * @param ValidationStatus $validation Current validation status
 * @param array            $params     Hook params
 * @return ValidationStatus
 */
function prototyper_validate_img_dimensions($hook, $type, $validation, $params)
{
    if (!$validation instanceof ValidationStatus) {
        $validation = new ValidationStatus();
    }
    $field = elgg_extract('field', $params);
    if (!$field instanceof ImageUploadField) {
        return $validation;
    }
    $value = elgg_extract('value', $params);
    $expectation = elgg_extract('expectation', $params);
    $img = elgg_extract('tmp_name', $value);
    $sizeinfo = getimagesize($img);
    if (!$sizeinfo) {
        $validation->setFail(elgg_echo('prototyper:validate:error:image_dimensions', array($field->getLabel())));
        return $validation;
    }
    list($width, $height) = $sizeinfo;
    $rule = elgg_extract('rule', $params);
    switch ($rule) {
        case 'img_min_width':
            if ($width < $expectation) {
                $validation->setFail(elgg_echo('prototyper:validate:error:img_min_width', array($field->getLabel(), $expectation)));
            }
            break;
        case 'img_max_width':
            if ($width > $expectation) {
                $validation->setFail(elgg_echo('prototyper:validate:error:img_max_width', array($field->getLabel(), $expectation)));
            }
            break;
        case 'img_min_height':
            if ($height < $expectation) {
                $validation->setFail(elgg_echo('prototyper:validate:error:img_min_height', array($field->getLabel(), $expectation)));
            }
            break;
        case 'img_max_height':
            if ($height > $expectation) {
                $validation->setFail(elgg_echo('prototyper:validate:error:img_max_height', array($field->getLabel(), $expectation)));
            }
            break;
    }
    return $validation;
}