Skip to content

Commit

Permalink
Finish adding models off Project
Browse files Browse the repository at this point in the history
  • Loading branch information
jdoyle65 committed Feb 25, 2016
1 parent 84f6723 commit dd2c90a
Show file tree
Hide file tree
Showing 14 changed files with 539 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/composer.lock
/vendor
/.env
/index.php
/.idea
147 changes: 147 additions & 0 deletions src/Sifter/JsonObjectHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php namespace Sifter;

use Sifter\Model\Category;
use Sifter\Model\Milestone;
use Sifter\Model\Person;
use Sifter\Model\Project;
use Sifter\Resource\IssuesResource;

class JsonObjectHelpers {

static public function toProject($json) {
if(is_string($json)) {
$json = json_decode($json);
}

return new Project(
$json->name,
$json->primary_company_name,
$json->archived,
$json->url,
$json->issues_url,
$json->milestones_url,
$json->api_url,
$json->api_issues_url,
$json->api_milestones_url,
$json->api_categories_url,
$json->api_people_url
);
}

static public function toProjectsArray($json)
{
if(is_string($json)) {
$json = json_decode($json);
}

$projects = [];
if ( ! isset($json->projects) || ! is_array($json->projects)) {
throw new \Exception('Projects were not returned');
} else {
foreach ($json->projects as $project) {
$projects[] = self::toProject($project);
}
return $projects;
}
}

static public function toIssuesResource($json) {
if(is_string($json)) {
$json = json_decode($json);
}
return new IssuesResource(
$json->issues,
$json->page,
$json->per_page,
$json->total_pages,
$json->next_page_url,
$json->previous_page_url
);
}

static public function toMilestone($json) {
if(is_string($json)) {
$json = json_decode($json);
}
return new Milestone(
$json->name,
$json->due_date,
$json->issues_url,
$json->api_issues_url
);
}

static public function toMilestonesArray($json) {
if(is_string($json)) {
$json = json_decode($json);
}
$milestones = [];

if ( ! isset($json->milestones) || ! is_array($json->milestones)) {
throw new \Exception('Milestones were not returned');
} else {
foreach($json->milestones as $milestone) {
$milestones[] = self::toMilestone($milestone);
}
return $milestones;
}
}

static public function toCategory($json) {
if(is_string($json)) {
$json = json_decode($json);
}
return new Category(
$json->name,
$json->issues_url,
$json->api_issues_url
);
}

static public function toCategoriesArray($json) {
if(is_string($json)) {
$json = json_decode($json);
}
$categories = [];

if ( ! isset($json->categories) || ! is_array($json->categories)) {
throw new \Exception('Categories were not returned');
} else {
foreach($json->categories as $category) {
$categories[] = self::toCategory($category);
}
return $categories;
}
}

static public function toPerson($json) {
if(is_string($json)) {
$json = json_decode($json);
}
return new Person(
$json->username,
$json->first_name,
$json->last_name,
$json->email,
$json->issues_url,
$json->api_issues_url
);
}

static public function toPeopleArray($json) {
if(is_string($json)) {
$json = json_decode($json);
}
$people = [];

if ( ! isset($json->people) || ! is_array($json->people)) {
throw new \Exception('People were not returned');
} else {
foreach($json->people as $person) {
$people[] = self::toPerson($person);
}
return $people;
}
}

}
36 changes: 2 additions & 34 deletions src/Sifter/Sifter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,13 @@ public function allProjects($all = false)
{
$url = Sifter::curl()->getBaseUrl().self::PROJECTS_URL;
if($all) { $url.'?all=true'; }
Sifter::curl()->get(Sifter::curl()->getBaseUrl().self::PROJECTS_URL);
Sifter::curl()->get($url);

if (Sifter::curl()->error) {
throw new \Exception('cURL GET failed with code ' . Sifter::curl()->error_code);
} else {
$projects = $this->jsonToProjects(json_decode(Sifter::curl()->response));
$projects = JsonObjectHelpers::toProjectsArray(Sifter::curl()->response);
return $projects;
}
}


/*
* HELPER FUNCTIONS
*/

private function jsonToProjects($json)
{
$projects = [];
if ( ! isset($json->projects) || ! is_array($json->projects)) {
throw new \Exception('Projects were not returned');
} else {
foreach ($json->projects as $project) {
$projects[] = new Project(
$project->name,
$project->primary_company_name,
$project->archived,
$project->url,
$project->issues_url,
$project->milestones_url,
$project->api_url,
$project->api_issues_url,
$project->api_milestones_url,
$project->api_categories_url,
$project->api_people_url
);
}
return $projects;
}
}


}
16 changes: 16 additions & 0 deletions src/Sifter/SifterCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

use Curl\Curl;

/**
* Class SifterCurl
* @package Sifter
*/
class SifterCurl extends Curl {

/**
* @var string
*/
private $baseUrl = '';

/**
* SifterCurl constructor.
* @param $apiKey
* @param $apiSubdomain
*/
public function __construct($apiKey, $apiSubdomain)
{
parent::__construct();
Expand All @@ -14,7 +26,11 @@ public function __construct($apiKey, $apiSubdomain)
$this->setHeader('Accept', 'application/json');
}

/**
* @return string
*/
public function getBaseUrl() {
return $this->baseUrl;
}

}
49 changes: 49 additions & 0 deletions src/Sifter/model/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php namespace Sifter\Model;

class Category {

private $name;
private $issuesUrl;
private $apiIssuesUrl;

/**
* Category constructor.
* @param $name
* @param $issuesUrl
* @param $apiIssuesUrl
*/
public function __construct($name, $issuesUrl, $apiIssuesUrl)
{
$this->name = $name;
$this->issuesUrl = $issuesUrl;
$this->apiIssuesUrl = $apiIssuesUrl;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

/**
* @return mixed
*/
public function getIssuesUrl()
{
return $this->issuesUrl;
}

/**
* @return mixed
*/
public function getApiIssuesUrl()
{
return $this->apiIssuesUrl;
}




}
61 changes: 61 additions & 0 deletions src/Sifter/model/Milestone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php namespace Sifter\Model;

use Carbon\Carbon;

class Milestone {
private $name;
private $dueDate;
private $issuesUrl;
private $apiIssuesUrl;

/**
* Milestone constructor.
* @param $name
* @param $dueDate
* @param $issuesUrl
* @param $apiIssuesUrl
*/
public function __construct($name, $dueDate, $issuesUrl, $apiIssuesUrl)
{
$this->name = $name;
$this->dueDate = new Carbon($dueDate);
$this->issuesUrl = $issuesUrl;
$this->apiIssuesUrl = $apiIssuesUrl;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

/**
* @return mixed
*/
public function getDueDate()
{
return $this->dueDate->copy();
}

/**
* @return mixed
*/
public function getIssuesUrl()
{
return $this->issuesUrl;
}

/**
* @return mixed
*/
public function getApiIssuesUrl()
{
return $this->apiIssuesUrl;
}




}
Loading

0 comments on commit dd2c90a

Please sign in to comment.