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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
|
<?php
namespace EasyRdf;
/**
* EasyRdf
*
* LICENSE
*
* Copyright (c) 2009-2015 Nicholas J Humfrey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2015 Nicholas J Humfrey
* @license https://www.opensource.org/licenses/bsd-license.php
*/
/**
* Class that represents an RDF resource
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2015 Nicholas J Humfrey
* @license https://www.opensource.org/licenses/bsd-license.php
*/
class Resource implements \ArrayAccess
{
/** The URI for this resource */
protected $uri = null;
/** The Graph that this resource belongs to */
protected $graph = null;
/** Constructor
*
* * Please do not call new EasyRdf\Resource() directly *
*
* To create a new resource use the get method in a graph:
* $resource = $graph->resource('http://www.example.com/');
*
*/
public function __construct($uri, $graph = null)
{
if (!is_string($uri) or $uri == null or $uri == '') {
throw new \InvalidArgumentException(
"\$uri should be a string and cannot be null or empty"
);
}
$this->uri = $uri;
// Check that $graph is an EasyRdf\Graph object
if (is_object($graph) and $graph instanceof Graph) {
$this->graph = $graph;
} elseif (!is_null($graph)) {
throw new \InvalidArgumentException(
'$graph should be an EasyRdf\Graph object'
);
}
}
/**
* Return the graph that this resource belongs to
*
* @return Graph
*/
public function getGraph()
{
return $this->graph;
}
/** Returns the URI for the resource.
*
* @return string URI of this resource.
*/
public function getUri()
{
return $this->uri;
}
/** Check to see if a resource is a blank node.
*
* @return bool True if this resource is a blank node.
*/
public function isBNode()
{
if (substr($this->uri, 0, 2) == '_:') {
return true;
} else {
return false;
}
}
/** Get the identifier for a blank node
*
* Returns null if the resource is not a blank node.
*
* @return string The identifer for the bnode
*/
public function getBNodeId()
{
if (substr($this->uri, 0, 2) == '_:') {
return substr($this->uri, 2);
} else {
return null;
}
}
/** Get a the prefix of the namespace that this resource is part of
*
* This method will return null the resource isn't part of any
* registered namespace.
*
* @return string The namespace prefix of the resource (e.g. foaf)
*/
public function prefix()
{
return RdfNamespace::prefixOfUri($this->uri);
}
/** Get a shortened version of the resources URI.
*
* This method will return the full URI if the resource isn't part of any
* registered namespace.
*
* @return string The shortened URI of this resource (e.g. foaf:name)
*/
public function shorten()
{
return RdfNamespace::shorten($this->uri);
}
/** Gets the local name of the URI of this resource
*
* The local name is defined as the part of the URI string
* after the last occurrence of the '#', ':' or '/' character.
*
* @return string The local name
*/
public function localName()
{
if (preg_match("|([^#:/]+)$|", $this->uri, $matches)) {
return $matches[1];
}
}
/** Parse the URI of the resource and return as a ParsedUri object
*
* @return ParsedUri
*/
public function parseUri()
{
return new ParsedUri($this->uri);
}
/** Generates an HTML anchor tag, linking to this resource.
*
* If no text is given, then the URI also uses as the link text.
*
* @param string $text Text for the link.
* @param array $options Associative array of attributes for the anchor tag
*
* @throws \InvalidArgumentException
* @return string The HTML link string
*/
public function htmlLink($text = null, $options = array())
{
$options = array_merge(array('href' => $this->uri), $options);
if ($text === null) {
$text = $this->uri;
}
$html = "<a";
foreach ($options as $key => $value) {
if (!preg_match('/^[-\w]+$/', $key)) {
throw new \InvalidArgumentException(
"\$options should use valid attribute names as keys"
);
}
$html .= " ".htmlspecialchars($key)."=\"".
htmlspecialchars($value)."\"";
}
$html .= ">".htmlspecialchars($text)."</a>";
return $html;
}
/** Returns the properties of the resource as an RDF/PHP associative array
*
* For example:
* array('type' => 'uri', 'value' => 'http://www.example.com/')
*
* @return array The properties of the resource
*/
public function toRdfPhp()
{
if ($this->isBNode()) {
return array('type' => 'bnode', 'value' => $this->uri);
} else {
return array('type' => 'uri', 'value' => $this->uri);
}
}
/** Return pretty-print view of the resource
*
* @param string $format Either 'html' or 'text'
* @param string $color The colour of the text
*
* @return string
*/
public function dumpValue($format = 'html', $color = 'blue')
{
return Utils::dumpResourceValue($this, $format, $color);
}
/** Magic method to return URI of resource when casted to string
*
* @return string The URI of the resource
*/
public function __toString()
{
return $this->uri;
}
/** Throw can exception if the resource does not belong to a graph
* @ignore
*/
protected function checkHasGraph()
{
if (!$this->graph) {
throw new Exception(
'EasyRdf\Resource is not part of a graph.'
);
}
}
/** Perform a load (download of remote URI) of the resource into the graph
*
* The document type is optional but should be specified if it
* can't be guessed or got from the HTTP headers.
*
* @param string $format Optional format of the data (eg. rdfxml)
*
* @return integer
*/
public function load($format = null)
{
$this->checkHasGraph();
return $this->graph->load($this->uri, $format);
}
/** Delete a property (or optionally just a specific value)
*
* @param string $property The name of the property (e.g. foaf:name)
* @param object $value The value to delete (null to delete all values)
*
* @return integer
*/
public function delete($property, $value = null)
{
$this->checkHasGraph();
return $this->graph->delete($this->uri, $property, $value);
}
/** Add values to for a property of the resource
*
* Example:
* $resource->add('prefix:property', 'value');
*
* @param mixed $property The property name
* @param mixed $value The value for the property
*
* @return integer The number of values added (1 or 0)
*/
public function add($property, $value)
{
$this->checkHasGraph();
return $this->graph->add($this->uri, $property, $value);
}
/** Add a literal value as a property of the resource
*
* The value can either be a single value or an array of values.
*
* Example:
* $resource->add('dc:title', 'Title of Page');
*
* @param mixed $property The property name
* @param mixed $values The value or values for the property
* @param string $lang The language of the literal
*
* @return integer The number of values added
*/
public function addLiteral($property, $values, $lang = null)
{
$this->checkHasGraph();
return $this->graph->addLiteral($this->uri, $property, $values, $lang);
}
/** Add a resource as a property of the resource
*
* Example:
* $bob->add('foaf:knows', 'http://example.com/alice');
*
* @param mixed $property The property name
* @param mixed $resource2 The resource to be the value of the property
*
* @return integer The number of values added (1 or 0)
*/
public function addResource($property, $resource2)
{
$this->checkHasGraph();
return $this->graph->addResource($this->uri, $property, $resource2);
}
/** Set value for a property
*
* The new value(s) will replace the existing values for the property.
* The name of the property should be a string.
* If you set a property to null or an empty array, then the property
* will be deleted.
*
* @param string $property The name of the property (e.g. foaf:name)
* @param mixed $value The value for the property.
*
* @return integer The number of values added (1 or 0)
*/
public function set($property, $value)
{
$this->checkHasGraph();
return $this->graph->set($this->uri, $property, $value);
}
/** Get a single value for a property
*
* If multiple values are set for a property then the value returned
* may be arbitrary.
*
* If $property is an array, then the first item in the array that matches
* a property that exists is returned.
*
* This method will return null if the property does not exist.
*
* @param string|array $property The name of the property (e.g. foaf:name)
* @param string $type The type of value to filter by (e.g. literal or resource)
* @param string $lang The language to filter by (e.g. en)
*
* @return mixed A value associated with the property
*/
public function get($property, $type = null, $lang = null)
{
$this->checkHasGraph();
return $this->graph->get($this->uri, $property, $type, $lang);
}
/** Get a single literal value for a property of the resource
*
* If multiple values are set for a property then the value returned
* may be arbitrary.
*
* This method will return null if there is not literal value for the
* property.
*
* @param string|array $property The name of the property (e.g. foaf:name)
* @param string $lang The language to filter by (e.g. en)
*
* @return Literal Literal value associated with the property
*/
public function getLiteral($property, $lang = null)
{
$this->checkHasGraph();
return $this->graph->get($this->uri, $property, 'literal', $lang);
}
/** Get a single resource value for a property of the resource
*
* If multiple values are set for a property then the value returned
* may be arbitrary.
*
* This method will return null if there is not resource for the
* property.
*
* @param string|array $property The name of the property (e.g. foaf:name)
*
* @return self Resource associated with the property
*/
public function getResource($property)
{
$this->checkHasGraph();
return $this->graph->get($this->uri, $property, 'resource');
}
/** Get all values for a property
*
* This method will return an empty array if the property does not exist.
*
* @param string $property The name of the property (e.g. foaf:name)
* @param string $type The type of value to filter by (e.g. literal)
* @param string $lang The language to filter by (e.g. en)
*
* @return array An array of values associated with the property
*/
public function all($property, $type = null, $lang = null)
{
$this->checkHasGraph();
return $this->graph->all($this->uri, $property, $type, $lang);
}
/** Get all literal values for a property of the resource
*
* This method will return an empty array if the resource does not
* has any literal values for that property.
*
* @param string $property The name of the property (e.g. foaf:name)
* @param string $lang The language to filter by (e.g. en)
*
* @return array An array of values associated with the property
*/
public function allLiterals($property, $lang = null)
{
$this->checkHasGraph();
return $this->graph->all($this->uri, $property, 'literal', $lang);
}
/** Get all resources for a property of the resource
*
* This method will return an empty array if the resource does not
* has any resources for that property.
*
* @param string $property The name of the property (e.g. foaf:name)
*
* @return array An array of values associated with the property
*/
public function allResources($property)
{
$this->checkHasGraph();
return $this->graph->all($this->uri, $property, 'resource');
}
/** Count the number of values for a property of a resource
*
* This method will return 0 if the property does not exist.
*
* @param string $property The name of the property (e.g. foaf:name)
* @param string $type The type of value to filter by (e.g. literal)
* @param string $lang The language to filter by (e.g. en)
*
* @return integer The number of values associated with the property
*/
public function countValues($property, $type = null, $lang = null)
{
$this->checkHasGraph();
return $this->graph->countValues($this->uri, $property, $type, $lang);
}
/** Concatenate all values for a property into a string.
*
* The default is to join the values together with a space character.
* This method will return an empty string if the property does not exist.
*
* @param string $property The name of the property (e.g. foaf:name)
* @param string $glue The string to glue the values together with.
* @param string $lang The language to filter by (e.g. en)
*
* @return string Concatenation of all the values.
*/
public function join($property, $glue = ' ', $lang = null)
{
$this->checkHasGraph();
return $this->graph->join($this->uri, $property, $glue, $lang);
}
/** Get a list of the full URIs for the properties of this resource.
*
* This method will return an empty array if the resource has no properties.
*
* @return array Array of full URIs
*/
public function propertyUris()
{
$this->checkHasGraph();
return $this->graph->propertyUris($this->uri);
}
/** Get a list of all the shortened property names (qnames) for a resource.
*
* This method will return an empty array if the resource has no properties.
*
* @return array Array of shortened URIs
*/
public function properties()
{
$this->checkHasGraph();
return $this->graph->properties($this->uri);
}
/** Get a list of the full URIs for the properties that point to this resource.
*
* @return array Array of full property URIs
*/
public function reversePropertyUris()
{
$this->checkHasGraph();
return $this->graph->reversePropertyUris($this->uri);
}
/** Check to see if a property exists for this resource.
*
* This method will return true if the property exists.
* If the value parameter is given, then it will only return true
* if the value also exists for that property.
*
* @param string $property The name of the property (e.g. foaf:name)
* @param mixed $value An optional value of the property
*
* @return bool True if value the property exists.
*/
public function hasProperty($property, $value = null)
{
$this->checkHasGraph();
return $this->graph->hasProperty($this->uri, $property, $value);
}
/** Get a list of types for a resource.
*
* The types will each be a shortened URI as a string.
* This method will return an empty array if the resource has no types.
*
* @return array All types assocated with the resource (e.g. foaf:Person)
*/
public function types()
{
$this->checkHasGraph();
return $this->graph->types($this->uri);
}
/** Get a single type for a resource.
*
* The type will be a shortened URI as a string.
* If the resource has multiple types then the type returned
* may be arbitrary.
* This method will return null if the resource has no type.
*
* @return string A type assocated with the resource (e.g. foaf:Person)
*/
public function type()
{
$this->checkHasGraph();
return $this->graph->type($this->uri);
}
/** Get a single type for a resource, as a resource.
*
* The type will be returned as an EasyRdf\Resource.
* If the resource has multiple types then the type returned
* may be arbitrary.
* This method will return null if the resource has no type.
*
* @return Resource A type assocated with the resource.
*/
public function typeAsResource()
{
$this->checkHasGraph();
return $this->graph->typeAsResource($this->uri);
}
/**
* Get a list of types for a resource, as EasyRdf\Resource
*
* @return Resource[]
* @throws Exception
*/
public function typesAsResources()
{
$this->checkHasGraph();
return $this->graph->typesAsResources($this->uri);
}
/** Check if a resource is of the specified type
*
* @param string $type The type to check (e.g. foaf:Person)
*
* @return boolean True if resource is of specified type.
*/
public function isA($type)
{
$this->checkHasGraph();
return $this->graph->isA($this->uri, $type);
}
/** Add one or more rdf:type properties to the resource
*
* @param string $types One or more types to add (e.g. foaf:Person)
*
* @return integer The number of types added
*/
public function addType($types)
{
$this->checkHasGraph();
return $this->graph->addType($this->uri, $types);
}
/** Change the rdf:type property for the resource
*
* Note that the PHP class of the resource will not change.
*
* @param string $type The new type (e.g. foaf:Person)
*
* @return integer The number of types added
*/
public function setType($type)
{
$this->checkHasGraph();
return $this->graph->setType($this->uri, $type);
}
/** Get the primary topic of this resource.
*
* Returns null if no primary topic is available.
*
* @return Resource The primary topic of this resource.
*/
public function primaryTopic()
{
$this->checkHasGraph();
return $this->graph->primaryTopic($this->uri);
}
/** Get a human readable label for this resource
*
* This method will check a number of properties for the resource
* (in the order: skos:prefLabel, rdfs:label, foaf:name, dc:title)
* and return an approriate first that is available. If no label
* is available then it will return null.
*
* @param string|null $lang
*
* @return string A label for the resource.
*/
public function label($lang = null)
{
$this->checkHasGraph();
return $this->graph->label($this->uri, $lang);
}
/** Return a human readable view of the resource and its properties
*
* This method is intended to be a debugging aid and will
* print a resource and its properties.
*
* @param string $format Either 'html' or 'text'
*
* @return string
*/
public function dump($format = 'html')
{
$this->checkHasGraph();
return $this->graph->dumpResource($this->uri, $format);
}
/** Magic method to get a property of a resource
*
* Note that only properties in the default namespace can be accessed in this way.
*
* Example:
* $value = $resource->title;
*
* @see EasyRdf\RdfNamespace::setDefault()
*
* @param string $name The name of the property
*
* @return string A single value for the named property
*/
public function __get($name)
{
return $this->graph->get($this->uri, $name);
}
/** Magic method to set the value for a property of a resource
*
* Note that only properties in the default namespace can be accessed in this way.
*
* Example:
* $resource->title = 'Title';
*
* @see EasyRdf\RdfNamespace::setDefault()
*
* @param string $name The name of the property
* @param string $value The value for the property
*
* @return int
*/
public function __set($name, $value)
{
return $this->graph->set($this->uri, $name, $value);
}
/** Magic method to check if a property exists
*
* Note that only properties in the default namespace can be accessed in this way.
*
* Example:
* if (isset($resource->title)) { blah(); }
*
* @see EasyRdf\RdfNamespace::setDefault()
*
* @param string $name The name of the property
*
* @return bool
*/
public function __isset($name)
{
return $this->graph->hasProperty($this->uri, $name);
}
/** Magic method to delete a property of the resource
*
* Note that only properties in the default namespace can be accessed in this way.
*
* Example:
* unset($resource->title);
*
* @see EasyRdf\RdfNamespace::setDefault()
*
* @param string $name The name of the property
*
* @return int
*/
public function __unset($name)
{
return $this->graph->delete($this->uri, $name);
}
/**
* Whether a offset exists
*
* The return value will be casted to boolean if non-boolean was returned.
*
* Example:
* if(isset($resource['rdfs:label'])) { }
*
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param mixed $offset An offset to check for.
*
* @return boolean true on success or false on failure.
*/
public function offsetExists($offset)
{
return $this->__isset($offset);
}
/**
* Offset to retrieve
*
* Example:
* $label = $resource['rdfs:label'];
*
* @link http://php.net/manual/en/arrayaccess.offsetget.php
*
* @param mixed $offset The offset to retrieve.
*
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
return $this->__get($offset);
}
/**
* Offset to set
*
* Example:
* $resource['rdfs:label'] = 'label';
*
* @link http://php.net/manual/en/arrayaccess.offsetset.php
*
* @param mixed The offset to assign the value to.
* @param mixed $value The value to set.
*
* @return void
*/
public function offsetSet($offset, $value)
{
$this->__set($offset, $value);
}
/**
* Offset to unset
*
* Example:
* unset($resource['rdfs:label']);
*
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
*
* @param mixed $offset The offset to unset.
*
* @return void
*/
public function offsetUnset($offset)
{
$this->__unset($offset);
}
}
|