OEModule\PASAPI\resources\BaseResource::validate PHP Method

validate() public method

Base validator of resource from schema definition.
public validate ( ) : boolean
return boolean
    public function validate()
    {
        foreach ($this->schema as $tag => $defn) {
            if ($this->shouldValidateRequired() && array_key_exists('required', $defn) && $defn['required']) {
                if (!property_exists($this, $tag)) {
                    $this->addError("{$tag} is required");
                }
            }
            if (property_exists($this, $tag) && @$defn['choices']) {
                if ($this->{$tag} && !in_array($this->{$tag}, $defn['choices'])) {
                    $this->addError("Invalid value '{$this->{$tag}}' for {$tag}'");
                }
            }
            if (isset($defn['resource']) && property_exists($this, $tag)) {
                $resources_to_validate = is_array($this->{$tag}) ? $this->{$tag} : array($this->{$tag});
                foreach ($resources_to_validate as $idx => $resource) {
                    if (!method_exists($resource, 'validate')) {
                        $this->addError("Internal processing error for {$tag}");
                        continue;
                    }
                    if (!$resource->validate()) {
                        $tag_pos = count($resources_to_validate) > 1 ? ':' . ($idx + 1) : null;
                        foreach ($resource->errors as $err) {
                            $this->addError("{$tag}{$tag_pos} error: {$err}");
                        }
                    }
                }
            }
        }
        return count($this->errors) === 0;
    }

Usage Example

Example #1
0
 /**
  * As a primary resource (i.e. mapped to external resource) we need to ensure we have an id for tracking
  * the resource in the system.
  *
  * @return bool
  */
 public function validate()
 {
     if (!$this->id) {
         $this->addError('Resource ID required');
     }
     return parent::validate();
 }