/** * Process and return the requested fragment, making further substitutions * as necessary. * @param VCard $vcard The card to look up values in. Not null. * @param string $fragmentKey The key to the fragment to output, not null. * @param string $iterOver The name of any property being iterated over, * or null. * @param string $iterItem The current value of any property being iterated * over, or null. * @return string */ private function i_processFragment(VCard $vcard, $fragmentKey, $iterOver = null, $iterItem = null) { assert(null !== $vcard); assert(null !== $fragmentKey); assert(is_string($fragmentKey)); $fragment = $this->i_findFragment($fragmentKey); $value = ''; if (null !== $fragment) { $low = 0; while (($high = strpos($fragment, '{{', $low)) !== false) { // Strip and output until we hit a template marker $value .= substr($fragment, $low, $high - $low); // strip the front marker $low = $high + 2; $high = strpos($fragment, '}}', $low); // Remove and process the new marker $newSubstitution = Substitution::fromText(substr($fragment, $low, $high - $low)); $high += 2; $low = $high; $value .= self::i_processSubstitution($vcard, $newSubstitution, $iterOver, $iterItem); } $value .= substr($fragment, $low); } // if fragment return $value; }
/** * @group default */ public function testFromTextIterates() { $substitution = Substitution::fromText('key, #n'); $this->assertInstanceOf('EVought\\vCardTools\\Substitution', $substitution); $this->assertTrue($substitution->hasFragment()); $this->assertEquals('key', $substitution->getFragment()); $this->assertFalse($substitution->hasQuest()); $this->assertFalse($substitution->shouldLookUp()); $this->assertTrue($substitution->iterates()); $this->assertEquals('n', $substitution->getIterOver()); }
/** * Parse the given text to produce and return a Substitution. * @param string $text * @return \vCardTools\Substitution */ public static function fromText($text) { assert($text !== null); assert(is_string($text)); $substitution = new Substitution(); // separate by commas, ignore leading and trailing space $text_parts = array_map("trim", explode(",", $text)); foreach ($text_parts as $part) { // If we have multiples of the same type, last one clobbers // Figure out what it is and store it if (substr($part, 0, 1) == "!") { $substitution->parseBangProperty(substr($part, 1)); } else { if (substr($part, 0, 1) == "?") { $substitution->quest = substr($part, 1); } else { if (substr($part, 0, 1) == "#") { $substitution->iterOver = substr($part, 1); } else { $substitution->fragment = $part; } } } } return $substitution; }