0
votes

I'm looking for the best way to model data for my Facebook integration component. For the sake of this example, we will be looking at Facebook's photo albums. Each user has one or many Albums, which contain one or many Photos. The data definition for Album and Photo can be found here: http://developers.facebook.com/docs/reference/api/album/ http://developers.facebook.com/docs/reference/api/photo/

My primary goal is to make using the Facebook Graph API as easy as possible, I am particularly interested in making everything I can code-complete compatible. Initially, my idea was to model the data in my own class exactly as defined in the above documents eg, for Album:

/**
 * @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;
    }

}

Using this method I can lazy load the photos from another entry point when/if they are required, and all is well, but I cannot get code completion for the array of photos that would be returned from the request, as I know no way to make an iterate-supported object that supports code completion like $this->photos[0]->... I also wonder if it's a waste of time having all the properties there, there is already a stcClass that comes back from Facebook, so I could store that in a data object, and use the @property annotation and magic methods to grab the data from the returned stdClass stored in one property, like:

/**
 * @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};
        }
    }
}

This saves me lots of time in declaring the individual methods, is quicker because I don't have to iterate through the returned stdClass to convert to my model, and generally works the same for code-completion as defining each property.

HELP!

1
What's the question here, exactly?JJJ
The question is, how can I model this data in PHP. Specifically, how can I model it in PHP with code completion.GeeH

1 Answers

1
votes

Depends on IDE/PHPDoc support - PHPStorm should support @property SomePhotoCLassName[] , otherwise you end up with @var SomePhotoClassName in your array iteration