锡罐PHP图书馆:查询处理结果

Tin Can PHP library: processing results of query

我正在尝试使用 Tin Can PHP 库从 LRS 记录中提取演员代理人的姓名。我只有此人的 mbox 值(电子邮件地址),所以我的检索尝试是这样进行的:

$actor = new TinCan\Agent();
$actor
    ->setMbox('mailto:bob@downe.com');

// return raw statement
$retrieve = $lrs->queryStatements(['agent' => $actor]);

如果我打印出 $retrieve 的值,我会得到以下原始语句(为简洁起见被截断):

TinCan\LRSResponse Object (
    [success] => 1
    [content] => TinCan\StatementsResult Object
        (
            [statements:protected] => Array
                (
                    [0] => TinCan\Statement Object
                        (
                            [id:protected] => 4c707377-384d-4547-a858-61696b386b6d
                            [stored:protected] => 2016-10-24T15:57:43.358Z
                            [authority:protected] => TinCan\Agent Object
                                (
                                    [objectType:protected] => Agent
                                    [name:protected] => Grant
                                    [mbox:protected] => 
                                    [mbox_sha1sum:protected] => 
                                    [openid:protected] => 
                                    [account:protected] => TinCan\AgentAccount Object
                                        (
                                            [name:protected] => ###
                                            [homePage:protected] => http://cloud.scorm.com/
                                        )

                                )

                            [version:protected] => 1.0.0
                            [attachments:protected] => Array
                                (
                                )

                            [actor:protected] => TinCan\Agent Object
                                (
                                    [objectType:protected] => Agent
                                    [name:protected] => Bob Downe
                                    [mbox:protected] => mailto:bob@downe.com
                                    [mbox_sha1sum:protected] => 
                                    [openid:protected] => 
                                    [account:protected] => 
                                )

然后我尝试从原始语句中提取名称,如下所示:

// take content from raw statements using getStatements() method
$further_output = $retrieve->content->getStatements();

这会生成语句 class 的一组对象,在本例中为一个值的数组。

然后我必须以某种方式从数组中取出对象,以便访问用于提取我想要的信息的方法。我是这样做的:

// Get actor out of object
$extracted = $further_output[0]->getActor()->getName();
echo "<p>$extracted</p>"; // produces 'Bob Downe'

这看起来效率很低,我相信一定有更好的方法。

我有两个问题:

  1. 提取我想要的信息的最有效方法是什么?

  2. 为什么原始语句会为每个属性显示 'protected',例如[statements:protected], [id:protected], [stored:protected] 等?

我研究了这些相关链接,但它们没有解决我的问题:

how to execute a query on tin-can statements

如有任何帮助,我将不胜感激。

  1. 这是从图书馆提取特定信息的最有效方法。是不是只有public界面你觉得效率低下?还有什么 "efficient"?请注意,您需要进行错误检查,换句话说,检查请求是否成功、数组中是否有语句以及名称 属性 是否已定义。库无法提前知道这些事情,并且在设计上被认为是与 LRS 通信的低级接口。

  2. 这是基本的 OOP 开发,请参阅 http://php.net/manual/en/language.oop5.visibility.php 或 Google "OOP protected"。最终在 TinCanPHP 中,您通过 public 方法访问数据,以便我们可以保持封装。