Example #1
0
 /**
  * Add the node to a position under the tree
  *
  * @param \SimpleXmlElement|Element $node
  * @param Element $parent
  */
 public function addChild($node, $parent = null)
 {
     if ($node instanceof \SimpleXmlElement) {
         $name = $node->getName();
         $attributes = (array) $node->attributes();
         $content = trim((string) $node);
         $element = new Element($name, $attributes, $content);
         if (!$this->tree) {
             $this->tree = $element;
         } else {
             if (!$parent) {
                 $parent = $this->tree;
             }
             $parent->addChild($element);
         }
         // Add child elements recursive
         if ($node->count() > 0) {
             foreach ($node as $childNode) {
                 $this->addChild($childNode, $element);
             }
         }
     } else {
         if ($node instanceof Element) {
             if (!$this->tree) {
                 $this->tree = $node;
             } else {
                 if (!$parent) {
                     $parent = $this->tree;
                 }
                 $parent->addChild($node);
             }
         }
     }
 }
Example #2
0
 /**
  * Gets a path to a node via SPL Stack implementation
  *
  * Pass in the child node and will recurse up the XML tree to print out
  * the path in the tree to that node
  *
  * <config>
  *   <path>
  *     <to>
  *       <node>
  *         Node Value
  *       </node>
  *     </to>
  *   </path>
  * </config>
  *
  * If you pass in the "node" object, this will print out
  * config/path/to/node/
  *
  * @param SimpleXmlElement $element Child element to find path to
  *
  * @return string
  * @access public
  */
 public function getPath(SimpleXmlElement $element)
 {
     $this->_iterator->push($element->getName() . '/');
     if (!$element->getSafeParent()) {
         return $this->_iterator->pop();
     }
     return $this->getPath($element->getParent()) . $this->_iterator->pop();
 }
Example #3
0
 /**
  * Gets a path to a node via array implementation
  *
  * Pass in the child node and will recurse up the XML tree to print out
  * the path in the tree to that node
  *
  * <config>
  *   <path>
  *     <to>
  *       <node>
  *         Node Value
  *       </node>
  *     </to>
  *   </path>
  * </config>
  *
  * If you pass in the "node" object, this will print out
  * config/path/to/node/
  *
  * @param SimpleXmlElement $element Child element to find path to
  *
  * @return string
  * @access public
  */
 public function getPath(SimpleXmlElement $element)
 {
     $this->_iterator[] = $element->getName() . '/';
     if (!$element->getSafeParent()) {
         return array_pop($this->_iterator);
     }
     return $this->getPath($element->getParent()) . array_pop($this->_iterator);
 }
Example #4
0
 public static function arrayToXml(\SimpleXMLElement $object = null, $data = array())
 {
     foreach ($data as $key => $value) {
         if (is_object($value)) {
             $value = ArrayUtilsVTT::objectToArray($value);
         }
         if ($load = simplexml_load_string($value)) {
             $keyXMl = null;
             $doc = new \DOMDocument();
             $doc->loadXML($object->asXML());
             $appendXml = $doc->createDocumentFragment();
             $keyXMl = is_numeric($key) ? StringUtilsVTT::ToPluralString($load->children()->getName()) : $key;
             $stringXml = "<{$keyXMl}>";
             foreach ($load as $item) {
                 $stringXml .= $item->asXML();
             }
             $stringXml .= "</{$keyXMl}>";
             $appendXml->appendXML($stringXml);
             $doc->documentElement->appendChild($appendXml);
             $object = new \SimpleXmlElement($doc->saveXML());
             continue;
         }
         if (is_array($value)) {
             if (!is_numeric($key)) {
                 $new_object = $object->addChild($key);
             } else {
                 $new_object = $object->addChild($object->getName());
             }
             self::arrayToXml($new_object, $value, false);
         } else {
             if (!is_numeric($key)) {
                 $object->addChild($key, $value);
             } else {
                 $object->addChild($object->getName(), $value);
             }
         }
     }
     return $object;
 }
 /**
  * given a single ->data or ->struct element, return an array containing its contents as old toolkit-style data description.
  *
  * @param \SimpleXmlElement $dataElement
  * @return array
  */
 public function singlePcmlToArray(\SimpleXmlElement $dataElement)
 {
     $tagName = $dataElement->getName();
     // get attributes of this element.
     $attrs = $dataElement->attributes();
     // both struct and data have name, count (optional), usage
     $name = isset($attrs['name']) ? (string) $attrs['name'] : '';
     $count = isset($attrs['count']) ? (string) $attrs['count'] : '';
     $usage = isset($attrs['usage']) ? (string) $attrs['usage'] : '';
     $structName = isset($attrs['struct']) ? (string) $attrs['struct'] : '';
     // fill this if we have a struct
     $subElements = array();
     // should all be data
     if ($tagName == 'data') {
         $type = isset($attrs['type']) ? (string) $attrs['type'] : '';
         // if a struct then we need to recurse.
         if ($type != 'struct') {
             // regular type (char, int...), not a struct, so the data element's name is just 'name'.
             $nameName = 'Name';
         } else {
             // it IS a struct.
             // old toolkit uses DSName for a data structure's name.
             $nameName = 'DSName';
             $theStruct = null;
             // init
             // look for matching struct
             if ($this->_pcmlStructs) {
                 // TODO verify type with is_array and count
                 foreach ($this->_pcmlStructs as $possibleStruct) {
                     $possStructAttrs = $possibleStruct->attributes();
                     if ($possStructAttrs['name'] == $structName) {
                         $theStruct = $possibleStruct;
                         $structAttrs = $possStructAttrs;
                         break;
                     }
                 }
             }
             // if struct was not found, generate error for log
             if (!$theStruct) {
                 //                    $this->getConnection->logThis("PCML structure '$structName' not found.");
                 return null;
             }
             // if we got here, we found our struct.
             // count can also be defined at the structure level. If so, it will override count from data level)
             if (isset($structAttrs['count'])) {
                 $count = (string) $structAttrs['count'];
             }
             // "usage" (in/out/inherit) can be defined here, at the structure level.
             $structUsage = isset($structAttrs['usage']) ? (string) $structAttrs['usage'] : '';
             // if we're not inheriting from our parent data element, but there is a struct usage, use the struct's usage (input, output, or inputoutput).
             if (!empty($structUsage) && $structUsage != 'inherit') {
                 $usage = $structUsage;
             }
             $structSubDataElementsXmlObj = $theStruct->xpath('data');
             if ($structSubDataElementsXmlObj) {
                 foreach ($structSubDataElementsXmlObj as $subDataElementXmlObj) {
                     if ($subDataElementXmlObj->attributes()->usage == 'inherit') {
                         // subdata is inheriting type from us. Give it to them.
                         $subDataElementXmlObj->attributes()->usage = $usage;
                     }
                     // here's where the recursion comes in. Convert data and add to array for our struct.
                     $subElements[] = $this->singlePcmlToArray($subDataElementXmlObj);
                 }
             }
         }
         $length = isset($attrs['length']) ? (string) $attrs['length'] : '';
         $precision = isset($attrs['precision']) ? (string) $attrs['precision'] : '';
         //$struct = (isset($attrs['struct'])) ? (string) $attrs['struct'] : ''; // if this is pointing to a struct name
         // find CW data type equivalent of PCML data type
         if (isset($this->_pcmlTypeMap[$type])) {
             // a simple type mapping
             $newType = (string) $this->_pcmlTypeMap[$type];
         } elseif ($type == 'int') {
             // one of the integer types. Need to use length to determine which one.
             if ($length == '2') {
                 $newType = I5_TYPE_SHORT;
             } elseif ($length == '4') {
                 $newType = I5_TYPE_INT;
             } else {
                 $newType = '';
                 // no match
             }
         } else {
             $newtype = '';
         }
         $newInout = isset($this->_pcmlInoutMap[$usage]) ? (string) $this->_pcmlInoutMap[$usage] : '';
         // create new length using precision if necessary
         if ($precision) {
             $newLength = "{$length}.{$precision}";
         } else {
             $newLength = $length;
         }
     }
     // count
     $newCount = 0;
     // initialize
     $newCountRef = '';
     if (is_numeric($count) && $count > 0) {
         $newCount = $count;
     } elseif (is_string($count) && !empty($count)) {
         // count is character, so it's really a countref
         $newCountRef = $count;
     }
     $element = array();
     $element[$nameName] = $name;
     // if not a struct, provide data type.
     if ($type != 'struct') {
         $element['Type'] = $newType;
     }
     if ($newCount) {
         $element['Count'] = $newCount;
     }
     if ($newCountRef) {
         $element['CountRef'] = $newCountRef;
     }
     if ($newLength) {
         $element['Length'] = $newLength;
     }
     if ($newInout) {
         $element['IO'] = $newInout;
     }
     if (count($subElements)) {
         $element['DSParm'] = $subElements;
     }
     return $element;
 }
 /**
  * @param \SimpleXmlElement $node
  * @param string $path
  * @return string
  */
 protected function getPathOfFileNodeToTarget($node, $path = '')
 {
     if ($node->getName() == 'target') {
         return $this->_getBasePathFromTargetName((string) $node['name']) . $path;
     }
     $path = '/' . $node['name'] . $path;
     $parent = $this->_getParentNode($node);
     return $this->_getPathOfFileNodeToTarget($parent, $path);
 }
 protected function parseResponse($response)
 {
     $xml = new SimpleXmlElement($response);
     $name = $xml->getName();
     if (strpos($name, 'ErrorResponse') !== false) {
         // ErrorResponse, ShipmentRatingErrorResponse, etc.
         return (string) $xml->Response->Status->Condition->ConditionData;
     }
     if ((string) $xml->Rated != 'Y') {
         return 'Shipment is not rated';
     }
     $services = $this->getServices();
     return array($this->product_code => array('id' => $this->product_code, 'currency' => (string) $xml->CurrencyCode, 'est_delivery' => '', 'name' => $services[$this->product_code], 'rate' => (string) $xml->ShippingCharge));
 }
Example #8
0
 /**
  * given a single ->data or ->struct element, return a parameter object in the new toolkit style.
  * 
  * @todo this needs more validation. It is possible that all parts are not set to create return
  * 
  * @param \SimpleXmlElement $dataElement
  * @return ProgramParameter
  */
 public function singlePcmlToParam(\SimpleXmlElement $dataElement)
 {
     $tagName = $dataElement->getName();
     // get attributes of this element.
     $attrs = $dataElement->attributes();
     // both struct and data have name, count (optional), usage
     $name = isset($attrs['name']) ? (string) $attrs['name'] : '';
     $count = isset($attrs['count']) ? (string) $attrs['count'] : '';
     $usage = isset($attrs['usage']) ? (string) $attrs['usage'] : '';
     $structName = isset($attrs['struct']) ? (string) $attrs['struct'] : '';
     // fill this if we have a struct
     $subElements = array();
     // each item should have tag name <data>
     if ($tagName != 'data') {
         return false;
     }
     $type = isset($attrs['type']) ? (string) $attrs['type'] : '';
     // Get initial value, if specified by PCML.
     $dataValue = isset($attrs['init']) ? (string) $attrs['init'] : '';
     // if a struct then we need to recurse.
     if ($type == 'struct') {
         $theStruct = null;
         // init
         // look for matching struct definition encountered earlier.
         if ($this->_pcmlStructs) {
             // @todo verify type with is_array and count
             foreach ($this->_pcmlStructs as $possibleStruct) {
                 $possStructAttrs = $possibleStruct->attributes();
                 if ($possStructAttrs['name'] == $structName) {
                     $theStruct = $possibleStruct;
                     $structAttrs = $possStructAttrs;
                     break;
                 }
             }
         }
         // if struct was not found, generate error for log
         if (!$theStruct) {
             // $this->getConnection->logThis("PCML structure '$structName' not found.");
             return null;
         }
         // count can also be defined at the structure level. If so, it will override count from data level)
         if (isset($structAttrs['count'])) {
             $count = (string) $structAttrs['count'];
         }
         // "usage" (in/out/inherit) can be defined here, at the structure level.
         $structUsage = isset($structAttrs['usage']) ? (string) $structAttrs['usage'] : '';
         // if we're not inheriting from our parent data element, but there is a struct usage, use the struct's usage (input, output, or inputoutput).
         if (!empty($structUsage) && $structUsage != 'inherit') {
             $usage = $structUsage;
         }
         $structSubDataElementsXmlObj = $theStruct->xpath('data');
         if ($structSubDataElementsXmlObj) {
             foreach ($structSubDataElementsXmlObj as $subDataElementXmlObj) {
                 if ($subDataElementXmlObj->attributes()->usage == 'inherit') {
                     // subdata is inheriting type from us. Give it to them.
                     $subDataElementXmlObj->attributes()->usage = $usage;
                 }
                 // here's where the recursion comes in. Convert data and add to array for our struct.
                 $subElements[] = $this->singlePcmlToParam($subDataElementXmlObj);
             }
         }
     }
     /* explanation of the terms "length" and "precision" in PCML:
      * http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/com.ibm.etools.iseries.webtools.doc/ref/rdtcattr.htm
      * 
      * For "int" values, length is the number of bytes; precision represents the number of bits. (Can be ignored here)
      * For zoned and packed values, length is the maximum number of digits; precision represents the maximum decimal places.
      * 
      */
     $length = isset($attrs['length']) ? (string) $attrs['length'] : '';
     $precision = isset($attrs['precision']) ? (string) $attrs['precision'] : '';
     $passBy = '';
     // default of blank will become 'ref'/Reference in XMLSERVICE. Blank is fine here.
     if (isset($attrs['passby']) && $attrs['passby'] == 'value') {
         $passBy = 'val';
         // rare. PCML calls it 'value'. XMLSERVICE calls it 'val'.
     }
     // find new toolkit equivalent of PCML data type
     if (isset($this->_pcmlTypeMap[$type])) {
         // a simple type mapping
         $newType = (string) $this->_pcmlTypeMap[$type];
     } elseif ($type == 'int') {
         // one of the integer types. Need to use length to determine which one.
         if ($length == '2') {
             $newType = '5i0';
             // short ints have two bytes
         } elseif ($length == '4') {
             $newType = '10i0';
             // normal ints have four bytes
         } else {
             $newType = '';
             // no match
         }
         //(length == 2, et al.)
     } else {
         $newType = '';
     }
     $newInout = isset($this->_pcmlInoutMap[$usage]) ? (string) $this->_pcmlInoutMap[$usage] : '';
     // @todo correct all this isArray business.
     // Can we manage without isArray?
     // well, it's already handled by toolkit....try and see, though.
     // poss. eliminate extra object levels, at least?
     if ($count > 1) {
         $isArray = true;
     } else {
         // no need for any dummy value.Could be 'init' from above, or leave the default.
         $isArray = false;
     }
     // @todo I think simply add 'counterLabel' and 'countedLabel'.
     // count
     $newCount = 0;
     // initialize
     // @todo deal with this. Really need a better way to find the counter data elements.
     // Allow a countref, too, in PCML??? Maybe! Count will be the dim (max) and countref is the actual name.
     // Some customers have done it wrong. Instead of specifying a field as count, gave max count.
     // "count can be a number where number defines a fixed, never-changing number of elements in a sized array.
     // OR a data-name where data-name defines the name of a <data> element within the PCML document that will contain, at runtime, the number of elements in the array. The data-name specified can be a fully qualified name or a name that is relative to the current element. In either case, the name must reference a <data> element that is defined with type="int". See Resolving Relative Names for more information about how relative names are resolved.
     // about finding the element: http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frzahh%2Flengthprecisionrelative.htm
     // Names are resolved by seeing if the name can be resolved as a child or descendent of the tag containing the current tag. If the name cannot be resolved at this level, the search continues with the next highest containing tag. This resolution must eventually result in a match of a tag that is contained by either the <pcml> tag or the <rfml> tag, in which case the name is considered to be an absolute name, not a relative name.""
     // Let's simply use $countersAndCounted. If necessary, pre-process PCML to create $countersAndCounted.
     if (is_numeric($count) && $count > 0) {
         $newCount = $count;
     }
     // $subElements are if this is a struct.
     if (count($subElements)) {
         $dataValue = $subElements;
     }
     $param = new ProgramParameter(sprintf($newType, $length, $precision), $newInout, '', $name, $dataValue, 'off', $newCount, $passBy, $isArray);
     if ($this->_countersAndCounted) {
         // some counters were configured
         // counter item reference was specified.
         if (isset($this->_countersAndCounted[$name])) {
             $param->setParamLabelCounter($name);
         }
         // counted item reference was specified as value in array.
         // look for value ($name). if found, counter is key.
         if ($counter = array_search($name, $this->_countersAndCounted)) {
             $param->setParamLabelCounted($counter);
         }
     }
     return $param;
 }