Exemplo n.º 1
0
 /**
  * Look-up and return the requested property value or magic value.
  * @param VCard $vcard The vcard to find the property in.
  * @param Substitution $substitution The Substitution currently being
  *  processed.
  * @param string $iterOver The name of a property being iterated over, or
  * null.
  * @param Property $iterItem The current value of the property being
  * iterated over, or null.
  * @return string
  */
 private function i_processLookUp(VCard $vcard, Substitution $substitution, $iterOver, Property $iterItem = null)
 {
     assert(null !== $vcard);
     assert(null !== $substitution);
     assert($substitution->shouldLookUp());
     $lookUpProperty = $substitution->getLookUp()['property'];
     assert(null !== $lookUpProperty);
     $value = '';
     if ($substitution->isMagic()) {
         if ($lookUpProperty == "_id") {
             $value = urlencode((string) $vcard->fn[0]);
         } else {
             if ($lookUpProperty == "_rawvcard") {
                 $value = $vcard->output();
             } else {
                 assert(false, 'bad magic:' . $lookUpProperty);
             }
         }
     } else {
         if ($substitution->lookUpIsStructured()) {
             $lookUpField = $substitution->getLookUp()['field'];
             // if we are already processing a list of #items...
             if ($lookUpProperty == $iterOver) {
                 \assert($iterItem instanceof StructuredProperty);
                 $value = $iterItem->getField($lookUpField);
             } else {
                 // otherwise look it up and *take first one found*
                 // FIXME: #64
                 $items = $vcard->{$lookUpProperty};
                 if (!empty($items)) {
                     \assert($items[0] instanceof StructuredProperty);
                     $value = htmlspecialchars($items[0]->getField($lookUpField) !== null ? $items[0]->getField($lookUpField) : '');
                 }
             }
         } else {
             if ($iterOver == $lookUpProperty) {
                 $value = htmlspecialchars($iterItem);
             } else {
                 $items = $vcard->{$lookUpProperty};
                 if (!empty($items)) {
                     if (is_array($items)) {
                         $value = htmlspecialchars(implode(" ", $items));
                     } else {
                         $value = htmlspecialchars($items);
                     }
                 }
             }
         }
     }
     return $value;
 }