Exemplo n.º 1
0
 /**
  * @see \Saft\Node
  */
 public function equals(Node $toCompare)
 {
     // Only compare, if given instance is a literal
     if ($toCompare->isLiteral() && $this->getDatatype()->equals($toCompare->getDatatype())) {
         return $this->getValue() === $toCompare->getValue() && $this->getLanguage() == $toCompare->getLanguage();
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * Returns an array representation of the given Node structure.
  *
  * @param Node $node
  *
  * @return array
  */
 public function toArray(Node $node)
 {
     return array('name' => $node->getName(), 'value' => $node->getValue(), 'attributes' => $node->getAttributes(), 'children' => array_map(function (Node $child) {
         return $this->toArray($child);
     }, $node->getChildren()));
 }
Exemplo n.º 3
0
 /**
  * A literal matches only another literal if its value, datatype and language are equal.
  *
  * @param  Node    $toMatch Node instance to apply the pattern on
  * @return boolean true, if this pattern matches the node, false otherwise
  * @todo check if that could be deleted
  */
 public function matches(Node $toMatch)
 {
     if ($toMatch->isConcrete()) {
         if ($toMatch instanceof Literal) {
             return $this->getValue() === $toMatch->getValue() && $this->getDatatype() === $toMatch->getDatatype() && $this->getLanguage() === $toMatch->getLanguage();
         }
         return false;
     } else {
         throw new \Exception('The node to match has to be a concrete node');
     }
 }
Exemplo n.º 4
0
 /**
  * Write an individual type to the stream.
  *
  * @param resource $fPtr    Stream pointer
  * @param int      $tagType Type of tag to write
  * @param Node     $node    Node containing value to write
  *
  * @return bool
  */
 private function writeType($fPtr, $tagType, Node $node)
 {
     switch ($tagType) {
         case Tag::TAG_BYTE:
             // Signed byte (8 bit)
             return $this->dataHandler->putTAGByte($fPtr, $node->getValue());
         case Tag::TAG_SHORT:
             // Signed short (16 bit, big endian)
             return $this->dataHandler->putTAGShort($fPtr, $node->getValue());
         case Tag::TAG_INT:
             // Signed integer (32 bit, big endian)
             return $this->dataHandler->putTAGInt($fPtr, $node->getValue());
         case Tag::TAG_LONG:
             // Signed long (64 bit, big endian)
             return $this->dataHandler->putTAGLong($fPtr, $node->getValue());
         case Tag::TAG_FLOAT:
             // Floating point value (32 bit, big endian, IEEE 754-2008)
             return $this->dataHandler->putTAGFloat($fPtr, $node->getValue());
         case Tag::TAG_DOUBLE:
             // Double value (64 bit, big endian, IEEE 754-2008)
             return $this->dataHandler->putTAGDouble($fPtr, $node->getValue());
         case Tag::TAG_BYTE_ARRAY:
             // Byte array
             return $this->dataHandler->putTAGByteArray($fPtr, $node->getValue());
         case Tag::TAG_STRING:
             // String
             return $this->dataHandler->putTAGString($fPtr, $node->getValue());
         case Tag::TAG_INT_ARRAY:
             // Byte array
             return $this->dataHandler->putTAGIntArray($fPtr, $node->getValue());
         case Tag::TAG_LIST:
             // List
             if (!($this->dataHandler->putTAGByte($fPtr, $node->getPayloadType()) && $this->dataHandler->putTAGInt($fPtr, count($node->getChildren())))) {
                 return false;
             }
             foreach ($node->getChildren() as $childNode) {
                 if (!$this->writeType($fPtr, $node->getPayloadType(), $childNode)) {
                     return false;
                 }
             }
             return true;
         case Tag::TAG_COMPOUND:
             // Compound
             foreach ($node->getChildren() as $childNode) {
                 if (!$this->writeTag($fPtr, $childNode)) {
                     return false;
                 }
             }
             if (!$this->writeType($fPtr, Tag::TAG_END, new Node())) {
                 return false;
             }
             return true;
         case Tag::TAG_END:
             // End tag
             return is_int(fwrite($fPtr, ""));
     }
 }