public function testScopeResolver() { $o = new TestObjectValue(); $scope = array('a' => 'value of a', 'b' => null, 'bool' => false, 'dbl' => 3.141592653589793, 'o' => $o, 'nonassoc' => array('value0', 'value1', 'value2', array('c' => 'value of c')), 'assocval' => array('array' => array('d' => 'value of d', 'e' => 'value of e')), 'nonassocval' => array('array' => array(100, 101, 'x', null))); $lines = array('Value of a is [[json->a|upper|underscore]]', 'Value of b is [[json->b|upper]]', 'Value of bool is [[json->bool]]', 'Value of dbl is [[json->dbl|underscore]]', 'Value of o is [[json->o]]', 'Value of nonassoc is [[json->nonassoc]]', 'Value of nonassoc.1 is [[json->nonassoc/1]]', 'Value of nonassoc.3 is [[json->nonassoc/3]]', 'Value of nonassoc.3.c is [[json->nonassoc/3/c]]', 'Value of assocval is [[json->assocval]]', 'Value of assocval.array is [[json->assocval/array]]', 'Value of assocval.array.e is [[json->assocval/array/e]]', 'Value of nonassocval is [[json->nonassocval]]', 'Value of nonassocval.array is [[json->nonassocval/array]]', 'Value of nonassocval.array.0 is [[json->nonassocval/array/0]]', 'Value of nonassocval.array.2 is [[json->nonassocval/array/2]]'); $expectedLines = array('Value of a is VALUE_OF_A', 'Value of b is NULL', 'Value of bool is false', 'Value of dbl is 3_14159', 'Value of o is {"a":"a","b":["a","b",null]}', 'Value of nonassoc is ["value0","value1","value2",{"c":"value of c"}]', 'Value of nonassoc.1 is value1', 'Value of nonassoc.3 is {"c":"value of c"}', 'Value of nonassoc.3.c is value of c', 'Value of assocval is {"array":{"d":"value of d","e":"value of e"}}', 'Value of assocval.array is {"d":"value of d","e":"value of e"}', 'Value of assocval.array.e is value of e', 'Value of nonassocval is {"array":[100,101,"x",null]}', 'Value of nonassocval.array is [100,101,"x",null]', 'Value of nonassocval.array.0 is 100', 'Value of nonassocval.array.2 is x'); $string = implode("\n", $lines); $expectedString = implode("\n", $expectedLines); $tokenParser = new TokenParser(); $tokens = $tokenParser->parseString($string); $tokenResolver = new JsonScopeTokenResolver('json', $scope); $this->assertTrue($tokenResolver->getSerializer() instanceof JsonScopeTokenValueSerializer); $this->assertEquals('json', $tokenResolver->getScopeTokenName()); $this->assertEquals($scope, $tokenResolver->getScope()); $this->assertEquals(false, $tokenResolver->getIgnoreOutOfScope()); $tokenResolver->setScopeTokenNameDelimiter('->'); $this->assertEquals('->', $tokenResolver->getScopeTokenNameDelimiter()); $tokenResolver->setScopeLevelDelimiter('/'); $this->assertEquals('/', $tokenResolver->getScopeLevelDelimiter()); $this->assertTrue($tokenResolver->isTokenValueRegistered('json->nonassoc')); $this->assertFalse($tokenResolver->isTokenValueRegistered('json->nonassoc/unknown')); $this->assertFalse($tokenResolver->isTokenValueRegistered('other')); $tokens->resolve($tokenResolver); $result = TokenInjector::injectString($string, $tokens); $this->assertEquals($expectedString, $result); }
public function testSimpleTokenParser() { $data = array('a' => '[[token1]]', 'b' => '[[token5]]', 'c_[[token1~underscore~lower]]' => 'prefix-[[token1]]-suffix-[[token5]]', 'j' => '[[token6~dash~lower]]', 'd' => '[[token2~dash]]', 'e' => '[[token3~dot]]', 'f' => '[[token4~lower~unknown_filter]]', 'g' => '[[token5~upper]]', 'h' => '[[token5~underscore]]', 'i' => '[[token5~underscore~upper]]'); $expected = array('a' => 'The Value-of.Token1', 'b' => 'The Value-of.Token5', 'c_the_value_of_token1' => 'prefix-The Value-of.Token1-suffix-The Value-of.Token5', 'j' => '[[token6~dash~lower]]', 'd' => 'The-Value-of-Token2', 'e' => 'The.Value.of.Token3', 'f' => 'the value-of.token4', 'g' => 'THE VALUE-OF.TOKEN5', 'h' => 'The_Value_of_Token5', 'i' => 'THE_VALUE_OF_TOKEN5'); $jsonData = json_encode($data); $jsonExpectedData = json_encode($expected); $tokenParser = new TokenParser('~'); $tokens = $tokenParser->parseString($jsonData); $this->assertEquals(md5($jsonData), $tokens->getSourceHash()); $result = $tokens->findByName('token1'); $this->assertEquals(2, count($result)); $result = $tokens->findByName('token2'); $this->assertEquals(1, count($tokens->findByName('token2'))); $this->assertEquals(0, count($tokens->findByName('token7'))); $this->assertTrue(isset($result['[[token2~dash]]'])); $result = current($result); /** @var Token $result */ $filters = $result->getFilters(); $this->assertEquals(1, count($filters)); $this->assertEquals('dash', current($filters)); $this->assertEquals(4, count($tokens->findByName('token5'))); $this->assertEquals(10, $tokens->getCount()); $tokenValues = array('token1' => 'The Value-of.Token1', 'token2' => 'The_Value of.Token2', 'token3' => 'The Value-of_Token3', 'token4' => 'The Value-of.Token4', 'token5' => 'The Value-of.Token5', 'token7' => 'The Value-of.Token7'); $tokenResolver = new RegisteredTokenResolver($tokenValues); $this->assertEquals($tokenValues, $tokenResolver->getRegisteredTokenValues()); $this->assertEquals($tokenValues['token5'], $tokenResolver->getRegisteredTokenValue('token5')); $this->assertNull($tokenResolver->getRegisteredTokenValue('token6')); $tokens->resolve($tokenResolver); $resolvedTokens = $tokens->findResolved(); $expectedResolved = array('[[token1]]', '[[token5]]', '[[token1~underscore~lower]]', '[[token2~dash]]', '[[token3~dot]]', '[[token4~lower~unknown_filter]]', '[[token5~upper]]', '[[token5~underscore]]', '[[token5~underscore~upper]]'); $this->assertEquals($expectedResolved, array_keys($resolvedTokens)); $resolvedAndNotInjectedTokens = $tokens->findResolvedAndNotInjected(); $this->assertEquals($expectedResolved, array_keys($resolvedAndNotInjectedTokens)); $this->assertTrue($tokens->hasUnresolved()); $unresolvedTokens = $tokens->findUnresolved(); $this->assertEquals(array('[[token6~dash~lower]]'), array_keys($unresolvedTokens)); /** @var Token $unresolvedToken */ $unresolvedToken = current($unresolvedTokens); $this->assertNull($unresolvedToken->getTokenValue()); $this->assertEquals(array('dash', 'lower'), array_values($unresolvedToken->getUnresolvedFilters())); $withUnresolvedFilters = $tokens->findWithUnresolvedFilters(); $this->assertEquals(2, count($withUnresolvedFilters)); /** @var Token $token6 */ $token6 = current($unresolvedTokens); $tokenResolver->registerTokenValue('token6', 'The Value of Token6'); $token6->resolve($tokenResolver); $this->assertTrue($token6->getIsResolved()); $tokenResolver->unRegisterTokenValue($token6->getTokenName()); $this->assertFalse($tokenResolver->isTokenValueRegistered($token6->getTokenName())); try { $tokenResolver->unRegisterTokenValue($token6->getTokenName()); $this->assertTrue(false); } catch (UnknownTokenException $e) { $this->assertTrue(true); } try { $tokenResolver->getTokenValue($token6->getTokenName(), false); $this->assertTrue(false); } catch (UnknownTokenException $e) { $this->assertTrue(true); } $token6->resolve($tokenResolver); $this->assertTrue($token6->getIsResolved()); $this->assertTrue($token6->getIsFiltered()); $this->assertFalse($token6->hasUnresolvedFilters()); $unresolvedTokens = $tokens->findUnresolved(); $this->assertEmpty($unresolvedTokens); $tokens->remove($token6); $this->assertFalse($tokens->has($token6->getTokenString())); unset($result); $result = $tokens->findByName('token4'); $token4 = current($result); $this->assertTrue($token4->getIsResolved()); $this->assertTrue($token4->getIsFiltered()); $this->assertTrue($token4->hasUnresolvedFilters()); try { $token4->applyFilters(false); $this->assertTrue(false); } catch (UnknownFilterException $e) { $this->assertTrue(true); } $withUnresolvedFilters = $tokens->findWithUnresolvedFilters(); $this->assertEquals(1, count($withUnresolvedFilters)); $tokenCollectionImp = new TokenCollection(array($token6->getTokenString() => $token6)); $tokens->import($tokenCollectionImp); $this->assertTrue($tokens->has($token6->getTokenString())); $tokens->remove($token6); /** @var Token $tokenWithUnresolvedFilters */ $tokenWithUnresolvedFilters = current($withUnresolvedFilters); $this->assertEquals(array('unknown_filter'), array_values($tokenWithUnresolvedFilters->getUnresolvedFilters())); $result = TokenInjector::injectString($jsonData, $tokens, true); $this->assertEquals($jsonExpectedData, $result); $tokens->clear(); $this->assertEquals(0, $tokens->getCount()); }
public function applyToArray(&$array) { foreach ($this->collection as $xrefTokenResolver) { $tokenParser = $xrefTokenResolver->getTokenParser(); $lookup = array(); $this->getTokensFromArray($array, $tokenParser, $lookup); foreach ($lookup as $record) { if (isset($record[self::_VALUE_TOKENS])) { $this->resolveTokens($record[self::_VALUE_TOKENS], $xrefTokenResolver); } if (isset($record[self::_KEY_TOKENS])) { $this->resolveTokens($record[self::_KEY_TOKENS], $xrefTokenResolver); } } foreach ($lookup as &$record) { if (isset($record[self::_VALUE])) { $newValue = TokenInjector::injectString($record[self::_VALUE], $record[self::_VALUE_TOKENS]); $record[self::_VALUE_REF] = $newValue; unset($record[self::_VALUE_TOKENS]); unset($record[self::_VALUE_REF]); } if (isset($record[self::_KEY])) { $oldKey = $record[self::_KEY]; $newKey = TokenInjector::injectString($oldKey, $record[self::_KEY_TOKENS]); unset($record[self::_KEY_TOKENS]); if ($oldKey != $newKey) { $record[self::_KEY_REF][$newKey] = $record[self::_KEY_REF][$oldKey]; unset($record[self::_KEY_REF][$oldKey]); unset($record[self::_KEY_REF]); } } } unset($record); } }