multco / bibliophpile
Requires
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-26 05:02:58 UTC
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
参数和一个 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"