{"action":"created","comment":{"url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/comments/63027642","id":63027642,"diff_hunk":"@@ -0,0 +1,124 @@\n+<?php\n+/**\n+ * Copyright 2016 Google Inc. All Rights Reserved.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *      http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+namespace Google\\Cloud\\PubSub;\n+\n+trait ResourceNameTrait\n+{\n+    /**\n+     * @var array\n+     */\n+    private $templates = [\n+        'project' => 'projects/%1$s',\n+        'topic' => 'projects/%2$s/topics/%1$s',\n+        'subscription' => 'projects/%2$s/subscriptions/%1$s'\n+    ];\n+\n+    /**\n+     * @var array\n+     */\n+    private $regexes = [\n+        'project' => '/^projects\\/([^\\/]*)$/',\n+        'topic' => '/projects\\/[^\\/]*\\/topics\\/(.*)/',\n+        'subscription' => '/projects\\/[^\\/]*\\/subscriptions\\/(.*)/'\n+    ];\n+\n+    /**\n+     * Convert a fully-qualified name into a simple name.\n+     *\n+     * ```php\n+     * <?php\n+     * use Google\\Cloud\\ServiceBuilder;\n+     *\n+     * $cloud = new ServiceBuilder([\n+     *      'projectId' => 'my-awesome-project'\n+     * ]);\n+     *\n+     * $pubsub = $cloud->pubsub();\n+     * $topic = $pubsub->topic('projects/my-awesome-project/topics/my-topic-name');\n+     * echo $topic->pluckName($name); // `my-topic-name`\n+     * ```\n+     *\n+     * @param  string $name\n+     * @return string\n+     */\n+    public function pluckName($type, $name)\n+    {\n+        if (!isset($this->regexes[$type])) {\n+            throw new \\InvalidArgumentException(sprintf(\n+                'Regex `%s` is not defined',\n+                $type\n+            ));\n+        }\n+\n+        $matches = [];\n+        $res = preg_match($this->regexes[$type], $name, $matches);\n+        return ($res === 1) ? $matches[1] : null;\n+    }\n+\n+    /**\n+     * Convert a simple name into the fully-qualified name required by\n+     * the API.\n+     *\n+     * ```php\n+     * <?php\n+     * use Google\\Cloud\\ServiceBuilder;\n+     *\n+     * $cloud = new ServiceBuilder([\n+     *      'projectId' => 'my-awesome-project'\n+     * ]);\n+     *\n+     * $pubsub = $cloud->pubsub();\n+     * $topic = $pubsub->topic('my-topic-name');\n+     * echo $topic->formatName($name); // `projects/my-awesome-project/topics/my-topic-name`\n+     * ```\n+     *\n+     * @param  string $type\n+     * @param  array  $pieces {\n+     *      Template Values\n+     *\n+     *      [0] @type string The simple name of the resource. Could be a project ID","path":"src/PubSub/ResourceNameTrait.php","position":97,"original_position":94,"commit_id":"3fbe67231246bf4a536df75b6d997b054a749bb9","original_commit_id":"4e1e60c19a5bf4fdc6a62810bdd21aed1dcc2ee9","user":{"login":"dwsupplee","id":2079879,"avatar_url":"https://avatars.githubusercontent.com/u/2079879?v=3","gravatar_id":"","url":"https://api.github.com/users/dwsupplee","html_url":"https://github.com/dwsupplee","followers_url":"https://api.github.com/users/dwsupplee/followers","following_url":"https://api.github.com/users/dwsupplee/following{/other_user}","gists_url":"https://api.github.com/users/dwsupplee/gists{/gist_id}","starred_url":"https://api.github.com/users/dwsupplee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dwsupplee/subscriptions","organizations_url":"https://api.github.com/users/dwsupplee/orgs","repos_url":"https://api.github.com/users/dwsupplee/repos","events_url":"https://api.github.com/users/dwsupplee/events{/privacy}","received_events_url":"https://api.github.com/users/dwsupplee/received_events","type":"User","site_admin":false},"body":"What do you think about this signature?\r\n\r\n```\r\npublic function formatName($type, $name, $projectId = null)\r\n```","created_at":"2016-05-12T14:16:02Z","updated_at":"2016-05-12T14:16:02Z","html_url":"https://github.com/GoogleCloudPlatform/gcloud-php/pull/39#discussion_r63027642","pull_request_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/39","_links":{"self":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/comments/63027642"},"html":{"href":"https://github.com/GoogleCloudPlatform/gcloud-php/pull/39#discussion_r63027642"},"pull_request":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/39"}}},"pull_request":{"url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/39","id":69194549,"html_url":"https://github.com/GoogleCloudPlatform/gcloud-php/pull/39","diff_url":"https://github.com/GoogleCloudPlatform/gcloud-php/pull/39.diff","patch_url":"https://github.com/GoogleCloudPlatform/gcloud-php/pull/39.patch","issue_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/issues/39","number":39,"state":"open","locked":false,"title":"Add Pubsub client, Topic and Subscription APIs","user":{"login":"jdpedrie","id":89034,"avatar_url":"https://avatars.githubusercontent.com/u/89034?v=3","gravatar_id":"","url":"https://api.github.com/users/jdpedrie","html_url":"https://github.com/jdpedrie","followers_url":"https://api.github.com/users/jdpedrie/followers","following_url":"https://api.github.com/users/jdpedrie/following{/other_user}","gists_url":"https://api.github.com/users/jdpedrie/gists{/gist_id}","starred_url":"https://api.github.com/users/jdpedrie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jdpedrie/subscriptions","organizations_url":"https://api.github.com/users/jdpedrie/orgs","repos_url":"https://api.github.com/users/jdpedrie/repos","events_url":"https://api.github.com/users/jdpedrie/events{/privacy}","received_events_url":"https://api.github.com/users/jdpedrie/received_events","type":"User","site_admin":false},"body":"- [x] Implementation of Pub/Sub API\r\n- [x] Test Coverage\r\n\r\n\r\n### Big stuff:\r\n\r\n* Preliminary pub/sub support\r\n* Add different exception types and throw exception based on http status code. (When gRPC support starts coming, this will allow us to present a standard interface to end users).\r\n* Extend my last change to the `RequestBuilder` to allow calling nested resource on a per-call basis.\r\n\r\n## Still coming:\r\n\r\n* Full test coverage is still in progress\r\n* IAM support (Some of the objects are there now, but are not implemented in the pubsub classes and will change a lot before they're ready.)\r\n* Some docblocks need more complete documentation of `array $options` possible values.\r\n\r\n## More about that `RequestBuilder` thing I mentioned above\r\n\r\nSome discovery documents nest resources fairly deeply. For instance, a call to list subscriptions by topics would look like `$schema['resources']['projects']['resources']['topics']['resources']['subscriptions']['methods']['list']`. To get to that level of nesting, we utilize the change I made to the `RequestWrapper` constructor in #38 (which applies to every resource call in the connection), and pair it with a string splitting strategy used on a per-call basis.\r\n\r\nThe end result is a connection call that looks like this:\r\n\r\n````php\r\n<?php\r\n\r\n// this is in the constructor\r\n$this->setRequestBuilder(new RequestBuilder(\r\n    __DIR__ . '/ServiceDefinition/pubsub-v1.json',\r\n    self::BASE_URI,\r\n    ['resources', 'projects']\r\n));\r\n\r\n// this is in listSubscriptionsByTopic\r\nreturn $this->send('topics.resources.subscriptions', 'list', $args);\r\n````\r\n\r\nThis isn't ready for a merge yet, but I wanted to get it out there and get some more eyes on it! Thanks for any feedback.","created_at":"2016-05-06T18:56:26Z","updated_at":"2016-05-12T14:16:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3a3867884c817a970899734568b3726be7c3d7df","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/39/commits","review_comments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/39/comments","review_comment_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/comments{/number}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/issues/39/comments","statuses_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/statuses/3fbe67231246bf4a536df75b6d997b054a749bb9","head":{"label":"jdpedrie:pubsub","ref":"pubsub","sha":"3fbe67231246bf4a536df75b6d997b054a749bb9","user":{"login":"jdpedrie","id":89034,"avatar_url":"https://avatars.githubusercontent.com/u/89034?v=3","gravatar_id":"","url":"https://api.github.com/users/jdpedrie","html_url":"https://github.com/jdpedrie","followers_url":"https://api.github.com/users/jdpedrie/followers","following_url":"https://api.github.com/users/jdpedrie/following{/other_user}","gists_url":"https://api.github.com/users/jdpedrie/gists{/gist_id}","starred_url":"https://api.github.com/users/jdpedrie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jdpedrie/subscriptions","organizations_url":"https://api.github.com/users/jdpedrie/orgs","repos_url":"https://api.github.com/users/jdpedrie/repos","events_url":"https://api.github.com/users/jdpedrie/events{/privacy}","received_events_url":"https://api.github.com/users/jdpedrie/received_events","type":"User","site_admin":false},"repo":{"id":56015430,"name":"gcloud-php","full_name":"jdpedrie/gcloud-php","owner":{"login":"jdpedrie","id":89034,"avatar_url":"https://avatars.githubusercontent.com/u/89034?v=3","gravatar_id":"","url":"https://api.github.com/users/jdpedrie","html_url":"https://github.com/jdpedrie","followers_url":"https://api.github.com/users/jdpedrie/followers","following_url":"https://api.github.com/users/jdpedrie/following{/other_user}","gists_url":"https://api.github.com/users/jdpedrie/gists{/gist_id}","starred_url":"https://api.github.com/users/jdpedrie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jdpedrie/subscriptions","organizations_url":"https://api.github.com/users/jdpedrie/orgs","repos_url":"https://api.github.com/users/jdpedrie/repos","events_url":"https://api.github.com/users/jdpedrie/events{/privacy}","received_events_url":"https://api.github.com/users/jdpedrie/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jdpedrie/gcloud-php","description":"Google Cloud Client Library for PHP","fork":true,"url":"https://api.github.com/repos/jdpedrie/gcloud-php","forks_url":"https://api.github.com/repos/jdpedrie/gcloud-php/forks","keys_url":"https://api.github.com/repos/jdpedrie/gcloud-php/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jdpedrie/gcloud-php/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jdpedrie/gcloud-php/teams","hooks_url":"https://api.github.com/repos/jdpedrie/gcloud-php/hooks","issue_events_url":"https://api.github.com/repos/jdpedrie/gcloud-php/issues/events{/number}","events_url":"https://api.github.com/repos/jdpedrie/gcloud-php/events","assignees_url":"https://api.github.com/repos/jdpedrie/gcloud-php/assignees{/user}","branches_url":"https://api.github.com/repos/jdpedrie/gcloud-php/branches{/branch}","tags_url":"https://api.github.com/repos/jdpedrie/gcloud-php/tags","blobs_url":"https://api.github.com/repos/jdpedrie/gcloud-php/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jdpedrie/gcloud-php/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jdpedrie/gcloud-php/git/refs{/sha}","trees_url":"https://api.github.com/repos/jdpedrie/gcloud-php/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jdpedrie/gcloud-php/statuses/{sha}","languages_url":"https://api.github.com/repos/jdpedrie/gcloud-php/languages","stargazers_url":"https://api.github.com/repos/jdpedrie/gcloud-php/stargazers","contributors_url":"https://api.github.com/repos/jdpedrie/gcloud-php/contributors","subscribers_url":"https://api.github.com/repos/jdpedrie/gcloud-php/subscribers","subscription_url":"https://api.github.com/repos/jdpedrie/gcloud-php/subscription","commits_url":"https://api.github.com/repos/jdpedrie/gcloud-php/commits{/sha}","git_commits_url":"https://api.github.com/repos/jdpedrie/gcloud-php/git/commits{/sha}","comments_url":"https://api.github.com/repos/jdpedrie/gcloud-php/comments{/number}","issue_comment_url":"https://api.github.com/repos/jdpedrie/gcloud-php/issues/comments{/number}","contents_url":"https://api.github.com/repos/jdpedrie/gcloud-php/contents/{+path}","compare_url":"https://api.github.com/repos/jdpedrie/gcloud-php/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jdpedrie/gcloud-php/merges","archive_url":"https://api.github.com/repos/jdpedrie/gcloud-php/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jdpedrie/gcloud-php/downloads","issues_url":"https://api.github.com/repos/jdpedrie/gcloud-php/issues{/number}","pulls_url":"https://api.github.com/repos/jdpedrie/gcloud-php/pulls{/number}","milestones_url":"https://api.github.com/repos/jdpedrie/gcloud-php/milestones{/number}","notifications_url":"https://api.github.com/repos/jdpedrie/gcloud-php/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jdpedrie/gcloud-php/labels{/name}","releases_url":"https://api.github.com/repos/jdpedrie/gcloud-php/releases{/id}","deployments_url":"https://api.github.com/repos/jdpedrie/gcloud-php/deployments","created_at":"2016-04-11T23:35:07Z","updated_at":"2016-04-11T23:47:30Z","pushed_at":"2016-05-12T13:46:37Z","git_url":"git://github.com/jdpedrie/gcloud-php.git","ssh_url":"git@github.com:jdpedrie/gcloud-php.git","clone_url":"https://github.com/jdpedrie/gcloud-php.git","svn_url":"https://github.com/jdpedrie/gcloud-php","homepage":null,"size":193,"stargazers_count":1,"watchers_count":1,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"GoogleCloudPlatform:master","ref":"master","sha":"f6a8283be022e32d22b56dc62e3fed5489c53d9d","user":{"login":"GoogleCloudPlatform","id":2810941,"avatar_url":"https://avatars.githubusercontent.com/u/2810941?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleCloudPlatform","html_url":"https://github.com/GoogleCloudPlatform","followers_url":"https://api.github.com/users/GoogleCloudPlatform/followers","following_url":"https://api.github.com/users/GoogleCloudPlatform/following{/other_user}","gists_url":"https://api.github.com/users/GoogleCloudPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleCloudPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleCloudPlatform/subscriptions","organizations_url":"https://api.github.com/users/GoogleCloudPlatform/orgs","repos_url":"https://api.github.com/users/GoogleCloudPlatform/repos","events_url":"https://api.github.com/users/GoogleCloudPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleCloudPlatform/received_events","type":"Organization","site_admin":false},"repo":{"id":43642389,"name":"gcloud-php","full_name":"GoogleCloudPlatform/gcloud-php","owner":{"login":"GoogleCloudPlatform","id":2810941,"avatar_url":"https://avatars.githubusercontent.com/u/2810941?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleCloudPlatform","html_url":"https://github.com/GoogleCloudPlatform","followers_url":"https://api.github.com/users/GoogleCloudPlatform/followers","following_url":"https://api.github.com/users/GoogleCloudPlatform/following{/other_user}","gists_url":"https://api.github.com/users/GoogleCloudPlatform/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleCloudPlatform/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleCloudPlatform/subscriptions","organizations_url":"https://api.github.com/users/GoogleCloudPlatform/orgs","repos_url":"https://api.github.com/users/GoogleCloudPlatform/repos","events_url":"https://api.github.com/users/GoogleCloudPlatform/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleCloudPlatform/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/GoogleCloudPlatform/gcloud-php","description":"Google Cloud Client Library for PHP","fork":false,"url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php","forks_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/forks","keys_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/keys{/key_id}","collaborators_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/teams","hooks_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/hooks","issue_events_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/issues/events{/number}","events_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/events","assignees_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/assignees{/user}","branches_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/branches{/branch}","tags_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/tags","blobs_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/git/refs{/sha}","trees_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/git/trees{/sha}","statuses_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/statuses/{sha}","languages_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/languages","stargazers_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/stargazers","contributors_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/contributors","subscribers_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/subscribers","subscription_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/subscription","commits_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/commits{/sha}","git_commits_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/git/commits{/sha}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/comments{/number}","issue_comment_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/issues/comments{/number}","contents_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/contents/{+path}","compare_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/compare/{base}...{head}","merges_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/merges","archive_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/downloads","issues_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/issues{/number}","pulls_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls{/number}","milestones_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/milestones{/number}","notifications_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/labels{/name}","releases_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/releases{/id}","deployments_url":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/deployments","created_at":"2015-10-04T16:09:46Z","updated_at":"2016-05-10T16:13:26Z","pushed_at":"2016-05-12T13:46:37Z","git_url":"git://github.com/GoogleCloudPlatform/gcloud-php.git","ssh_url":"git@github.com:GoogleCloudPlatform/gcloud-php.git","clone_url":"https://github.com/GoogleCloudPlatform/gcloud-php.git","svn_url":"https://github.com/GoogleCloudPlatform/gcloud-php","homepage":null,"size":190,"stargazers_count":19,"watchers_count":19,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":6,"forks":10,"open_issues":6,"watchers":19,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/39"},"html":{"href":"https://github.com/GoogleCloudPlatform/gcloud-php/pull/39"},"issue":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/issues/39"},"comments":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/issues/39/comments"},"review_comments":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/39/comments"},"review_comment":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/pulls/39/commits"},"statuses":{"href":"https://api.github.com/repos/GoogleCloudPlatform/gcloud-php/statuses/3fbe67231246bf4a536df75b6d997b054a749bb9"}}}}