OEModule\PASAPI\resources\BaseResource::remapValues PHP Method

remapValues() public static method

Update the given doc nodes as per the provided remaps.
public static remapValues ( $doc, XpathRemap[] $remaps = [] )
$doc
$remaps OEModule\PASAPI\models\XpathRemap[]
    public static function remapValues($doc, $remaps = array())
    {
        if (!count($remaps)) {
            return;
        }
        // no point in instantiating DOMXPath if nothing to remap
        $xdoc = new \DOMXPath($doc);
        foreach ($remaps as $remap) {
            if ($el = $xdoc->query($remap->xpath)) {
                $lookup = array();
                foreach ($remap->values as $val) {
                    $lookup[$val->input] = $val->output;
                }
                // track index for item removal
                $remove_list = array();
                for ($i = 0; $i < $el->length; ++$i) {
                    $current = $el->item($i)->textContent;
                    if (array_key_exists($current, $lookup)) {
                        if (is_null($lookup[$current])) {
                            $remove_list[] = $i;
                        } else {
                            $el->item($i)->nodeValue = $lookup[$current];
                        }
                    }
                }
                foreach ($remove_list as $remove) {
                    $el->item($remove)->parentNode->removeChild($el->item($remove));
                }
            }
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * @dataProvider xpathremapping_provider
  *
  * @param $xml
  * @param $maps
  * @param $expected
  */
 public function test_Xpathremapping($xml, $maps, $expected)
 {
     $doc = new \DOMDocument();
     $doc->loadXML($xml);
     $remaps = array();
     foreach ($maps as $m) {
         $remaps[] = $this->generateMappings($m);
     }
     BaseResource::remapValues($doc, $remaps);
     $this->assertXmlStringEqualsXmlString($expected, $doc->saveXML());
 }