-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClisClient.php
85 lines (75 loc) · 2.65 KB
/
ClisClient.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
<?php
use Goutte\Client;
use Symfony\Component\DomCrawler\Crawler; //インスペクション対策
/**
* URLが「clis」となっている図書館システムのクライアント
*/
class ClisClient
{
private $client;
private $crawler;
/**
* ClisClient constructor.
* @param string $login_url ログインURL
* @param array $login_params ログイン情報 [name => value]
* @param string $form_selector ログインフォームのセレクタ
*/
public function __construct($login_url, $login_params, $form_selector = "form")
{
$this->client = new Client();
$this->crawler = $this->client->request('GET', $login_url);
$form = $this->crawler->filter($form_selector)->form();
$this->crawler = $this->client->submit($form, $login_params);
}
//public
/**
* 貸出状況を取得する
* @return array 貸出状況(No, type, title, id, place, lent_at, expiration, extend)
*/
public function getBorrowing()
{
$this->clickLink("貸出状況照会へ");
$list = $this->crawler->filter("table.FULL tbody tr")->each(function (Crawler $tr) {
$item = $tr->filter("td")->each(function (Crawler $td) {
return $this->zenkaku_trim($td->text());
});
return array_combine([
"No", "type", "title", "id", "place", "lent_at", "expiration", "extend"
], $item);
});
$this->back();
return $list;
}
/**
* 予約状況を取得する
* @return array 予約状況(No, type, title, place, reserved_at, status, expiration, stock, order)
*/
public function getReservation()
{
$this->clickLink("予約状況照会へ");
$list = $this->crawler->filter("table.FULL tbody tr")->each(function (Crawler $tr) {
$item = $tr->filter("td")->each(function (Crawler $td) {
return $this->zenkaku_trim($td->text());
});
return array_combine([
"No", "type", "title", "place", "reserved_at", "status", "expiration", "stock", "order"
], $item);
});
$this->back();
return $list;
}
//private
private function clickLink($value)
{
$link = $this->crawler->selectLink($value)->link();
$this->crawler = $this->client->click($link);
}
private function back()
{
$this->clickLink("メニューへ戻る");
}
private function zenkaku_trim($str)
{
return trim(preg_replace("/^[\s ]+|[\s ]+$/u", " ", $str));
}
}