ARC2_Resource.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * ARC2 Resource object
  4. *
  5. * @author Benjamin Nowack <bnowack@semsol.com>
  6. * @license http://arc.semsol.org/license
  7. * @homepage <http://arc.semsol.org/>
  8. * @package ARC2
  9. * @version 2010-11-16
  10. */
  11. ARC2::inc('Class');
  12. class ARC2_Resource extends ARC2_Class {
  13. function __construct($a, &$caller) {
  14. parent::__construct($a, $caller);
  15. }
  16. function __init() {
  17. parent::__init();
  18. $this->uri = '';
  19. $this->index = array();
  20. $this->fetched = array();
  21. $this->store = '';
  22. }
  23. /* */
  24. function setURI($uri) {
  25. $this->uri = $uri;
  26. }
  27. function setIndex($index) {
  28. $this->index = $index;
  29. }
  30. function setProps($props, $s = '') {
  31. if (!$s) $s = $this->uri;
  32. $this->index[$s] = $props;
  33. }
  34. function setProp($p, $os, $s = '') {
  35. if (!$s) $s = $this->uri;
  36. /* single plain value */
  37. if (!is_array($os)) $os = array('value' => $os, 'type' => 'literal');
  38. /* single array value */
  39. if (isset($os['value'])) $os = array($os);
  40. /* list of values */
  41. foreach ($os as $i => $o) {
  42. if (!is_array($o)) $os[$i] = array('value' => $o, 'type' => 'literal');
  43. }
  44. $this->index[$s][$this->expandPName($p)] = $os;
  45. }
  46. function setStore($store) {
  47. $this->store = $store;
  48. }
  49. /* */
  50. function fetchData($uri = '') {
  51. if (!$uri) $uri = $this->uri;
  52. if (!$uri) return 0;
  53. if (in_array($uri, $this->fetched)) return 0;
  54. $this->index[$uri] = array();
  55. if ($this->store) {
  56. $index = $this->store->query('CONSTRUCT { <' . $uri . '> ?p ?o . } WHERE { <' . $uri . '> ?p ?o . } ', 'raw');
  57. }
  58. else {
  59. $index = $this->toIndex($uri);
  60. }
  61. $this->index = ARC2::getMergedIndex($this->index, $index);
  62. $this->fetched[] = $uri;
  63. }
  64. /* */
  65. function getProps($p = '', $s = '') {
  66. if (!$s) $s = $this->uri;
  67. if (!$s) return array();
  68. if (!isset($this->index[$s])) $this->fetchData($s);
  69. if (!$p) return $this->index[$s];
  70. return $this->v($this->expandPName($p), array(), $this->index[$s]);
  71. }
  72. function getProp($p, $s = '') {
  73. $props = $this->getProps($p, $s);
  74. return $props ? $props[0] : '';
  75. }
  76. function getPropValue($p, $s = '') {
  77. $prop = $this->getProp($p, $s);
  78. return $prop ? $prop['value'] : '';
  79. }
  80. function getPropValues($p, $s = '') {
  81. $r = array();
  82. $props = $this->getProps($p, $s);
  83. foreach ($props as $prop) {
  84. $r[] = $prop['value'];
  85. }
  86. return $r;
  87. }
  88. function hasPropValue($p, $o, $s = '') {
  89. $props = $this->getProps($p, $s);
  90. $o = $this->expandPName($o);
  91. foreach ($props as $prop) {
  92. if ($prop['value'] == $o) return 1;
  93. }
  94. return 0;
  95. }
  96. /* */
  97. }