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 GistConfig class #68
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php namespace Gistlog\Gists; | ||
|
||
use Illuminate\Support\Arr; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class GistConfig implements \ArrayAccess | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $default_settings = array( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use short array syntax here and on :21 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we've done camelCase everywhere else in the app, both for properties and for inline variables There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this include the for example: $defaultSettings = [
'published',
'publishedOn',
'preview',
]; |
||
'published' => false, | ||
'published_on' => null, | ||
'preview' => null | ||
); | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $settings = array(); | ||
|
||
/** | ||
* @param array|ArrayAccess $githubGist | ||
* @return GistConfig | ||
*/ | ||
public static function fromGitHub($githubGist) | ||
{ | ||
$config = new self; | ||
$config->settings = $config->default_settings; | ||
|
||
if (! array_key_exists('gistlog.yml', $githubGist['files'])) { | ||
return $config; | ||
} | ||
|
||
$user_settings = Yaml::parse($githubGist['files']['gistlog.yml']['content']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline after please :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any contexts in which There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did some checking on this over at https://developer.github.com/v3/gists and I didn't see any cases in which that would be a concern. Should always contain the |
||
if (! is_array($user_settings)) { | ||
return $config; | ||
} | ||
|
||
foreach ($config->default_settings as $setting => $default_value) { | ||
$config->settings[$setting] = Arr::get($user_settings, $setting, $default_value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we've been using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed all those out and removed that dependency |
||
} | ||
|
||
return $config; | ||
} | ||
|
||
|
||
/** | ||
* @param mixed $offset | ||
* @return bool | ||
*/ | ||
public function offsetExists($offset) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. boilerplate... |
||
{ | ||
return isset($this->settings[$offset]); | ||
} | ||
|
||
/** | ||
* @param mixed $offset | ||
* @return mixed | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
return $this->settings[$offset]; | ||
} | ||
|
||
/** | ||
* @param mixed $offset | ||
* @param mixed $value | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
Arr::set($this->settings, $offset, $value); | ||
} | ||
|
||
/** | ||
* @param mixed $offset | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
Arr::set($this->settings, $offset, null); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected 1 newline at end of file; 0 found |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?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("05-15-2015", $config['published_on']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, if we're choosing any date format I would choose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call on this http://symfony.com/legacy/doc/reference/1_3/en/02-YAML#chapter_02_sub_dates Specifies that |
||
$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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method name |
||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opening brace indented incorrectly; expected 0 spaces, found 4 |
||
$githubGist = $this->loadFixture('9e5ea4d44dbc5ccb77b4.json'); | ||
|
||
$config = GistConfig::fromGitHub($githubGist); | ||
|
||
$this->assertNull($config['preview']); // excluded | ||
$this->assertFalse($config['published']); // excluded | ||
$this->assertEquals("05-15-2015", $config['published_on']); // provided | ||
} | ||
} |
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: 05-15-2015" | ||
} | ||
}, | ||
"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" | ||
} | ||
] | ||
} |
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: 05-15-2015" | ||
} | ||
}, | ||
"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" | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you
use
ArrayAccess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍