Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data structure for NPF Content Blocks #108

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ usage.php

vendor
composer.lock
.phpunit.result.cache
.idea/*
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ language: php
before_script: composer install

php:
- 7.1
- 7.3
- 7.2
- 7.0
- 5.6
- hhvm

matrix:
allow_failures:
- php: hhvm
- php: 5.6
- php: 7.0
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},

"require-dev": {
"phpunit/phpunit": "5.3.*"
"phpunit/phpunit": "^8"
},

"autoload": {
Expand Down
99 changes: 86 additions & 13 deletions lib/Tumblr/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace Tumblr\API;

use Tumblr\API\Write\NPFScheme;
use Tumblr\API\Write\NPFReblogScheme;
use Tumblr\API\Read\BlogInfo;
use Tumblr\API\RequestException;
use Tumblr\API\Read\User;
/**
* A client to access the Tumblr API
*/
class Client
{

/**
* @var string
* API Key for a registered user
*/
private $apiKey;

/**
Expand All @@ -18,7 +26,7 @@ class Client
* @param string $token oauth token
* @param string $secret oauth token secret
*/
public function __construct($consumerKey, $consumerSecret = null, $token = null, $secret = null)
public function __construct(string $consumerKey, string $consumerSecret = null, string $token = null, string $secret = null)
{
$this->requestHandler = new RequestHandler();
$this->setConsumer($consumerKey, $consumerSecret);
Expand Down Expand Up @@ -68,7 +76,14 @@ public function getRequestHandler()
*/
public function getUserInfo()
{
return $this->getRequest('v2/user/info', null, false);
$response = $this->getRequest('v2/user/info', null, false);
return new User(
$response->user->name,
$response->user->likes,
$response->user->default_post_format,
$response->user->following,
$response->user->blogs
);
}

/**
Expand Down Expand Up @@ -200,29 +215,53 @@ public function reblogPost($blogName, $postId, $reblogKey, $options = null)
* Edit a post
*
* @param string $blogName the name of the blog
* @param int $postId the id of the post to edit
* @param array $data the data to save
* @param int $postId the id of the post to edit
* @param NPFScheme $data the data to save
*
* @return array the response array
*/
public function editPost($blogName, $postId, $data)
public function editPost($blogName, $postId, NPFScheme $data)
{
$data->id = $postId;
$path = $this->blogPath($blogName, '/posts/'.$postId);
return $this->postRequest($path, $data->toJSON(), false);
}

/**
* @param $blogName the name of the blog
* @param $postId the id of the post to edit
* @param $data the data to save
*
* @return array the response array
*/
public function editLegacyPost($blogName, $postId, $data) {
$data['id'] = $postId;
$path = $this->blogPath($blogName, '/post/edit');

return $this->postRequest($path, $data, false);
}

/**
* Create a post
*
* @param string $blogName the name of the blog
* @param array $data the data to save
* @param NPFScheme $data the data to save
*
* @return array the response array
*/
public function createPost($blogName, $data)
public function createPost($blogName, NPFScheme $data)
{
$path = $this->blogPath($blogName, '/posts');

return $this->postRequest($path, $data->toJSON(), false);
}

/**
* @param $blogName the name of the blog
* @param $data the data to save
*
* @return array the response array
*/
public function createLegacyPost($blogName, $data) {
$path = $this->blogPath($blogName, '/post');

return $this->postRequest($path, $data, false);
Expand Down Expand Up @@ -250,13 +289,21 @@ public function getTaggedPosts($tag, $options = null)
* Get information about a given blog
*
* @param string $blogName the name of the blog to look up
* @return array the response array
* @return BlogInfo the response array
*/
public function getBlogInfo($blogName)
{
$path = $this->blogPath($blogName, '/info');

return $this->getRequest($path, null, true);
$result = $this->getRequest($path, null, true);
$info = new BlogInfo($result->blog->name, $result->blog->title, $result->blog->description, $result->blog->url,
$result->blog->uuid, $result->blog->updated, $result->blog->posts, $result->blog->ask,
$result->blog->ask_anon, $result->blog->ask_page_title, $result->blog->likes,
$result->blog->is_blocked_from_primary, $result->blog->avatar, $result->blog->theme,
$result->blog->timezone_offset, $result->blog->can_chat, $result->blog->can_subscribe,
$result->blog->is_nsfw, $result->blog->share_likes, $result->blog->submission_page_title,
$result->blog->subscribed, $result->blog->is_optout_ads);
return $info;
}

/**
Expand Down Expand Up @@ -289,7 +336,33 @@ public function getBlogLikes($blogName, $options = null)
{
$path = $this->blogPath($blogName, '/likes');

return $this->getRequest($path, $options, true);
$result = $this->getRequest($path, $options, true);
$liked_posts = [];
foreach($result->liked_posts as $post) {
array_push($liked_posts, null);
}
}

/**
* Get blog followers for a given blog
*
* @param string $blogName the name of the blog to look up
* @param array $options the options for the call
*
* @return array the response array
*/
public function getBlogFollowing($blogName, $options = null)
{
$path = $this->blogPath($blogName, '/following');

$result = $this->getRequest($path, $options, false);
$blogs = [];
foreach($result->blogs as $blog) {
array_push($blogs, new BlogInfo($blog->name, $blog->title, $blog->description,
$blog->url, $blog->uuid, $blog->updated));
}
$result->blogs = $blogs;
return $result;
}

/**
Expand Down Expand Up @@ -378,7 +451,7 @@ public function getSubmissionPosts($blogName, $options = null)
* @param array $options the options to call with
* @param bool $addApiKey whether or not to add the api key
*
* @return array the response object (parsed)
* @return stdClass the response object (parsed)
*/
public function getRequest($path, $options, $addApiKey)
{
Expand Down
17 changes: 17 additions & 0 deletions lib/Tumblr/API/HashInitializable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tumblr\API;

abstract class HashInitializable
{
public function __construct($args)
{
foreach($args as $name => $value)
{
if(property_exists($this, $name))
{
$this->{$name} = $value;
}
}
}
}
49 changes: 49 additions & 0 deletions lib/Tumblr/API/NPF/Content/Attribution/AppAttribution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Tumblr\API\NPF\Content\Attribution;

use Tumblr\API\NPF\Content\Attribution\AttributionObject;
use Tumblr\API\NPF\Content\Attribution\AttributionTypes;
use Tumblr\API\NPF\Content\MediaBlock;
use Tumblr\API\NPF\Content\ValidationTrait;

/**
* Class AppAttribution
* @package Tumblr\API\NPF\Content\Attribution
*/
class AppAttribution extends AttributionObject{
use ValidationTrait;

/**
* @var string
*/
protected $url;
/**
* @var string
*/
protected $app_name;
/**
* @var string
*/
protected $display_text;
/**
* @var MediaBlock
*/
protected $logo;

/**
* AppAttribution constructor.
* @param string $url URL to the Application Endpoint used for this type of attribution
* @param string $app_name Name of the connected app as a canonical string
* @param string $display_text Text to display when incorporating the attribution
* @param MediaBlock|null $logo Application logo to display with the attributed content
*/
public function __construct(string $url, string $app_name = "", string $display_text = "",
MediaBlock $logo = null) {
parent::__construct(AttributionTypes::App);
$this->url = $this->validURL($url);
$this->app_name = $app_name;
$this->display_text = $display_text;
$this->logo = $logo;
}
}
37 changes: 37 additions & 0 deletions lib/Tumblr/API/NPF/Content/Attribution/AttributionObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tumblr\API\NPF\Content\Attribution;

use Tumblr\API\NPF\Content\Attribution\AttributionType;

/**
* Class AttributionObject
* An attribution is a reference to an external resource.
* A provider can be another type of application or media type.
* e.g.:
* * Instagram image post
* * Blog reference
* @package Tumblr\API\NPF\Content\Attribution
*/
abstract class AttributionObject {
use \Tumblr\API\Read\ReadableTrait;
/**
* Type of attribution.
* Used by the API to further process and embed the declared attribution.
*
* Supported and thus valid types of attribution are being provided in:
*
* Tumblr\API\NPF\Content\Attribution\AttributionTypes
*
* @var string
*/
protected $type;

/**
* AttributionObject constructor.
* @param string $type type of the provided attribution
*/
protected function __construct(string $type) {
$this->type = $type;
}
}
15 changes: 15 additions & 0 deletions lib/Tumblr/API/NPF/Content/Attribution/AttributionTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tumblr\API\NPF\Content\Attribution;

/**
* Class AttributionTypes
* Collection of constants associated with the supported types of Attribution.
* @package Tumblr\API\NPF\Content\Attribution
*/
class AttributionTypes {
const Link = "link";
const Blog = "blog";
const Post = "post";
const App = "app";
}
18 changes: 18 additions & 0 deletions lib/Tumblr/API/NPF/Content/Attribution/BlogAttribution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tumblr\API\NPF\Content\Attribution;

use Tumblr\API\NPF\Content\Attribution\AttributionObject;
use Tumblr\API\NPF\Content\Attribution\AttributionTypes;

class BlogAttribution extends AttributionObject{
/**
* @var object
*/
protected $blog;

public function __construct(object $blog) {
parent::__construct(AttributionTypes.Blog);
$this->blog = $blog;
}
}
20 changes: 20 additions & 0 deletions lib/Tumblr/API/NPF/Content/Attribution/LinkAttribution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tumblr\API\NPF\Content\Attribution;

use Tumblr\API\NPF\Content\Attribution\AttributionObject;
use Tumblr\API\NPF\Content\Attribution\AttributionTypes;

class LinkAttribution extends AttributionObject{
use Tumblr\API\NPF\Content\ValidationTrait;

/**
* @var string
*/
protected $url;

public function __construct(string $url) {
parent::__construct(AttributionTypes.Link);
$this->url = validURL($url);
}
}