如何在PHP中建模这些数据?

时间:2012-03-22 13:41:16

标签: php

我正在寻找为我的Facebook集成组件建模数据的最佳方法。为了这个例子,我们将查看Facebook的相册。每个用户都有一个或多个相册,其中包含一个或多个照片。相册和照片的数据定义可以在这里找到: http://developers.facebook.com/docs/reference/api/album/ http://developers.facebook.com/docs/reference/api/photo/

我的主要目标是尽可能简单地使用Facebook Graph API,我特别感兴趣的是我可以编写完全兼容的代码。 最初,我的想法是完全按照上述文档中的定义对我自己班级中的数据进行建模,例如,对于专辑:

/**
 * @property string $id The album ID
 * @property Profile $from The profile that created this album
 * @property string $name The title of the album
 * @property string $description The description of the album
 * @property string $location The location of the album
 * @property string $link A link to this album on Facebook
 * @property string $cover_photo The album cover photo ID
 * @property string $privacy The privacy settings for the album
 * @property int $count The number of photos in this album
 * @property string $type The type of the album: profile, mobile, wall, normal or album
 * @property string $created_time The time the photo album was initially created (ISO-8601 date-time)
 * @property array $likes The information about user likes
 * @property array $photos The Photos in this album
 * @property array $comments
 */
class Album extends Graph
{
    /**
     * The album ID. 
     * @var string
     */
    protected $id;
    /**
     * The profile that created this album. 
     * @var Profile
     */
    protected $from;

    /**
     * The title of the album. 
     * @var string
     */
    protected $name;
    /**
     * The description of the album. 
     * @var string
     */
    protected $description;
    /**
     * The location of the album. 
     * @var string
     */
    protected $location;
    /**
     * A link to this album on Facebook. 
     * @var string
     */
    protected $link;
    /**
     * The album cover photo ID.
     * Valid URL
     * @var string
     */
    protected $cover_photo;
    /**
     * The privacy settings for the album. 
     * @var string
     */
    protected $privacy;
    /**
     * The number of photos in this album. 
     * @var string
     */
    protected $count;
    /**
     * The type of the album: profile, mobile, wall, normal or album. 
     * @var string
     */
    protected $type;
    /**
     * The time the photo album was initially created. 
     * ISO-8601 date-time
     * @var string
     */
    protected $created_time;
    /**
     * The last time the photo album was updated. 
     * ISO-8601 date-time
     * @var string
     */
    protected $updated_time;
    /**
     * The information about user likes
     * @var array
     */
    protected $likes;
    /**
     * The comments posted on this Album
     * @var array
     */
    protected $comments;
    /**
     * The Photos in this album
     * @var array Graph\Album\Photo
     */
    protected $photos;

    /**
     * Gets a specific album from the graph
     * @param $id string
     * @return Album
     */
    public function getAlbum($id)
    {
        $this->setFromStdClass($this->getFromGraph("/{$id}"));
        return $this;
    }

}

使用这种方法,如果需要,我可以延迟加载来自另一个入口点的照片,一切都很好,但我无法获得从请求中返回的照片数组的代码完成,正如我所知道的那样无法创建支持代码完成的迭代支持对象,如$this->photos[0]->...我也想知道是否浪费时间拥有所有属性,已经有一个stcClass从Facebook返回,所以我可以将它存储在数据对象中,并使用@property注释和魔术方法从存储在一个属性中的返回的stdClass中获取数据,如:

/**
 * @property string $id The album ID
 * @property Profile $from The profile that created this album
 * @property string $name The title of the album
 * @property string $description The description of the album
 * @property string $location The location of the album
 * @property string $link A link to this album on Facebook
 * @property string $cover_photo The album cover photo ID
 * @property string $privacy The privacy settings for the album
 * @property int $count The number of photos in this album
 * @property string $type The type of the album: profile, mobile, wall, normal or album
 * @property string $created_time The time the photo album was initially created (ISO-8601 date-time)
 * @property array $likes The information about user likes
 * @property array $photos The Photos in this album
 * @property array $comments
 */
class Album extends Graph
{
    protected $data;

    public function __get($name)
    {
        $method = 'get' . ucfirst($name);
        if (method_exists($this, $method)) {
            return $this->{$method}();
        }
        if (property_exists($this->data, $name)) {
            return $this->data->{$name};
        }
    }
}

这节省了我用于声明各个方法的大量时间,因为我不必遍历返回的stdClass转换为我的模型,并且通常与定义每个属性的代码完成相同。< / p>

HELP!

1 个答案:

答案 0 :(得分:1)

取决于IDE / PHPDoc支持 - PHPStorm应该支持@property SomePhotoCLassName [],否则你最终会在数组迭代中使用@var SomePhotoClassName