Created
September 17, 2019 20:20
-
-
Save AdnanHussainTurki/c57e5099a180619da023be6217dd3989 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @Author: Adnan Hussain Turki | |
* @Website: www.myphpnotes.com | |
* @Email Address: [email protected] | |
===================================== | |
* @Creation Time: 2019-09-06 19:47:42 | |
* @Last Modified by: Adnan | |
* @Last Modified time: 2019-09-18 01:00:35 | |
===================================== | |
PROPERTY OF WWW.MYPHPNOTES.COM | |
*/ | |
namespace myPHPnotes; | |
/** | |
* | |
*/ | |
class Gist | |
{ | |
protected $token; | |
protected $username; | |
protected $ssl; | |
protected $host = "https://api.github.com"; | |
function __construct(string $username = null, string $token = null, $ssl = true) | |
{ | |
$this->username = $username; | |
$this->token = $token; | |
$this->ssl = $ssl; | |
} | |
public function getPublicGists($page = 1, $perPage = 100) | |
{ | |
return $this->curl($this->host . "/gists", ["page" => $page, 'per_page' => $perPage]); | |
} | |
public function getPublicStarred() | |
{ | |
// TO BE BUILT | |
} | |
public function myGists($page = 1, $perPage = 100) | |
{ | |
$this->checkAuth(); | |
return $this->curl($this->host . "/users/{$this->username}/gists", ["access_token" => $this->token, "page" => $page, 'per_page' => $perPage]); | |
} | |
public function view(string $gist_id) | |
{ | |
return $this->curl($this->host . "/gists/" . $gist_id, ["access_token" => $this->token]); | |
} | |
public function viewPrivate(string $gist_id) | |
{ | |
$this->checkAuth(); | |
return $this->curl($this->host . "/gists/" . $gist_id, ["access_token" => $this->token]); | |
} | |
public function create(GistObject $gist) | |
{ | |
$this->checkAuth(); | |
return $this->curl($this->host . "/gists?access_token=" . $this->token, json_encode($gist, JSON_FORCE_OBJECT), "application/json", true); | |
} | |
public function delete(string $gist_id) | |
{ | |
$this->checkAuth(); | |
return $this->curl($this->host . "/gists/{$gist_id}?access_token=" . $this->token, [], "application/json", false, false, true); | |
} | |
public function edit(string $gist_id, GistObject $gist) | |
{ | |
// PRIVACY OF THE GIST CANNOT BE CHANGED BY THIS | |
$this->checkAuth(); | |
return $this->curl($this->host . "/gists/{$gist_id}?access_token=" . $this->token, json_encode($gist, JSON_FORCE_OBJECT), "application/json", false, true); | |
} | |
protected function curl($url, $parameters = [], $content_type = "application/json", $post = false, $patch = false, $delete = false) | |
{ | |
$ch = curl_init(); | |
if ($post) { | |
curl_setopt($ch, CURLOPT_POST, $post); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); | |
} elseif($patch) { | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); | |
} elseif($delete) { | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); | |
} else { | |
$http_parameters = http_build_query($parameters); | |
$joiner = "?"; | |
if (stripos($url, "?")) { | |
$joiner = "&"; | |
} | |
$url.= $joiner . $http_parameters; | |
} | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$headers = []; | |
$headers[] = "Content-Type: {$content_type}"; | |
$headers[] = "user-agent: myPHPnotes PHP wrapper"; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$result = curl_exec($ch); | |
return $result; | |
} | |
protected function checkAuth() { | |
if (!$this->username or !$this->token) { | |
die("UNAUTHORISED"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace myPHPnotes; | |
use JsonSerializable; | |
class GistObject implements JsonSerializable | |
{ | |
protected $objectArray; | |
function __construct(string $description, bool $public) | |
{ | |
$this->objectArray['description'] = $description; | |
$this->objectArray['public'] = $public; | |
} | |
public function addContent(string $file_name, string $file_content) | |
{ | |
$this->objectArray['files'][$file_name] = ["content" =>$file_content]; | |
} | |
public function removeContent(string $filename) | |
{ | |
if (isset($this->objectArray['files'][$filename])) { | |
unset($this->objectArray['files'][$filename]); | |
} else { | |
die("Content with this filename not found. File Name: " . $filename); | |
} | |
} | |
public function jsonSerialize() { | |
if ((!isset($this->objectArray['files'])) OR (count($this->objectArray['files']) === 0)) { | |
die("Gist Object does not contains any content."); | |
} | |
return $this->objectArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment