greg-boggs / bibliophpile
Requires
Requires (Dev)
README
PHP包装的BiblioCommons API
致谢
最初由纽约公共图书馆编写:https://www.nypl.org/
目前由Multnomah县图书馆维护:https://multcolib.org/
安装
克隆仓库,cd
到您克隆的目录并运行composer
php composer.phar install
php composer.phar install --dev
运行测试
alias phpunit='php vendor/phpunit/phpunit/phpunit.php'
phpunit tests
或
phpunit --testdox tests
生成文档
与PHPDocumentor(以及其他一些工具)配合良好。
phpdoc -d src -t docs
安装PHPDocumentor
pear channel-discover pear.phpdoc.org
pear install phpdoc/phpDocumentor-alpha
注意:您可能还需要安装GraphViz,以便phpdoc运行时不会出错。
运行演示
如果您使用的是PHP 5.4或更高版本,您可以使用内置的Web服务器来运行演示
php -S localhost:8000
然后在浏览器中打开https://:8000/demo.php
基本用法
创建客户端
$client = new \NYPL\Bibliophpile\Client('yourapikey');
通过客户端可以检索各种其他BiblioCommons API资源
$library = $client->library('nypl'); // Returns a Library
$locations = $client->location('nypl'); // Returns a list of Location objects
$list = $client->itemList('1234567'); // Returns an ItemList
$title = $client->title('123456789'); // Returns a Title
$titles = $client->titles('Moby Dick', 'nypl'); // Returns a list of matching Title objects
$user = $client->user('1234567'); // Returns a User
$users = $client->users('example'); // Returns a list of matching User objects
$session = $client->session('a1b2c3d4-e5f6-g7h9-i9j0-k1l2m3n4o5p6'); // Returns a Session
$borrower = $client->borrower('123456'); // Returns a Borrower
ID说明
上述示例中的许多ID看起来像整数,但实际上它们传递和返回时都是字符串。
分页列表
某些方法返回ItemLists
、Titles
或Users
对象,它们都是分页列表。任何返回分页列表的方法都接受一个page
参数(默认为1)和一个limit
参数,指定所需的页面结果(默认:1)和每页的结果数(默认:10)。例如
// Searches NYPL for “Moby Dick” and returns the 5th page
// with 20 items per page.
// Returns a Titles object
$client->titles('Moby Dick', 'nypl', 5, 20);
// Gets the 2nd page of lists made by the user, with the
// default 10 items per page.
// Returns a Users object.
$user->userLists('123456', 2);
这些对象本身具有以下方法
count()
:返回结果中的项目总数page()
:返回当前页码pages()
:返回结果中的页数limit()
:返回每页的项目数gotoPage(page)
:检索所需页码的结果。next()
:检索下一页的结果。
尝试获取不存在的页面(小于1或高于最高页码)将抛出NoSuchPageException
。当您已到达结果末尾时尝试获取下一页将抛出EndOfResultsException
,这可以被捕获并用作退出循环的信号。
空属性
BiblioCommons API返回的许多对象可以具有空、null或缺失的属性。Nypl-bibliophpile通过为预期为数组的属性返回空数组,为其他任何内容返回NULL
来处理所有这些情况。
资源
图书馆
可以通过ID检索图书馆对象
$library = $client->library('nypl');
echo $library->id(); // "nypl"
echo $library->name(); // "New York Public Library"
echo $library->catalog(); // "http://nypl.bibliocommons.com"
位置
一个图书馆可以有多个位置(基本上是分馆)。可以从图书馆对象或使用图书馆ID从客户端检索图书馆的位置列表
$locations = $library->locations();
echo $locations[0]->name(); // "115th Street"
或从客户端使用图书馆的ID
$locations = $client->locations('nypl');
个别位置也作为Copy(特定副本的位置)和Borrower(借阅者的首选位置)对象中的属性出现。
标题
“标题”可以是书籍、DVD、CD或任何可以借出的物品。
方法
列表
BiblioCommons列表是由用户生成的标题列表
通过其ID检索列表
$list = $client->itemList('170265611');
echo $list->name(); // "Recommended by our librarians 4";
标题
通过ID获取标题(注意,ID是数字,但仍然是字符串)
$title = $client->title('18708779052907');
echo $title->name(); // "Moby-Dick";
获取一个标题的副本
$copies = $title->copies(); // Array of copy objects
您也可以通过标题的ID从客户端获取副本
$copies = $client->copies('18708779052907');
用户
通过ID检索用户
$user = $client->user('123456789');
echo $user->name(); // "fakeuser"
通过用户名搜索用户
$users = $client->users('fakeuser');
$userlist = $users->users(); // array of results
count($users->users()); // 1
echo $userlist[0]->name(); // "fakeuser"