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