Пример #1
0
 protected function validateTags(Node $node, Node $obj, NodeSchema $schema)
 {
     $in_tags = array();
     $out_tags = array();
     $tag_defs = $schema->getTagDefs();
     $tag_strings = array();
     // Validate each tag def
     foreach ($tag_defs as $tag_def) {
         $allowed_values = array();
         $value_multiple = false;
         $value_mode = "none";
         $value_options = $tag_def->ValueOptions;
         if ($value_options) {
             $value_multiple = $value_options->isMultiple();
             $value_mode = strtolower((string) $value_options->Mode);
             if ($value_mode == 'predefined') {
                 // Extract values from the value tags
                 foreach ($value_options->getValues() as $value => $title) {
                     $allowed_values[] = (string) $value;
                 }
             }
         } else {
             $allowed_values[] = $tag_def->Partial->getTagValue();
         }
         if (strtolower($tag_def->Direction) == 'in') {
             $tags = $obj->getInTags($tag_def->Partial->getTagRole());
             //$tag_strings = &$in_tags;
         } else {
             $tags = $obj->getOutTags($tag_def->Partial->getTagRole());
             //$tag_strings = &$out_tags;
         }
         // remove duplicates from tags
         foreach ($tags as $o => $tag) {
             foreach ($tags as $i => $dtag) {
                 if ($o != $i && $tag->matchExact($dtag)) {
                     unset($tags[$o]);
                 }
             }
         }
         // Validate multiple across the entire tag
         if (!$tag_def->isMultiple() && count($tags) > 1) {
             $this->getErrors()->reject('Multiple tags not supported for tag role: ' . $tag_def->Id);
         }
         foreach ($tags as $tag) {
             // Validate Tag direction
             if (strcasecmp($tag_def->Direction, $tag->getTagDirection()) !== 0) {
                 $this->getErrors()->reject('Invalid tag direction. Should be an ' . $tag_def->Direction . ' tag. Tag: ' . $tag);
                 break;
                 // Don't even attempt to validate a tag in the wrong direction.
             }
             if (!$this->TagsHelper->matchPartial($tag_def->Partial, $tag)) {
                 $this->getErrors()->reject('Invalid tag [' . $tag->toString() . '] does not match tag definition partial [' . $tag_def->Partial . ']');
             }
             if ($value_mode == 'none' && !empty($tag->TagValue)) {
                 $this->getErrors()->reject('Tag has value where none is allowed:' . $tag->toString());
             }
             if ($value_mode == 'predefined' && !in_array($tag->TagValue, $allowed_values)) {
                 $this->getErrors()->reject('Value of "' . $tag->TagValue . '" not allowed for tag: ' . $tag->toString());
             }
             if ($value_mode != 'none') {
                 $key = $tag->TagElement . ':' . $tag->TagSlug;
                 @$values_per_slug[$key]++;
                 if (!$value_multiple && $values_per_slug[$key] > 1) {
                     $this->getErrors()->reject('Multiple values not supported for tag partial: ' . $tag_def->Partial->toString());
                 }
             }
             $tag_strings[] = $tag->toString();
         }
     }
     // Find any extra tags that haven't been validated
     foreach ($obj->getOutTags() as $tag) {
         if (!is_object($tag)) {
             $tag = new Tag($tag);
         }
         if (!in_array($tag->toString(), $tag_strings)) {
             $this->getErrors()->reject("Attached out tag doesn't exist in tag definitions: " . $tag->toString());
         }
     }
     foreach ($obj->getInTags() as $tag) {
         if (!is_object($tag)) {
             $tag = new Tag($tag);
         }
         if (!in_array($tag->toString(), $tag_strings)) {
             $this->getErrors()->reject("Attached in tag doesn't exist in TagDefs: " . $tag->toString());
         }
     }
 }
Пример #2
0
 protected function bindAll(Node &$node, NodeSchema &$schema, Errors &$errors, array $fields, array $rawFields)
 {
     foreach ($schema->getTagDefs() as $tagDef) {
         if ($tagDef->Direction == 'in') {
             $this->bindInTags($node, $errors, $fields, $rawFields, $tagDef->Id);
         } else {
             $this->bindOutTags($node, $errors, $fields, $rawFields, $tagDef->Id);
         }
     }
     foreach ($schema->getMetaDefs() as $metaDef) {
         $this->bindMeta($node, $errors, $fields, $rawFields, $metaDef->Id);
     }
 }