<card>
# GraphObject for the Facebook SDK for PHP

A `Facebook\GraphNodes\GraphObject` is a collection that represents a node returned by the Graph API.
</card>

<card>
## Facebook\GraphNodes\GraphObject {#overview}

This base class has several subclasses:

[__GraphUser__](#user-instance-methods)
[__GraphPage__](#page-instance-methods)
[__GraphAlbum__](#album-instance-methods)
[__GraphLocation__](#location-instance-methods)
[__GraphPicture__](#picture-instance-methods)
[__GraphAchievement__](#achievement-instance-methods)

`GraphObject`'s are obtained from a [`Facebook\FacebookResponse`](/docs/php/FacebookResponse) object which represents an HTTP response from the Graph API.

Usage:

~~~~
$fb = new Facebook\Facebook(\* *\);
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/something');

// Get the base class GraphObject from the response
$node = $response->getGraphObject();

// Get the response typed as a GraphUser
$user = $response->getGraphUser());

// Get the response typed as a GraphPage
$page = $response->getGraphPage();

// User example
echo $node->getProperty('name'); // From GraphObject
echo $user->getName(); // From GraphUser

// Location example
echo $node->getProperty('country'); // From GraphObject
echo $location->getCountry(); // From GraphLocation
~~~~
</card>

<card>
## SPL Libraries {#spl}

The `GraphObject` collection and its subclasses implement several [SPL](http://php.net/manual/en/book.spl.php) libraries and [predefined PHP interfaces and classes](http://php.net/manual/en/reserved.interfaces.php) which make it convenient to work with the object in PHP. The supported libraries are `ArrayAccess`, `ArrayIterator`, `Countable`, and `IteratorAggregate`.

All of the following operations are possible on a `GraphObject`.

~~~~
$graphObject = $response->getGraphObject();

// Array access
$id = $graphObject['id'];

// Iteration
foreach ($graphObject as $key => $value) {
  // . . .
}

// Counting
$total = count($graphObject);
~~~~

</card>

<card>
## GraphObject Instance Methods {#instance-methods}


### asArray {#as-array}
`asArray()`  
Returns the raw representation (associative arrays, nested) of this objects underlying data.


### asJson {#as-json}
`asJson()`
Returns the data as a JSON string.


### getProperty {#get-property}
`getProperty(string $name, string $default = 'foo')`
Gets the value of a named key for this graph object.  If the value is a scalar (string, number, etc.) it will be returned.  If it's an associative array, it will be returned as a GraphObject.

The second argument lets you define a default value to return if the property doesn't exist.


### getPropertyNames {#get-property-names}
`getPropertyNames()`  
Returns an array with the names of all properties present on this graph object.


### map {#map}
`map(Closure $callback)`
Provides a way to map over the data within the collection just like `array_map()`.
</card>

<card>
## GraphUser Instance Methods {#user-instance-methods}

The `GraphUser` collection represents a [User](https://developers.facebook.com/docs/graph-api/reference/user) Graph node.

### Auto-cast properties {#user-auto-casting}

The following properties on the `GraphUser` collection will get automatically cast as `GraphObject` subtypes:

FB(devsite:markdown-wiki:table {
  columns: ['Property','GraphObject subtype',],
  rows: [
    [
      '`hometown`',
      '[`Facebook\GraphNodes\GraphPage`](#page-instance-methods)',
    ],
    [
      '`location`',
      '[`Facebook\GraphNodes\GraphPage`](#page-instance-methods)',
    ],
    [
      '`significant_other`',
      '[`Facebook\GraphNodes\GraphUser`](#user-instance-methods)',
    ],
  ],
})

All getter methods return `null` if the property does not exist on the node.

### getId() {#user-getid}
~~~~
public string|null getId()
~~~~
Returns the `id` property for the user as a string if present.

### getName() {#user-getname}
~~~~
public string|null getName()
~~~~
Returns the `name` property for the user as a string if present.

### getFirstName() {#user-getfirstname}
~~~~
public string|null getFirstName()
~~~~
Returns the `first_name` property for the user as a string if present.

### getMiddleName() {#user-getmiddlename}
~~~~
public string|null getMiddleName()
~~~~
Returns the `middle_name` property for the user as a string if present.

### getLastName() {#user-getlastname}
~~~~
public string|null getLastName()
~~~~
Returns the `last_name` property for the user as a string if present.

### getLink() {#user-getlink}
~~~~
public string|null getLink()
~~~~
Returns the `link` property for the user as a string if present.

### getBirthday() {#user-getbirthday}
~~~~
public \DateTime|null getBirthday()
~~~~
Returns the `birthday` property for the user as a `\DateTime` if present.

### getLocation() {#user-getlocation}
~~~~
public Facebook\GraphNodes\GraphPage|null getLocation()
~~~~
Returns the `location` property for the user as a `Facebook\GraphNodes\GraphPage` if present.

### getHometown() {#user-gethometown}
~~~~
public Facebook\GraphNodes\GraphPage|null getHometown()
~~~~
Returns the `hometown` property for the user as a `Facebook\GraphNodes\GraphPage` if present.

### getSignificantOther() {#user-getsignificantother}
~~~~
public Facebook\GraphNodes\GraphUser|null getHometown()
~~~~
Returns the `significant_other` property for the user as a `Facebook\GraphNodes\GraphUser` if present.
</card>

<card>
## GraphPage Instance Methods {#page-instance-methods}

The `GraphPage` collection represents a [Page](https://developers.facebook.com/docs/graph-api/reference/page) Graph node.

### Auto-cast properties {#page-auto-casting}

The following properties on the `GraphPage` collection will get automatically cast as `GraphObject` subtypes:

FB(devsite:markdown-wiki:table {
  columns: ['Property','GraphObject subtype',],
  rows: [
    [
      '`best_page`',
      '[`Facebook\GraphNodes\GraphPage`](#page-instance-methods)',
    ],
    [
      '`global_brand_parent_page`',
      '[`Facebook\GraphNodes\GraphPage`](#page-instance-methods)',
    ],
    [
      '`location`',
      '[`Facebook\GraphNodes\GraphLocation`](#location-instance-methods)',
    ],
  ],
})

All getter methods return `null` if the property does not exist on the node.

### getId() {#page-getid}
~~~~
public string|null getId()
~~~~
Returns the `id` property for the page as a string if present.

### getName() {#page-getname}
~~~~
public string|null getName()
~~~~
Returns the `name` property for the page as a string if present.

### getCategory() {#page-getcategory}
~~~~
public string|null getCategory()
~~~~
Returns the `category` property for the page as a string if present.

### getBestPage() {#page-getbestpage}
~~~~
public Facebook\GraphNodes\GraphPage|null getBestPage()
~~~~
Returns the `best_page` property for the page as a `Facebook\GraphNodes\GraphPage` if present.

### getGlobalBrandParentPage() {#page-getglobalbrandparentpage}
~~~~
public Facebook\GraphNodes\GraphPage|null getGlobalBrandParentPage()
~~~~
Returns the `global_brand_parent_page` property for the page as a `Facebook\GraphNodes\GraphPage` if present.

### getLocation() {#page-getlocation}
~~~~
public Facebook\GraphNodes\GraphLocation|null getLocation()
~~~~
Returns the `location` property for the page as a `Facebook\GraphNodes\GraphLocation` if present.

### getAccessToken() {#page-getaccesstoken}
~~~~
public string|null getAccessToken()
~~~~
Returns the `access_token` property for the page if present. (Only available in the `/me/accounts` context.)

### getPerms() {#page-getperms}
~~~~
public array|null getAccessToken()
~~~~
Returns the `perms` property for the page as an `array` if present. (Only available in the `/me/accounts` context.)
</card>

<card>
## GraphAlbum Instance Methods {#album-instance-methods}

The `GraphAlbum` collection represents an [Album](https://developers.facebook.com/docs/graph-api/reference/album) Graph node.

### Auto-cast properties {#album-auto-casting}

The following properties on the `GraphAlbum` collection will get automatically cast as `GraphObject` subtypes:

FB(devsite:markdown-wiki:table {
  columns: ['Property','GraphObject subtype',],
  rows: [
    [
      '`from`',
      '[`Facebook\GraphNodes\GraphUser`](#user-instance-methods)',
    ],
    [
      '`place`',
      '[`Facebook\GraphNodes\GraphPage`](#page-instance-methods)',
    ],
  ],
})

All getter methods return `null` if the property does not exist on the node.

### getId() {#album-getid}
~~~~
public string|null getId()
~~~~
Returns the `id` property for the album as a string if present.

### getName() {#album-getname}
~~~~
public string|null getName()
~~~~
Returns the `name` property for the album as a string if present.

### getCanUpload() {#album-getcanupload}
~~~~
public boolean|null getCanUpload()
~~~~
Returns the `can_upload` property for the album as a boolean if present.

### getCount() {#album-getcount}
~~~~
public int|null getCount()
~~~~
Returns the `count` property for the album as an integer if present.

### getCoverPhoto() {#album-getcoverphoto}
~~~~
public string|null getCoverPhoto()
~~~~
Returns the `cover_photo` property for the album as a string if present.

### getCreatedTime() {#album-getcreatedtime}
~~~~
public \DateTime|null getCreatedTime()
~~~~
Returns the `created_time` property for the album as a `\DateTime` if present.

### getUpdatedTime() {#album-getupdatedtime}
~~~~
public \DateTime|null getUpdatedTime()
~~~~
Returns the `updated_time` property for the album as a `\DateTime` if present.

### getDescription() {#album-getdescription}
~~~~
public string|null getDescription()
~~~~
Returns the `description` property for the album as a string if present.

### getFrom() {#album-getfrom}
~~~~
public Facebook\GraphNodes\GraphUser|null getFrom()
~~~~
Returns the `from` property for the album as a `Facebook\GraphNodes\GraphUser` if present.

### getPlace() {#album-getplace}
~~~~
public Facebook\GraphNodes\GraphPage|null getPlace()
~~~~
Returns the `place` property for the album as a `Facebook\GraphNodes\GraphPage` if present.

### getLink() {#album-getlink}
~~~~
public string|null getLink()
~~~~
Returns the `link` property for the album as a string if present.

### getLocation() {#album-getlocation}
~~~~
public Facebook\GraphNodes\GraphObject|string|null getLocation()
~~~~
Returns the `location` property for the album as a `Facebook\GraphNodes\GraphObject` or string if present.

### getPrivacy() {#album-getprivacy}
~~~~
public string|null getPrivacy()
~~~~
Returns the `privacy` property for the album as a string if present.

### getType() {#album-gettype}
~~~~
public string|null getType()
~~~~
Returns the `type` property for the album as a string (`profile`, `mobile`, `wall`, `normal` or `album`) if present.
</card>

<card>
## GraphLocation Instance Methods {#location-instance-methods}

All getter methods return `null` if the property does not exist on the node.

### getStreet() {#location-getstreet}
~~~~
public string|null getStreet()
~~~~
Returns the `street` property for the location as a string if present.

### getCity() {#location-getcity}
~~~~
public string|null getCity()
~~~~
Returns the `city` property for the location as a string if present.

### getCountry() {#location-getcountry}
~~~~
public string|null getCountry()
~~~~
Returns the `country` property for the location as a string if present.

### getZip() {#location-getzip}
~~~~
public string|null getZip()
~~~~
Returns the `zip` property for the location as a string if present.

### getLatitude() {#location-getlatitude}
~~~~
public float|null getLatitude()
~~~~
Returns the `latitude` property for the location as a float if present.

### getLongitude() {#location-getlongitude}
~~~~
public float|null getLongitude()
~~~~
Returns the `longitude` property for the location as a float if present.
</card>

<card>
## GraphPicture Instance Methods {#picture-instance-methods}

All getter methods return `null` if the property does not exist on the node.

### getUrl() {#picture-geturl}
~~~~
public string|null getUrl()
~~~~
Returns the `url` property for the picture as a string if present.
</card>

<card>
## GraphAchievement Instance Methods {#achievement-instance-methods}

All getter methods return `null` if the property does not exist on the node.

### getId() {#achievement-getid}
~~~~
public string|null getId()
~~~~
Returns the `id` property for the achievement as a string if present.
</card>
