forked from waylaidwanderer/PHP-SteamCommunity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Group.php
135 lines (119 loc) · 4.06 KB
/
Group.php
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
<?php
/**
* Created by PhpStorm.
* User: Joel
* Date: 2016-02-18
* Time: 11:30 AM
*/
namespace waylaidwanderer\SteamCommunity;
use waylaidwanderer\SteamCommunity\Group\History\HistoryItem;
class Group
{
const BASE_URL = "http://steamcommunity.com/gid/";
private $steamCommunity;
private $gid;
private $xml;
public function __construct($gid, SteamCommunity $steamCommunity = null)
{
$this->gid = $gid;
$this->steamCommunity = is_null($steamCommunity) ? new SteamCommunity() : $steamCommunity;
}
public function getGroupXml($page = 1)
{
if ($this->xml == null) {
$url = self::BASE_URL . $this->gid . '/memberslistxml/?xml=1&p=' . $page;
$response = Helper::cURL($url);
$this->xml = new \SimpleXMLElement($response);
}
return $this->xml;
}
/**
* @return int Number of pages in memberslistxml.
*/
public function getNumXmlPages()
{
$xml = $this->getGroupXml();
return (int)$xml->totalPages;
}
/**
* @return array An array of SteamID64s.
*/
public function getMembersList()
{
$members = [];
$numPages = $this->getNumXmlPages();
for ($i = 1; $i <= $numPages; $i++) {
$members = array_merge($members, $this->getMembersListForPage($i));
}
return $members;
}
/**
* @param $page
* @return array An array of SteamID64s.
*/
public function getMembersListForPage($page)
{
$members = [];
$xml = $this->getGroupXml($page);
foreach ($xml->members->steamID64 as $steamID64) {
$members[] = (string)$steamID64;
}
return $members;
}
/**
* @return int
*/
public function getMemberCount()
{
$xml = $this->getGroupXml();
return (int)$xml->memberCount;
}
/**
* @param int $page
* @return HistoryItem[]
*/
public function getHistory($page = 1)
{
$history = [];
$xpath = $this->getHistoryXPath($page);
/** @var \DOMElement[] $historyItems */
$historyItems = $xpath->query('//div[contains(@class, "group_summary")]/div[contains(@class, "historyItem")]');
foreach ($historyItems as $historyItem) {
$type = $xpath->query('.//span[contains(@class, "historyShort")]', $historyItem)->item(0)->nodeValue;
$date = $xpath->query('.//span[contains(@class, "historyDate")]', $historyItem)->item(0)->nodeValue;
$users = $xpath->query('.//a', $historyItem);
if ($users->length == 2) {
/** @var \DOMElement $user */
$user = $users->item(1);
$userAccountId = $user->getAttribute('data-miniprofile');
/** @var \DOMElement $targetUser */
$targetUser = $users->item(0);
$targetAccountId = $targetUser->getAttribute('data-miniprofile');
$history[] = new HistoryItem($type, $date, Helper::toCommunityID($userAccountId), Helper::toCommunityID($targetAccountId));
} else if ($users->length == 1) {
$user = $users->item(0);
$userAccountId = $user->getAttribute('data-miniprofile');
$history[] = new HistoryItem($type, $date, Helper::toCommunityID($userAccountId));
}
}
return $history;
}
public function getNumHistoryPages()
{
$xpath = $this->getHistoryXPath();
$pagingText = $xpath->query('//div[contains(@class, "group_summary")]/div[contains(@class, "group_paging")]/p');
if (preg_match('/(?<=of )(.*)(?= History)/', $pagingText->item(0)->nodeValue, $matches)) {
return (int)ceil($matches[1] / 50);
}
return 1;
}
private function getHistoryXPath($page = 1)
{
$url = self::BASE_URL . $this->gid . '/history?p=' . $page;
$html = $this->steamCommunity->cURL($url);
libxml_use_internal_errors(true);
$doc = new \DOMDocument();
$doc->loadHTML($html);
return new \DOMXPath($doc);
}
}