/**
  * Recursivly serialize data to the given node.
  *
  * Scalar values are natively supported by the protocol, so we just encode
  * them as the spec tells us. As arrays and structs / hashes are the same
  * in PHP, and structs are the more powerful construct, we're always encoding 
  * arrays as structs.
  * 
  * XP objects are encoded as structs, having their FQDN stored in the member
  * __xp_class.
  *
  * @param   xml.Node node
  * @param   var data
  * @throws  lang.IllegalArgumentException in case the data could not be serialized.
  */
 protected function _marshall($data)
 {
     $value = new Node('value');
     // Handle objects:
     // - util.Date objects are serialized as dateTime.iso8601
     // - lang.types.Bytes object are serialized as base64
     // - Provide a standard-way to serialize Object-derived classes
     if ($data instanceof Date) {
         $value->addChild(new Node('dateTime.iso8601', $data->toString('Ymd\\TH:i:s')));
         return $value;
     } else {
         if ($data instanceof Bytes || $data instanceof \lang\types\Bytes) {
             $value->addChild(new Node('base64', base64_encode($data)));
             return $value;
         } else {
             if ($data instanceof \lang\Generic) {
                 $n = $value->addChild(new Node('struct'));
                 $n->addChild(Node::fromArray(['name' => '__xp_class', 'value' => ['string' => nameof($data)]], 'member'));
                 foreach ($data->getClass()->getFields() as $field) {
                     if ($field->getModifiers() & MODIFIER_STATIC) {
                         continue;
                     }
                     $member = $n->addChild(new Node('member'));
                     $member->addChild(new Node('name', $field->getName()));
                     $member->addChild($this->_marshall($field->setAccessible(true)->get($data)));
                 }
                 return $value;
             }
         }
     }
     switch (\xp::typeOf($data)) {
         case 'integer':
             $value->addChild(new Node('int', $data));
             break;
         case 'boolean':
             $value->addChild(new Node('boolean', (string) (int) $data));
             break;
         case 'double':
         case 'float':
             $value->addChild(new Node('double', $data));
             break;
         case 'array':
             if ($this->_isVector($data)) {
                 $n = $value->addChild(new Node('array'))->addChild(new Node('data'));
                 for ($i = 0, $s = sizeof($data); $i < $s; $i++) {
                     $n->addChild($this->_marshall($data[$i]));
                 }
             } else {
                 $n = $value->addChild(new Node('struct'));
                 foreach ($data as $name => $v) {
                     $member = $n->addChild(new Node('member'));
                     $member->addChild(new Node('name', $name));
                     $member->addChild($this->_marshall($v));
                 }
             }
             break;
         case 'string':
             $value->addChild(new Node('string', $data));
             break;
         case 'NULL':
             $value->addChild(new Node('nil'));
             break;
         default:
             throw new \lang\IllegalArgumentException('Cannot serialize data of type "' . \xp::typeOf($data) . '"');
     }
     return $value;
 }
 /**
  * Called when a test run finishes.
  *
  * @param   unittest.TestSuite suite
  * @param   unittest.TestResult result
  */
 public function testRunFinished(\unittest\TestSuite $suite, TestResult $result)
 {
     $coverage = xdebug_get_code_coverage();
     xdebug_stop_code_coverage();
     $results = array();
     foreach ($coverage as $fileName => $data) {
         foreach ($this->paths as $path) {
             if (substr($fileName, 0, strlen($path)) !== $path) {
                 continue;
             }
             $results[dirname($fileName)][basename($fileName)] = $data;
             break;
         }
     }
     $pathsNode = new Node('paths');
     foreach ($results as $pathName => $files) {
         $pathNode = new Node('path');
         $pathNode->setAttribute('name', $pathName);
         foreach ($files as $fileName => $data) {
             $fileNode = new Node('file');
             $fileNode->setAttribute('name', $fileName);
             $num = 1;
             $reader = new TextReader(new FileInputStream($pathName . '/' . $fileName));
             while (($line = $reader->readLine()) !== null) {
                 $lineNode = new Node('line', new CData($line));
                 if (isset($data[$num])) {
                     if (1 === $data[$num]) {
                         $lineNode->setAttribute('checked', 'checked');
                     } elseif (-1 === $data[$num]) {
                         $lineNode->setAttribute('unchecked', 'unchecked');
                     }
                 }
                 $fileNode->addChild($lineNode);
                 ++$num;
             }
             $pathNode->addChild($fileNode);
         }
         $pathsNode->addChild($pathNode);
     }
     $pathsNode->setAttribute('time', date('Y-m-d H:i:s'));
     $this->processor->setXMLBuf($pathsNode->getSource());
     $this->processor->run();
     FileUtil::setContents(new File($this->reportFile), $this->processor->output());
 }
Exemple #3
0
 /**
  * @param  string $nodeName
  * @return Node
  */
 public final function toNode($nodeName)
 {
     $node = new Node($nodeName);
     foreach ($this->toArray() as $group => $keyList) {
         $groupNode = $node->addChild(new Node($group));
         foreach ($keyList as $key => $value) {
             $groupNode->addChild(new Node($key))->setContent($value);
         }
     }
     return $node;
 }
 /**
  * @param  string $name
  * @param  string $value
  * @return AbstractSitePage this
  */
 public final function setFormData($name, $value)
 {
     $this->formData->addChild(new Node($name))->setContent($value);
     return $this;
 }