-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.php
170 lines (159 loc) · 4.75 KB
/
Node.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace librdf;
/**
* Node, a node or arc in an RDF graph.
*
* A Node is the type of the {@link Statement} triples.
*
* PHP version 5
*
* Copyright (C) 2006, David Shea <[email protected]>
*
* LICENSE: This package is Free Software and a derivative work of Redland
* http://librdf.org/. This package is not endorsed by Dave Beckett or the
* University of Bristol. It is licensed under the following three licenses as
* alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of the
* above three licenses.
*
* See LICENSE.txt at the top of this package for the complete terms and futher
* detail along with the license tests for the licenses in COPYING.LIB, COPYING
* and LICENSE-2.0.txt repectively.
*
* @package LibRDF
* @author David Shea <[email protected]>
* @copyright 2006 David Shea
* @license LGPL/GPL/APACHE
* @version Release: 1.0.0
* @link http://reallylongword.org/projects/librdf-php/
*/
/**
*/
use librdf\exception\Error;
use librdf\node\BlankNode;
use librdf\node\LiteralNode;
use librdf\node\URINode;
/**
* A wrapper around the node datatype.
*
* The values of nodes come from three potential, disjoint sets: URIs,
* literal strings and blank identifiers. These types are represented by
* {@link URINode}, {@link LiteralNode} and
* {@link BlankNode}, respectively.
*
* @package LibRDF
* @author David Shea <[email protected]>
* @copyright 2006 David Shea
* @license LGPL/GPL/APACHE
* @version Release: 1.0.0
* @link http://reallylongword.org/projects/librdf-php/
*/
abstract class Node
{
/**
* The underlying node resource.
*
* This value must be set by the constructors for the concrete node types.
*
* @var resource
* @access protected
*/
protected $node;
/**
* Destroy the Node object.
*
* @return void
* @access public
*/
public function __destruct()
{
if ($this->node) {
librdf_free_node($this->node);
}
}
/**
* Create a new node object from an existing node.
*
* @return void
* @throws Error If unable to copy the node
* @access public
*/
public function __clone()
{
$this->node = librdf_new_node_from_node($this->node);
if (!$this->node) {
throw new Error("Unable to create new Node from Node");
}
}
/**
* Return a string representation of the node.
*
* @return string A string representation of the node
* @access public
*/
public function __toString()
{
return librdf_node_to_string($this->node);
}
/**
* Compare this node with another node for equality.
*
* Nodes of different types are not equal; thus, a URI of
* http://example.org/ and a literal string of http://example.org are not
* equal, even though they contain the same string. Similarly, literal
* nodes must match in both type and language to be considered equal.
*
* @param Node $node The node against which to compare
* @return boolean Whether the nodes are equal
* @access public
*/
public function isEqual(Node $node)
{
return (boolean) librdf_node_equals($this->node, $node->node);
}
/**
* Return the underlying node resource.
*
* This function is intended for other LibRDF classes and should not
* be called.
*
* @return resource The wrapped node
* @access public
*/
public function getNode()
{
return $this->node;
}
/**
* Wrap a node resource in the correct Node object.
*
* This function is intended for use by LibRDF classes, allowing them
* to easily convert a node resource into the correct type of
* Node object.
*
* @param resource $node The node to convert
* @return Node A concrete object implementing Node
* @throws Error If unable to create a new node
* @access public
* @static
*/
public static function makeNode($node)
{
if (!is_resource($node)) {
throw new Error("Argument must be a node resource");
}
if (librdf_node_is_resource($node)) {
return new URINode($node);
} elseif (librdf_node_is_literal($node)) {
return new LiteralNode($node);
} elseif (librdf_node_is_blank($node)) {
return new BlankNode($node);
} else {
throw new Error("Unknown query results type");
}
}
}