1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Analytics; |
4
|
|
|
|
5
|
|
|
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement; |
6
|
|
|
use Scriptotek\Alma\Client; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property \Generator|Row[] rows |
10
|
|
|
*/ |
11
|
|
|
class Report |
12
|
|
|
{ |
13
|
|
|
public $path; |
14
|
|
|
public $chunkSize = 1000; |
15
|
|
|
|
16
|
|
|
/** @var Client */ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
protected $headers = []; |
20
|
|
|
|
21
|
|
|
public function __construct(Client $client = null, $path = null, $headers = [], $filter = null) |
22
|
|
|
{ |
23
|
|
|
$this->path = $path; |
24
|
|
|
$this->client = $client; |
25
|
|
|
|
26
|
|
|
$this->headers = $headers; |
27
|
|
|
$this->filter = $filter; |
|
|
|
|
28
|
|
|
// $this->rows = new Rows($this->path, $this->client); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __get($key) |
32
|
|
|
{ |
33
|
|
|
if ($key == 'rows') { |
34
|
|
|
return $this->getRows(); |
35
|
|
|
} |
36
|
|
|
if ($key == 'headers') { |
37
|
|
|
return $this->headers; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function fetchRows($resumptionToken = null) |
42
|
|
|
{ |
43
|
|
|
$results = $this->client->getXML('/analytics/reports', [ |
44
|
|
|
'path' => $this->path, |
45
|
|
|
'limit' => $this->chunkSize, |
46
|
|
|
'token' => $resumptionToken, |
47
|
|
|
'filter' => $this->filter ? str_replace(['\''], ['''], $this->filter) : null, |
48
|
|
|
]); |
49
|
|
|
$results->registerXPathNamespaces([ |
50
|
|
|
'rowset' => 'urn:schemas-microsoft-com:xml-analysis:rowset', |
51
|
|
|
'xsd' => 'http://www.w3.org/2001/XMLSchema', |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
$this->readColumnHeaders($results); |
55
|
|
|
|
56
|
|
|
return $results; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Read column headers from response, and check that we got the right number of columns back. |
61
|
|
|
* |
62
|
|
|
* @param QuiteSimpleXMLElement $results |
63
|
|
|
*/ |
64
|
|
|
protected function readColumnHeaders(QuiteSimpleXMLElement $results) |
65
|
|
|
{ |
66
|
|
|
$headers = array_map(function (QuiteSimpleXMLElement $node) { |
67
|
|
|
return $node->attr('name'); |
68
|
|
|
}, $results->all('//xsd:complexType[@name="Row"]/xsd:sequence/xsd:element[position()>1]')); |
69
|
|
|
|
70
|
|
|
if (!count($headers)) { |
71
|
|
|
// No column headers included in this response. They're only |
72
|
|
|
// included in the first response, so that's probably fine. |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!count($this->headers)) { |
77
|
|
|
$this->headers = $headers; |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (count($headers) != count($this->headers)) { |
82
|
|
|
throw new \RuntimeException(sprintf('The number of returned columns (%d) does not match the number of assigned headers (%d).', |
83
|
|
|
count($headers), count($this->headers))); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \Generator|Row[] |
89
|
|
|
*/ |
90
|
|
|
public function getRows() |
91
|
|
|
{ |
92
|
|
|
$isFinished = false; |
93
|
|
|
$resumptionToken = null; |
94
|
|
|
|
95
|
|
|
while (!$isFinished) { |
96
|
|
|
$results = $this->fetchRows($resumptionToken); |
97
|
|
|
|
98
|
|
|
foreach ($results->all('//rowset:Row') as $row) { |
99
|
|
|
yield new Row($row, $this->headers); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$resumptionToken = $results->text('/report/QueryResult/ResumptionToken') ?: $resumptionToken; |
103
|
|
|
$isFinished = ($results->text('/report/QueryResult/IsFinished') == 'true'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: