Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from JacobBennett/user-config
Add GistConfig class
- Loading branch information
Showing
6 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php namespace Gistlog\Gists; | ||
|
||
use ArrayAccess; | ||
use Carbon\Carbon; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class GistConfig implements ArrayAccess | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $defaultSettings = [ | ||
'published' => false, | ||
'published_on' => null, | ||
'preview' => null | ||
]; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $settings = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $dates = ['published_on']; | ||
|
||
/** | ||
* @param array|ArrayAccess $githubGist | ||
* @return GistConfig | ||
*/ | ||
public static function fromGitHub($githubGist) | ||
{ | ||
$config = new self; | ||
$config->settings = $config->defaultSettings; | ||
|
||
if (! array_key_exists('gistlog.yml', $githubGist['files'])) { | ||
return $config; | ||
} | ||
|
||
$userSettings = Yaml::parse($githubGist['files']['gistlog.yml']['content']); | ||
|
||
if (! is_array($userSettings)) { | ||
return $config; | ||
} | ||
|
||
foreach ($config->defaultSettings as $setting => $defaultValue) { | ||
$config->settings[$setting] = array_get($userSettings, $setting, $defaultValue); | ||
} | ||
|
||
foreach ($config->dates as $setting) { | ||
|
||
if (is_null($config->settings[$setting])) { | ||
continue; | ||
} | ||
|
||
try { | ||
$config->settings[$setting] = Carbon::createFromTimestamp($config->settings[$setting]); | ||
} catch (\ErrorException $e) { | ||
$config->settings[$setting] = null; | ||
} | ||
|
||
} | ||
|
||
return $config; | ||
} | ||
|
||
|
||
/** | ||
* @param mixed $setting | ||
* @return bool | ||
*/ | ||
public function offsetExists($setting) | ||
{ | ||
return isset($this->settings[$setting]); | ||
} | ||
|
||
/** | ||
* @param mixed $setting | ||
* @return mixed | ||
*/ | ||
public function offsetGet($setting) | ||
{ | ||
return $this->settings[$setting]; | ||
} | ||
|
||
/** | ||
* @param mixed $setting | ||
* @param mixed $value | ||
*/ | ||
public function offsetSet($setting, $value) | ||
{ | ||
array_set($this->settings, $setting, $value); | ||
} | ||
|
||
/** | ||
* @param mixed $setting | ||
*/ | ||
public function offsetUnset($setting) | ||
{ | ||
array_set($this->settings, $setting, null); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
use Gistlog\Gists\GistConfig; | ||
|
||
class GistConfigTest extends TestCase | ||
{ | ||
|
||
use GistFixtureHelpers; | ||
|
||
/** @test */ | ||
public function it_can_be_created_from_github_api_data() | ||
{ | ||
$githubGist = $this->loadFixture('8f5ea4d44dbc5ccb77a3.json'); | ||
|
||
$config = GistConfig::fromGitHub($githubGist); | ||
|
||
$this->assertEquals("A test preview.", $config['preview']); | ||
$this->assertEquals("2015-05-15", $config['published_on']->format('Y-m-d')); | ||
$this->assertTrue($config['published']); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_default_values_when_no_gistlog_yml_is_present() | ||
{ | ||
$githubGist = $this->loadFixture('002ed429c7c21ab89300.json'); | ||
|
||
$config = GistConfig::fromGitHub($githubGist); | ||
|
||
$this->assertNull($config['preview']); | ||
$this->assertNull($config['published_on']); | ||
$this->assertFalse($config['published']); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_default_values_when_gistlog_yml_does_not_provide_them() | ||
{ | ||
$githubGist = $this->loadFixture('9e5ea4d44dbc5ccb77b4.json'); | ||
|
||
$config = GistConfig::fromGitHub($githubGist); | ||
|
||
$this->assertNull($config['preview']); // excluded | ||
$this->assertFalse($config['published']); // excluded | ||
$this->assertEquals("2015-05-15", $config['published_on']->format('Y-m-d')); // provided | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_a_null_value_when_a_date_value_is_invalid() | ||
{ | ||
$githubGist = $this->loadFixture('bb5ea4d44dbc5ccb77s7.json'); | ||
|
||
$config = GistConfig::fromGitHub($githubGist); | ||
|
||
$this->assertNull($config['published_on']); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
{ | ||
"url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3", | ||
"forks_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/forks", | ||
"commits_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/commits", | ||
"id": "8f5ea4d44dbc5ccb77a3", | ||
"git_pull_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3.git", | ||
"git_push_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3.git", | ||
"html_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3", | ||
"files": { | ||
"blog.MD": { | ||
"filename": "blog.MD", | ||
"type": "text/plain", | ||
"language": "Markdown", | ||
"raw_url": "https://gist.githubusercontent.com/JacobBennett/8f5ea4d44dbc5ccb77a3/raw/fb81c6b2666987c7e3556a67362ed20df76cf3a7/blog.MD", | ||
"size": 963, | ||
"truncated": false, | ||
"content": "The end goal was to order a query by a list of specific values for a column, but then also order those by a created_at date in ascending order and then by id in ascending order\n\nThis proved to be more of a challenge than I originally thought. The solution I came up with is as follows.\n\n```SQL\nSELECT id, clientId, created_at \nFROM `claims` \nWHERE `clientId` IN ('FOO', 'BAR', 'BAZ', 'FIZZ') \nORDER BY clientId = 'FIZZ', clientId = 'BAZ', clientId = 'BAR', clientId = 'FOO', \ncreated_at ASC, id ASC\n```\n\nDirect your attention to line 4. Looking at this, you might assume that the returned order of the results would be `FIZZ, BAZ, BAR, FOO` but in fact, you would be wrong.\n\nWhile normally the order by statements seem to stack on top of the previous order statement, in appears that when using an `=` to define the order, the orders stack in reverse.\n\nThat is, the last order by statement containing the `=` was the first result returned. It's the little things." | ||
}, | ||
"gistlog.yml": { | ||
"filename": "gistlog.yml", | ||
"type": "text/x-yaml", | ||
"language": "YAML", | ||
"raw_url": "https://gist.githubusercontent.com/JacobBennett/8f5ea4d44dbc5ccb77a3/raw/f5a9ef036c44acd7c8aba799cf290bdf0feb6e56/gistlog.yml", | ||
"size": 73, | ||
"truncated": false, | ||
"content": "published: true\npreview: A test preview.\npublished_on: 2015-05-15" | ||
} | ||
}, | ||
"public": true, | ||
"created_at": "2015-07-22T15:41:53Z", | ||
"updated_at": "2015-07-26T05:01:32Z", | ||
"description": "How SQL orders results when using equals", | ||
"comments": 0, | ||
"user": null, | ||
"comments_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/comments", | ||
"owner": { | ||
"login": "JacobBennett", | ||
"id": 1517011, | ||
"avatar_url": "https://avatars.githubusercontent.com/u/1517011?v=3", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/JacobBennett", | ||
"html_url": "https://github.com/JacobBennett", | ||
"followers_url": "https://api.github.com/users/JacobBennett/followers", | ||
"following_url": "https://api.github.com/users/JacobBennett/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/JacobBennett/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/JacobBennett/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/JacobBennett/subscriptions", | ||
"organizations_url": "https://api.github.com/users/JacobBennett/orgs", | ||
"repos_url": "https://api.github.com/users/JacobBennett/repos", | ||
"events_url": "https://api.github.com/users/JacobBennett/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/JacobBennett/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"forks": [], | ||
"history": [ | ||
{ | ||
"user": { | ||
"login": "JacobBennett", | ||
"id": 1517011, | ||
"avatar_url": "https://avatars.githubusercontent.com/u/1517011?v=3", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/JacobBennett", | ||
"html_url": "https://github.com/JacobBennett", | ||
"followers_url": "https://api.github.com/users/JacobBennett/followers", | ||
"following_url": "https://api.github.com/users/JacobBennett/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/JacobBennett/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/JacobBennett/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/JacobBennett/subscriptions", | ||
"organizations_url": "https://api.github.com/users/JacobBennett/orgs", | ||
"repos_url": "https://api.github.com/users/JacobBennett/repos", | ||
"events_url": "https://api.github.com/users/JacobBennett/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/JacobBennett/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"version": "5c63bf5ac93dfa4d0d62a9677ba015b34c6f9df5", | ||
"committed_at": "2015-07-26T05:01:32Z", | ||
"change_status": { | ||
"total": 3, | ||
"additions": 2, | ||
"deletions": 1 | ||
}, | ||
"url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/5c63bf5ac93dfa4d0d62a9677ba015b34c6f9df5" | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
{ | ||
"url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3", | ||
"forks_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/forks", | ||
"commits_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/commits", | ||
"id": "8f5ea4d44dbc5ccb77a3", | ||
"git_pull_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3.git", | ||
"git_push_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3.git", | ||
"html_url": "https://gist.github.com/8f5ea4d44dbc5ccb77a3", | ||
"files": { | ||
"blog.MD": { | ||
"filename": "blog.MD", | ||
"type": "text/plain", | ||
"language": "Markdown", | ||
"raw_url": "https://gist.githubusercontent.com/JacobBennett/8f5ea4d44dbc5ccb77a3/raw/fb81c6b2666987c7e3556a67362ed20df76cf3a7/blog.MD", | ||
"size": 963, | ||
"truncated": false, | ||
"content": "The end goal was to order a query by a list of specific values for a column, but then also order those by a created_at date in ascending order and then by id in ascending order\n\nThis proved to be more of a challenge than I originally thought. The solution I came up with is as follows.\n\n```SQL\nSELECT id, clientId, created_at \nFROM `claims` \nWHERE `clientId` IN ('FOO', 'BAR', 'BAZ', 'FIZZ') \nORDER BY clientId = 'FIZZ', clientId = 'BAZ', clientId = 'BAR', clientId = 'FOO', \ncreated_at ASC, id ASC\n```\n\nDirect your attention to line 4. Looking at this, you might assume that the returned order of the results would be `FIZZ, BAZ, BAR, FOO` but in fact, you would be wrong.\n\nWhile normally the order by statements seem to stack on top of the previous order statement, in appears that when using an `=` to define the order, the orders stack in reverse.\n\nThat is, the last order by statement containing the `=` was the first result returned. It's the little things." | ||
}, | ||
"gistlog.yml": { | ||
"filename": "gistlog.yml", | ||
"type": "text/x-yaml", | ||
"language": "YAML", | ||
"raw_url": "https://gist.githubusercontent.com/JacobBennett/8f5ea4d44dbc5ccb77a3/raw/f5a9ef036c44acd7c8aba799cf290bdf0feb6e56/gistlog.yml", | ||
"size": 73, | ||
"truncated": false, | ||
"content": "published_on: 2015-05-15" | ||
} | ||
}, | ||
"public": true, | ||
"created_at": "2015-07-22T15:41:53Z", | ||
"updated_at": "2015-07-26T05:01:32Z", | ||
"description": "How SQL orders results when using equals", | ||
"comments": 0, | ||
"user": null, | ||
"comments_url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/comments", | ||
"owner": { | ||
"login": "JacobBennett", | ||
"id": 1517011, | ||
"avatar_url": "https://avatars.githubusercontent.com/u/1517011?v=3", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/JacobBennett", | ||
"html_url": "https://github.com/JacobBennett", | ||
"followers_url": "https://api.github.com/users/JacobBennett/followers", | ||
"following_url": "https://api.github.com/users/JacobBennett/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/JacobBennett/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/JacobBennett/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/JacobBennett/subscriptions", | ||
"organizations_url": "https://api.github.com/users/JacobBennett/orgs", | ||
"repos_url": "https://api.github.com/users/JacobBennett/repos", | ||
"events_url": "https://api.github.com/users/JacobBennett/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/JacobBennett/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"forks": [], | ||
"history": [ | ||
{ | ||
"user": { | ||
"login": "JacobBennett", | ||
"id": 1517011, | ||
"avatar_url": "https://avatars.githubusercontent.com/u/1517011?v=3", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/JacobBennett", | ||
"html_url": "https://github.com/JacobBennett", | ||
"followers_url": "https://api.github.com/users/JacobBennett/followers", | ||
"following_url": "https://api.github.com/users/JacobBennett/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/JacobBennett/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/JacobBennett/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/JacobBennett/subscriptions", | ||
"organizations_url": "https://api.github.com/users/JacobBennett/orgs", | ||
"repos_url": "https://api.github.com/users/JacobBennett/repos", | ||
"events_url": "https://api.github.com/users/JacobBennett/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/JacobBennett/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"version": "5c63bf5ac93dfa4d0d62a9677ba015b34c6f9df5", | ||
"committed_at": "2015-07-26T05:01:32Z", | ||
"change_status": { | ||
"total": 3, | ||
"additions": 2, | ||
"deletions": 1 | ||
}, | ||
"url": "https://api.github.com/gists/8f5ea4d44dbc5ccb77a3/5c63bf5ac93dfa4d0d62a9677ba015b34c6f9df5" | ||
} | ||
] | ||
} |
Oops, something went wrong.