/** * @param $arguments * @param TransformationContext $context * @return true */ public function call($arguments, TransformationContext $context) { /** @var string $groupId */ $groupId = $arguments[0]; /** @var DOMElement[] $elements */ $elements = $arguments[1]; /** @var string $elementId */ $elementId = $arguments[2]; /** @var string $groupBy */ $groupBy = base64_decode($arguments[3]); /** @var string[] $namespaces */ $namespaces = json_decode(base64_decode($arguments[4]), true); $collection = $this->groups->get($groupId); foreach ($elements as $key => $element) { $xpath = new DOMXPath($element->ownerDocument); $xpath->registerNamespace('php', 'http://php.net/xpath'); $xpath->registerPhpFunctions(); $element->ownerDocument->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:php', 'http://php.net/xpath'); foreach ($namespaces as $prefix => $namespace) { $element->ownerDocument->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $prefix, $namespace); $xpath->registerNamespace($prefix, $namespace); } $groupingKey = $xpath->evaluate($this->compiler->compile('string(' . $groupBy . ')', $element), $element); $collection->get($groupingKey)->addId($elementId); } return true; }
/** * @param DOMAttr $attribute * @throws InvalidArgumentException * @credits https://github.com/Saxonica/Saxon-CE/ https://github.com/Saxonica/Saxon-CE/blob/master/notices/MOZILLA.txt */ public function transform(DOMAttr $attribute) { $expression = $attribute->nodeValue; $components = []; $length = strlen($expression); $last = 0; while ($last < $length) { $i0 = strpos($expression, "{", $last); $i1 = strpos($expression, "{{", $last); $i8 = strpos($expression, "}", $last); $i9 = strpos($expression, "}}", $last); if (($i0 === false || $length < $i0) && ($i8 === false || $length < $i8)) { // found end of string $components[] = substr($expression, $last); break; } elseif ($i8 >= 0 && ($i0 === false || $i8 < $i0)) { // found a "}" if ($i8 !== $i9) { // a "}" that isn't a "}}" $exception = new InvalidArgumentException("Closing curly brace in attribute value template \"" . $expression . "\" must be doubled"); $exception->setErrorCode("XTSE0370"); throw $exception; } $components[] = substr($expression, $last, $i8 + 2 - $last); $last = $i8 + 2; } elseif ($i1 >= 0 && $i1 === $i0) { // found a doubled "{{" $components[] = substr($expression, $last, $i1 + 2 - $last); $last = $i1 + 2; } elseif ($i0 >= 0) { // found a single "{" if ($i0 > $last) { $components[] = substr($expression, $last, $i0 - $last); } if ($i8 === false) { $exception = new InvalidArgumentException("Curly brace in attribute value template \"" . $expression . "\" must be closed"); $exception->setErrorCode("XTSE0370"); throw $exception; } $compileFrom = $i0 + 1; $compileUntil = $i8 - $compileFrom; $components[] = '{'; $components[] = $this->xpathCompiler->compile(substr($expression, $compileFrom, $compileUntil), $attribute); $components[] = '}'; $last = $i8 + 1; } else { // @codeCoverageIgnoreStart throw new RuntimeException("Internal error parsing Attribute Value Template"); // @codeCoverageIgnoreEnd } } $attribute->nodeValue = htmlentities(implode('', $components), ENT_XML1); }
/** * @param $arguments * @param TransformationContext $context * @return true */ public function call($arguments, TransformationContext $context) { /** @var string $groupId */ $groupId = $arguments[0]; /** @var DOMElement[] $elements */ $elements = $arguments[1]; /** @var string $elementId */ $elementId = $arguments[2]; /** @var string $groupBy */ $groupBy = base64_decode($arguments[3]); $collection = $this->groups->get($groupId); foreach ($elements as $key => $element) { $xpath = new DOMXPath($element->ownerDocument); $xpath->registerPhpFunctions($context->getPhpFunctions()); $groupingKey = $xpath->evaluate($this->compiler->compile('string(' . $groupBy . ')', $element), $element); $collection->get($groupingKey)->addId($elementId); } return true; }
/** * @param DOMElement $element */ public function transform(DOMElement $element) { $element->setAttribute('select', $this->xpathCompiler->compile($element->getAttribute('select'), $element)); }