|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Analytics; |
|
4
|
|
|
|
|
5
|
|
|
use Scriptotek\Alma\Client; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @property \Generator|Row[] rows |
|
9
|
|
|
*/ |
|
10
|
|
|
class Report |
|
11
|
|
|
{ |
|
12
|
|
|
public $path; |
|
13
|
|
|
public $chunkSize = 1000; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Client */ |
|
16
|
|
|
protected $client; |
|
17
|
|
|
|
|
18
|
|
|
protected $headers = []; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(Client $client = null, $path = null, $headers = [], $filter = null) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->path = $path; |
|
23
|
|
|
$this->client = $client; |
|
24
|
|
|
|
|
25
|
|
|
$this->headers = $headers; |
|
26
|
|
|
$this->filter = $filter; |
|
|
|
|
|
|
27
|
|
|
// $this->rows = new Rows($this->path, $this->client); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __get($key) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($key == 'rows') { |
|
33
|
|
|
return $this->getRows(); |
|
34
|
|
|
} |
|
35
|
|
|
if ($key == 'headers') { |
|
36
|
|
|
return $this->headers; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function fetchRows($resumptionToken = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$results = $this->client->getXML('/analytics/reports', [ |
|
43
|
|
|
'path' => $this->path, |
|
44
|
|
|
'limit' => $this->chunkSize, |
|
45
|
|
|
'token' => $resumptionToken, |
|
46
|
|
|
'filter' => $this->filter ? str_replace(['\''], ['''], $this->filter) : null, |
|
47
|
|
|
]); |
|
48
|
|
|
$results->registerXPathNamespaces([ |
|
49
|
|
|
'rowset' => 'urn:schemas-microsoft-com:xml-analysis:rowset', |
|
50
|
|
|
'xsd' => 'http://www.w3.org/2001/XMLSchema', |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
$this->readResponseHeaders($results); |
|
54
|
|
|
|
|
55
|
|
|
return $results; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $results |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function readResponseHeaders($results) |
|
62
|
|
|
{ |
|
63
|
|
|
$headers = array_map(function ($node) { |
|
64
|
|
|
return $node->attr('name'); |
|
65
|
|
|
}, $results->all('//xsd:complexType[@name="Row"]/xsd:sequence/xsd:element[position()>1]')); |
|
66
|
|
|
|
|
67
|
|
|
if (count($headers)) { |
|
68
|
|
|
if (!count($this->headers)) { |
|
69
|
|
|
$this->headers = $headers; |
|
70
|
|
|
} elseif (count($headers) != count($this->headers)) { |
|
71
|
|
|
throw new \RuntimeException(sprintf('Number of returned columns (%d) does not match number of assigned headers (%d).', |
|
72
|
|
|
count($headers), count($this->headers))); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return \Generator|Row[] |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getRows() |
|
81
|
|
|
{ |
|
82
|
|
|
$isFinished = false; |
|
83
|
|
|
$resumptionToken = null; |
|
84
|
|
|
|
|
85
|
|
|
while (!$isFinished) { |
|
86
|
|
|
$results = $this->fetchRows($resumptionToken); |
|
87
|
|
|
|
|
88
|
|
|
foreach ($results->all('//rowset:Row') as $row) { |
|
89
|
|
|
yield new Row($row, $this->headers); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$resumptionToken = $results->text('/report/QueryResult/ResumptionToken') ?: $resumptionToken; |
|
93
|
|
|
$isFinished = ($results->text('/report/QueryResult/IsFinished') == 'true'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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: