锡罐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();

生成一个Statement类的对象数组,在本例中是一个只有一个值的数组。

然后,我必须以某种方式将对象从数组中取出,以便访问用于提取我想要的信息的方法。我是这样做的:
// 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]等?

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

如何对罐头语句执行查询

使用TinCan API从Learning Locker LRS获取语句

我将非常感激任何帮助。
  1. 这是使用库提取特定信息的最有效方法。您认为仅仅是公共接口效率低下吗?什么会更"有效率"?注意,您需要进行错误检查,换句话说,检查请求是否成功,数组中是否有语句,以及name属性是否定义。库不能提前知道这些事情,并且被设计为与LRS通信的低级接口。

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