1<?php
2
3class phpSesame_SparqlRes
4{
5	private $xml;
6
7	function __construct($responseBody)
8	{
9		$this->xml = simplexml_load_string($responseBody);
10	}
11
12	public function getHeaders()
13	{
14		$headers = array();
15		foreach($this->xml->head->variable as $header)
16		{
17			$headers[] = (string) $header['name'];
18		}
19
20		return $headers;
21	}
22
23	/**
24	 * Returns the rows in an array format, false if there are no rows.
25	 *
26	 * @return	mixed
27	 */
28	public function getRows()
29	{
30		$rows = array();
31		foreach($this->xml->results->result as $result)
32		{
33			//print_r($result);
34			$row = array();
35			foreach($result->binding as $binding)
36			{
37				$name = (string) $binding['name'];
38				if($binding->literal)
39				{
40					$row[$name] = (string) $binding->literal;
41					$row[$name . '_type'] = 'literal';
42				}
43				else if($binding->uri)
44				{
45					$row[$name] = (string) $binding->uri;
46					$row[$name . '_type'] = 'uri';
47				}
48				else if($binding->bnode)
49				{
50					$row[$name] = (string) $binding->bnode;
51					$row[$name . '_type'] = 'bnode';
52				}
53			}
54			$rows[] = $row;
55		}
56
57		if(sizeof($rows) <= 0)
58		{
59			return false;
60		}
61
62		return $rows;
63	}
64
65
66	/**
67	 * Returns whether the result contains any rows
68	 *
69	 * @return	bool
70	 */
71	public function hasRows()
72	{
73		//return true;
74		//print_r($this->xml->results);
75		foreach($this->xml->results->result as $result)
76		{
77			foreach($result->binding as $binding) {
78			//print_r($result->binding);
79			if ((string) $binding->literal != "") return true;
80			//if ((string) $result->binding->uri != "") return true;
81			//if ((string) $result->binding->bnode != "") return true;
82			}
83		}
84
85		return false;
86	}
87}
88?>
89