Exemplo n.º 1
0
 /**
  * @expectedException \ConfigToken\TokenResolver\Exception\ScopeTokenValueSerializationException
  */
 public function testCustomSerializerException()
 {
     $scope = array('a' => 'value of a', 'b' => array('c'));
     $lines = array('Value of a is [[json:a]]', 'Value of b is [[json:b]]');
     $string = implode("\n", $lines);
     $tokenParser = new TokenParser();
     $tokens = $tokenParser->parseString($string);
     $tokenResolver = new ScopeTokenResolver('json', $scope);
     $tokenResolver->setSerializer(new CustomScopeTokenValueSerializer());
     $tokens->resolve($tokenResolver, false);
 }
Exemplo n.º 2
0
 /**
  * Parse the array corresponding to the $includeKey:$includeXrefKey:<xref name>:$includeXrefResolveKey.
  *
  * @param string $xrefKey
  * @param array $tokenResolversInfo
  * @param XrefTokenResolverCollection $tokenResolvers
  * @param Xref[] $xrefPath
  * @return XrefTokenResolverCollection
  * @throws \Exception
  */
 protected function parseXrefTokenResolverDefinitions($xrefKey, $tokenResolversInfo, XrefTokenResolverCollection $tokenResolvers = null, $xrefPath)
 {
     $resolverValues = array();
     $tokenResolverDefinitionIndex = 0;
     // validate
     if (!is_array($tokenResolversInfo)) {
         throw new TokenResolverDefinitionException(sprintf('Token resolver definitions at index %d for Xref key "%s" must be an array. (%s)', $tokenResolverDefinitionIndex, $xrefKey, json_encode($tokenResolversInfo)));
     }
     foreach ($tokenResolversInfo as $tokenResolverKey => $tokenResolverInfo) {
         if (!is_array($tokenResolverInfo)) {
             throw new TokenResolverDefinitionException(sprintf('Token resolver definition at index %d for Xref key "%s" must be an associative array. (%s)', $tokenResolverKey, $xrefKey, json_encode($tokenResolversInfo)));
         }
         if (!isset($tokenResolverInfo[$this->xrefTokenResolverTypeKey])) {
             throw new TokenResolverDefinitionException(sprintf("Token resolver definition at index %d for Xref key \"%s\" is missing the \"%s\" type identifier key.\n%s", $tokenResolverDefinitionIndex, $xrefKey, $this->xrefTokenResolverTypeKey, json_encode($tokenResolverInfo)));
         }
         $tokenResolverType = $tokenResolverInfo[$this->xrefTokenResolverTypeKey];
         if (!TokenResolverFactory::isRegisteredByType($tokenResolverType)) {
             throw new TokenResolverDefinitionException(sprintf('Unknown token resolver type identifier "%s" at index %d for Xref key "%s".', $tokenResolverType, $tokenResolverDefinitionIndex, $xrefKey));
         }
         $hasValues = isset($tokenResolverInfo[$this->xrefTokenResolverValuesKey]);
         $hasValuesXref = isset($tokenResolverInfo[$this->xrefTokenResolverValuesXrefKey]);
         if (!$hasValues && !$hasValuesXref) {
             throw new TokenResolverDefinitionException(sprintf('Token resolver definition at index %d for Xref key "%s" does not have a "%s" key or a "%s" key.', $tokenResolverDefinitionIndex, $xrefKey, $this->xrefTokenResolverValuesKey, $this->xrefTokenResolverValuesXrefKey));
         }
         $tokenResolverBaseType = TokenResolverFactory::getBaseTypeForType($tokenResolverType);
         if (isset($tokenResolverInfo[$this->xrefTokenResolverOptionsKey])) {
             $options = $tokenResolverInfo[$this->xrefTokenResolverOptionsKey];
             $this->validateXrefTokenResolverOptions($xrefKey, $tokenResolverDefinitionIndex, $tokenResolverBaseType, $options);
         }
         if ($hasValues) {
             $values = $tokenResolverInfo[$this->xrefTokenResolverValuesKey];
         } else {
             // has values xref
             $xrefInfo = $tokenResolverInfo[$this->xrefTokenResolverValuesXrefKey];
             $xref = $this->parseXrefInfo(sprintf('%s.%s[%d]', $xrefKey, $this->includeXrefResolversKey, $tokenResolverDefinitionIndex), $xrefInfo, $tokenResolvers, $xrefPath);
             // pass down current token resolvers
             $values = $this->recursiveCompileXref($xref, $tokenResolvers, null, null, $xrefPath);
         }
         if (!is_array($values)) {
             throw new TokenResolverDefinitionException(sprintf('The "%s" key must be an associative array for token resolver definition at ' . 'index %d for Xref key "%s".', $this->xrefTokenResolverValuesKey, $tokenResolverDefinitionIndex, $xrefKey));
         }
         $resolverValues[$tokenResolverKey] = $values;
         $tokenResolverDefinitionIndex++;
     }
     $result = new XrefTokenResolverCollection();
     // parse
     foreach ($tokenResolversInfo as $tokenResolverKey => $tokenResolverInfo) {
         if (isset($tokenResolverInfo[$this->xrefTokenResolverOptionsKey])) {
             $options = $tokenResolverInfo[$this->xrefTokenResolverOptionsKey];
         } else {
             $options = null;
         }
         $tokenResolver = TokenResolverFactory::get($tokenResolverInfo[$this->xrefTokenResolverTypeKey]);
         $xrefTokenResolver = new XrefTokenResolver($tokenResolver);
         $values = $resolverValues[$tokenResolverKey];
         $xrefTokenResolver->setRegisteredTokenValues($values);
         if (isset($options)) {
             if (isset($options[$this->xrefTokenResolverOptionIgnoreUnknownTokensKey])) {
                 $tokenResolver->setIgnoreUnknownTokens($options[$this->xrefTokenResolverOptionIgnoreUnknownTokensKey]);
             }
             if ($tokenResolver::getBaseType() == ScopeTokenResolver::getBaseType()) {
                 /** @var ScopeTokenResolver $tokenResolver */
                 if (isset($options[$this->xrefTokenResolverOptionScopeTokenNameKey])) {
                     $tokenResolver->setScopeTokenName($options[$this->xrefTokenResolverOptionScopeTokenNameKey]);
                 }
                 if (isset($options[$this->xrefTokenResolverOptionScopeTokenNameDelimiterKey])) {
                     $tokenResolver->setScopeTokenNameDelimiter($options[$this->xrefTokenResolverOptionScopeTokenNameDelimiterKey]);
                 }
                 if (isset($options[$this->xrefTokenResolverOptionScopeTokenLevelDelimiterKey])) {
                     $tokenResolver->setScopeLevelDelimiter($options[$this->xrefTokenResolverOptionScopeTokenLevelDelimiterKey]);
                 }
                 if (isset($options[$this->xrefTokenResolverOptionIgnoreOutOfScopeKey])) {
                     $tokenResolver->setIgnoreOutOfScope($options[$this->xrefTokenResolverOptionIgnoreOutOfScopeKey]);
                 }
             }
             if (isset($options[$this->xrefTokenResolverOptionIgnoreUnknownFiltersKey])) {
                 $xrefTokenResolver->setIgnoreUnknownFilters($options[$this->xrefTokenResolverOptionIgnoreUnknownFiltersKey]);
             }
             if (isset($options[$this->xrefTokenResolverOptionTokenRegexKey])) {
                 $xrefTokenResolver->setTokenRegex($options[$this->xrefTokenResolverOptionTokenRegexKey]);
             }
             if (isset($options[$this->xrefTokenResolverOptionTokenPrefixKey])) {
                 $xrefTokenResolver->setTokenPrefix($options[$this->xrefTokenResolverOptionTokenPrefixKey]);
             }
             if (isset($options[$this->xrefTokenResolverOptionTokenSuffixKey])) {
                 $xrefTokenResolver->setTokenSuffix($options[$this->xrefTokenResolverOptionTokenSuffixKey]);
             }
             if (isset($options[$this->xrefTokenResolverOptionTokenFilterDelimiterKey])) {
                 $xrefTokenResolver->setTokenFilterDelimiter($options[$this->xrefTokenResolverOptionTokenFilterDelimiterKey]);
             }
         }
         $result->add($xrefTokenResolver);
     }
     return $result;
 }
Exemplo n.º 3
0
 public function __construct($scopeTokenName = null, $scope = null, $ignoreOutOfScope = False)
 {
     parent::__construct($scopeTokenName, $scope, $ignoreOutOfScope);
     $this->setSerializer(new JsonScopeTokenValueSerializer());
 }