Skip to content

Instantly share code, notes, and snippets.

@devmsh
Created May 20, 2015 10:28
Show Gist options
  • Save devmsh/81363a68fb62ccc28fb5 to your computer and use it in GitHub Desktop.
Save devmsh/81363a68fb62ccc28fb5 to your computer and use it in GitHub Desktop.
Generate the Graph API Subclass src, test, and docs!
<?php
function to_camel_case($str, $capitalise_first_char = false) {
if($capitalise_first_char) {
$str[0] = strtoupper($str[0]);
}
$func = create_function('$c', 'return strtoupper($c[1]);');
return preg_replace_callback('/_([a-z])/', $func, $str);
}
function func_name($field)
{
return to_camel_case("get_" . $field->name) . "()";
}
$type_map = array(
'null' => 'GraphNode',
'group' => 'GraphNode',
'coverphoto' => 'GraphCoverPhoto',
'picture' => 'GraphPicture',
'place' => 'GraphPage',
'datetime' => '\DateTime',
'int32' => 'int',
'unsigned int32' => 'int',
'numeric string' => 'string',
'user|page' => 'GraphNode',
'group|page|application' => 'GraphNode',
'location' => 'GraphPage',
'address' => 'GraphLocation',
'page' => 'GraphPage',
'user' => 'GraphUser',
'enum' => 'string',
'bool' => 'boolean',
'application' => 'GraphApplication',
/* Album */
'event' => 'GraphEvent',
'token with structure: url' => 'string',
/* Comment */
'token with structure: comment id' => 'string',
'storyattachment' => 'GraphStoryAttachment',
'comment' => 'GraphComment',
/* TBD from Video */
'list<videoformat>' => 'GraphVideoFormat',
/* TBD from Comment */
'list<entityattextrange>' => 'GraphNode',
/* TBD from User */
'list<userdevice>' => 'GraphNode',
'list<educationexperience>' => 'GraphNode',
'list<experience>' => 'GraphNode',
'list<string>' => 'GraphNode',
'list<messagingsuggestedgroup>' => 'GraphNode',
'list<workexperience>' => 'GraphNode',
'agerange' => 'GraphNode',
'usercontext' => 'GraphNode',
'currency' => 'GraphNode',
'paymentpricepoints' => 'GraphNode',
'securitysettings' => 'GraphNode',
'videouploadlimits' => 'GraphNode',
);
$datetime_fields = array(
'created_time',
'updated_time',
'start_time',
'end_time',
'backdated_time',
'issued_at',
'expires_at',
'birthday',
'publish_time'
);
$type = "docs";
$subclass = "Album";
// get the fields from the metadata json file
$response = json_decode(file_get_contents("json/{$subclass}.js"));
$fields = $response->metadata->fields;
// prepare the types, graphObjectMap, and datesFields
$graphObjectMap = array();
$datesFields = array();
foreach($fields as $field) {
if (in_array($field->name, $datetime_fields)) {
$field->type = "datetime";
$datesFields[] = $field;
} else {
$field->type = (isset($field->type)) ? $field->type : "null";
}
if (isset($type_map[$field->type])) {
$field->type = $type_map[$field->type];
if(strstr($field->type,"Graph") and $field->type != "GraphNode"){
$graphObjectMap[$field->name] = $field->type;
}
}
}
// start code generation
if($type== "src"){
echo "<?php\n";
?>
/**
* Copyright 2014 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
namespace Facebook\GraphNodes;
/**
* Class Graph<?php echo $subclass."\n"; ?>
*
* @package Facebook
*/
class Graph<?php echo $subclass; ?> extends GraphNode
{
/**
* @var array Maps object key names to GraphNode types.
*/
protected static $graphObjectMap = [
<?php foreach($graphObjectMap as $name => $type){ ?>
'<?php echo $name ?>' => '\Facebook\GraphNodes\<?php echo $type ?>',
<?php } ?>
];
<?php
foreach($fields as $field){
?>
/**
* <?php echo $field->description ?>
*
* @return <?php echo $field->type ?>|null
*/
public function <?php echo func_name($field)."\n" ?>
{
return $this->getField('<?php echo $field->name ?>');
}
<?php
}
?>
}
<?php
}
if($type== "test"){
echo "<?php\n";
?>
/**
* Copyright 2014 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
namespace Facebook\Tests\GraphNodes;
use Facebook\FacebookResponse;
use Mockery as m;
use Facebook\GraphNodes\GraphNodeFactory;
class Graph<?php echo $subclass; ?>Test extends \PHPUnit_Framework_TestCase
{
/**
* @var FacebookResponse
*/
protected $responseMock;
public function setUp()
{
$this->responseMock = m::mock('\Facebook\FacebookResponse');
}
<?php
/*if(!empty($datesFields)) {
?>
public function testDatesGetCastToDateTime()
{
$dataFromGraph = [
<?php foreach($datesFields as $field){ ?>
'<?php echo $field->name ?>' => '2015-04-14T11:00:00+0300',
<?php } ?>
];
$this->responseMock
->shouldReceive('getDecodedBody')
->once()
->andReturn($dataFromGraph);
$factory = new GraphNodeFactory($this->responseMock);
$graphNode = $factory->makeGraph<?php echo $subclass ?>();
<?php foreach($datesFields as $field){ ?>
$<?php echo $field->name ?> = $graphNode-><?php echo func_name($field) ?>;
$this->assertInstanceOf('DateTime', $<?php echo $field->name ?>);
<?php } ?>
}
<?php
}
*/
?>
<?php foreach($graphObjectMap as $name => $type){
?>
public function test<?php echo ucfirst(to_camel_case($name)); ?>GetsCastAs<?php echo $type; ?>()
{
$dataFromGraph = [
'<?php echo $name ?>' => ['id' => '1337']
];
$this->responseMock
->shouldReceive('getDecodedBody')
->once()
->andReturn($dataFromGraph);
$factory = new GraphNodeFactory($this->responseMock);
$graphNode = $factory->makeGraph<?php echo $subclass ?>();
$<?php echo $name ?> = $graphNode-><?php echo to_camel_case("get_".$name)."()" ?>;
$this->assertInstanceOf('\Facebook\GraphNodes\<?php echo $type ?>', $<?php echo $name ?>);
}
<?php } ?>
}
<?php
}
if($type == "docs"){?>
<card>
## Graph<?php echo $subclass ?> Instance Methods {#<?php echo strtolower($subclass) ?>-instance-methods}
The `Graph<?php echo $subclass ?>` collection represents a [<?php echo $subclass ?>](https://developers.facebook.com/docs/graph-api/reference/<?php echo strtolower($subclass) ?>) Graph node.
All getter methods return `null` if the field does not exist on the node.
<?php
foreach($fields as $field) {
?>
### <?php echo func_name($field) ?> {#<?php echo strtolower($subclass) ?>-get<?php echo str_replace('_','',$field->name) ?>}
~~~~
public <?php echo $field->type ?>|null <?php echo func_name($field)."\n" ?>
~~~~
<?php echo $field->description ?>.
<?php
}
?>
</card>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment