{"action":"created","comment":{"url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/comments/54846869","id":54846869,"diff_hunk":"@@ -0,0 +1,134 @@\n+<?php\n+namespace CsvViews\\FieldHandlers;\n+\n+use Cake\\Core\\Configure;\n+use CsvViews\\FieldHandlers\\BaseFieldHandler;\n+\n+class ListFieldHandler extends BaseFieldHandler\n+{\n+    const FIELD_TYPE_PATTERN = 'list:';\n+\n+    /**\n+     * Field parameters\n+     * @var array\n+     */\n+    protected $_fieldParams = ['value', 'label', 'active'];\n+\n+    /**\n+     * Method that renders list field's value.\n+     * @param  mixed  $table   name or instance of the Table\n+     * @param  string $field   field name\n+     * @param  string $data    field data\n+     * @param  array  $options field options\n+     * @return string\n+     */\n+    public function renderValue($table, $field, $data, array $options)\n+    {\n+        $result = parent::renderValue($table, $field, $data, $options);\n+        $listName = $this->_getListName($options['fieldDefinitions']['type']);\n+        $fieldOptions = $this->_getListFieldOptions($listName);\n+\n+        if (!empty($fieldOptions[$data]['label'])) {\n+            $result .= h($fieldOptions[$data]['label']);\n+        } else {\n+            $result .= $data;\n+        }\n+\n+        return $result;\n+    }\n+\n+    /**\n+     * Method that extracts list name from field type definition.\n+     * @param  string $type field type\n+     * @return string       list name\n+     */\n+    protected function _getListName($type)\n+    {\n+        $result = str_replace(static::FIELD_TYPE_PATTERN, '', $type);\n+\n+        return $result;\n+    }\n+\n+    /**\n+     * Method that retrieves list field options.\n+     * @param string $listName list name\n+     * @return array\n+     */\n+    protected function _getListFieldOptions($listName)\n+    {\n+        $result = [];\n+        $path = Configure::readOrFail('CsvListsOptions.path') . $listName . '.csv';\n+        $listData = $this->_getCsvData($path);\n+        if (!empty($listData)) {\n+            $result = $this->_prepareListOptions($listData);\n+        }\n+\n+        return $result;\n+    }\n+\n+    /**\n+     * Method that retrieves csv file path from specified directory.\n+     * @param  string $path directory to search in\n+     * @return string       csv file path\n+     */\n+    protected function _getCsvFile($path)\n+    {\n+        $result = '';\n+        if (file_exists($path)) {\n+            foreach (new \\DirectoryIterator($path) as $fileInfo) {\n+                if ($fileInfo->isFile() && 'csv' === $fileInfo->getExtension()) {\n+                    $result = $fileInfo->getPathname();\n+                    break;\n+                }\n+            }\n+        }\n+\n+        return $result;\n+    }\n+\n+    /**\n+     * Method that retrieves csv file data.\n+     * @param  string $path csv file path\n+     * @return array        csv data\n+     */\n+    protected function _getCsvData($path)\n+    {\n+        $result = [];\n+        if (file_exists($path)) {\n+            if (false !== ($handle = fopen($path, 'r'))) {\n+                while (false !== ($data = fgetcsv($handle, 0, ','))) {\n+                    $result[] = $data;\n+                }\n+                fclose($handle);\n+            }\n+        }\n+\n+        return $result;\n+    }\n+\n+    /**\n+     * Method that restructures list options csv data for better handling.\n+     * @param  array  $data csv data\n+     * @return array\n+     */\n+    protected function _prepareListOptions($data)\n+    {\n+        $result = [];\n+        $paramsCount = count($this->_fieldParams);\n+\n+        foreach ($data as $row) {\n+            $colCount = count($row);\n+            if ($colCount !== $paramsCount) {\n+                throw new \\RuntimeException(sprintf($this->_errorMessages[__FUNCTION__], $colCount, $paramsCount));","path":"src/FieldHandlers/ListFieldHandler.php","position":122,"original_position":122,"commit_id":"f16de7bb820747cb711062cc22a1120e006e4bf5","original_commit_id":"f16de7bb820747cb711062cc22a1120e006e4bf5","user":{"login":"mamchenkov","id":437928,"avatar_url":"https://avatars.githubusercontent.com/u/437928?v=3","gravatar_id":"","url":"https://api.github.com/users/mamchenkov","html_url":"https://github.com/mamchenkov","followers_url":"https://api.github.com/users/mamchenkov/followers","following_url":"https://api.github.com/users/mamchenkov/following{/other_user}","gists_url":"https://api.github.com/users/mamchenkov/gists{/gist_id}","starred_url":"https://api.github.com/users/mamchenkov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mamchenkov/subscriptions","organizations_url":"https://api.github.com/users/mamchenkov/orgs","repos_url":"https://api.github.com/users/mamchenkov/repos","events_url":"https://api.github.com/users/mamchenkov/events{/privacy}","received_events_url":"https://api.github.com/users/mamchenkov/received_events","type":"User","site_admin":false},"body":"@georgeconstantinou yeah, if the column is boolean, people can either enter '0' in there, or leave it empty (especially if it is the last column of the CSV file) .. ","created_at":"2016-03-03T08:06:40Z","updated_at":"2016-03-03T08:06:40Z","html_url":"https://github.com/QoboLtd/cakephp-csv-views/pull/14#discussion_r54846869","pull_request_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/14","_links":{"self":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/comments/54846869"},"html":{"href":"https://github.com/QoboLtd/cakephp-csv-views/pull/14#discussion_r54846869"},"pull_request":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/14"}}},"pull_request":{"url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/14","id":61421686,"html_url":"https://github.com/QoboLtd/cakephp-csv-views/pull/14","diff_url":"https://github.com/QoboLtd/cakephp-csv-views/pull/14.diff","patch_url":"https://github.com/QoboLtd/cakephp-csv-views/pull/14.patch","issue_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues/14","number":14,"state":"open","locked":false,"title":"Field handlers (task #1376)","user":{"login":"georgeconstantinou","id":1926811,"avatar_url":"https://avatars.githubusercontent.com/u/1926811?v=3","gravatar_id":"","url":"https://api.github.com/users/georgeconstantinou","html_url":"https://github.com/georgeconstantinou","followers_url":"https://api.github.com/users/georgeconstantinou/followers","following_url":"https://api.github.com/users/georgeconstantinou/following{/other_user}","gists_url":"https://api.github.com/users/georgeconstantinou/gists{/gist_id}","starred_url":"https://api.github.com/users/georgeconstantinou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/georgeconstantinou/subscriptions","organizations_url":"https://api.github.com/users/georgeconstantinou/orgs","repos_url":"https://api.github.com/users/georgeconstantinou/repos","events_url":"https://api.github.com/users/georgeconstantinou/events{/privacy}","received_events_url":"https://api.github.com/users/georgeconstantinou/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-03-02T15:58:27Z","updated_at":"2016-03-03T08:06:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4832560b14756773c0a39f17b680eafa1ecc76be","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/14/commits","review_comments_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/14/comments","review_comment_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/comments{/number}","comments_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues/14/comments","statuses_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/statuses/f16de7bb820747cb711062cc22a1120e006e4bf5","head":{"label":"QoboLtd:task-1376-field-handlers","ref":"task-1376-field-handlers","sha":"f16de7bb820747cb711062cc22a1120e006e4bf5","user":{"login":"QoboLtd","id":9074850,"avatar_url":"https://avatars.githubusercontent.com/u/9074850?v=3","gravatar_id":"","url":"https://api.github.com/users/QoboLtd","html_url":"https://github.com/QoboLtd","followers_url":"https://api.github.com/users/QoboLtd/followers","following_url":"https://api.github.com/users/QoboLtd/following{/other_user}","gists_url":"https://api.github.com/users/QoboLtd/gists{/gist_id}","starred_url":"https://api.github.com/users/QoboLtd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QoboLtd/subscriptions","organizations_url":"https://api.github.com/users/QoboLtd/orgs","repos_url":"https://api.github.com/users/QoboLtd/repos","events_url":"https://api.github.com/users/QoboLtd/events{/privacy}","received_events_url":"https://api.github.com/users/QoboLtd/received_events","type":"Organization","site_admin":false},"repo":{"id":51936274,"name":"cakephp-csv-views","full_name":"QoboLtd/cakephp-csv-views","owner":{"login":"QoboLtd","id":9074850,"avatar_url":"https://avatars.githubusercontent.com/u/9074850?v=3","gravatar_id":"","url":"https://api.github.com/users/QoboLtd","html_url":"https://github.com/QoboLtd","followers_url":"https://api.github.com/users/QoboLtd/followers","following_url":"https://api.github.com/users/QoboLtd/following{/other_user}","gists_url":"https://api.github.com/users/QoboLtd/gists{/gist_id}","starred_url":"https://api.github.com/users/QoboLtd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QoboLtd/subscriptions","organizations_url":"https://api.github.com/users/QoboLtd/orgs","repos_url":"https://api.github.com/users/QoboLtd/repos","events_url":"https://api.github.com/users/QoboLtd/events{/privacy}","received_events_url":"https://api.github.com/users/QoboLtd/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/QoboLtd/cakephp-csv-views","description":"CakePHP plugin for generating standard views based on CSV configuration","fork":false,"url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views","forks_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/forks","keys_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/keys{/key_id}","collaborators_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/teams","hooks_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/hooks","issue_events_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues/events{/number}","events_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/events","assignees_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/assignees{/user}","branches_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/branches{/branch}","tags_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/tags","blobs_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/refs{/sha}","trees_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/trees{/sha}","statuses_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/statuses/{sha}","languages_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/languages","stargazers_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/stargazers","contributors_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/contributors","subscribers_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/subscribers","subscription_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/subscription","commits_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/commits{/sha}","git_commits_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/commits{/sha}","comments_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/comments{/number}","issue_comment_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues/comments{/number}","contents_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/contents/{+path}","compare_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/compare/{base}...{head}","merges_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/merges","archive_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/downloads","issues_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues{/number}","pulls_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls{/number}","milestones_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/milestones{/number}","notifications_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/labels{/name}","releases_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/releases{/id}","deployments_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/deployments","created_at":"2016-02-17T16:18:24Z","updated_at":"2016-02-18T07:14:47Z","pushed_at":"2016-03-02T15:58:27Z","git_url":"git://github.com/QoboLtd/cakephp-csv-views.git","ssh_url":"git@github.com:QoboLtd/cakephp-csv-views.git","clone_url":"https://github.com/QoboLtd/cakephp-csv-views.git","svn_url":"https://github.com/QoboLtd/cakephp-csv-views","homepage":null,"size":61,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"QoboLtd:master","ref":"master","sha":"68b07e6dcc33502bccd9560088d460adcf29e49a","user":{"login":"QoboLtd","id":9074850,"avatar_url":"https://avatars.githubusercontent.com/u/9074850?v=3","gravatar_id":"","url":"https://api.github.com/users/QoboLtd","html_url":"https://github.com/QoboLtd","followers_url":"https://api.github.com/users/QoboLtd/followers","following_url":"https://api.github.com/users/QoboLtd/following{/other_user}","gists_url":"https://api.github.com/users/QoboLtd/gists{/gist_id}","starred_url":"https://api.github.com/users/QoboLtd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QoboLtd/subscriptions","organizations_url":"https://api.github.com/users/QoboLtd/orgs","repos_url":"https://api.github.com/users/QoboLtd/repos","events_url":"https://api.github.com/users/QoboLtd/events{/privacy}","received_events_url":"https://api.github.com/users/QoboLtd/received_events","type":"Organization","site_admin":false},"repo":{"id":51936274,"name":"cakephp-csv-views","full_name":"QoboLtd/cakephp-csv-views","owner":{"login":"QoboLtd","id":9074850,"avatar_url":"https://avatars.githubusercontent.com/u/9074850?v=3","gravatar_id":"","url":"https://api.github.com/users/QoboLtd","html_url":"https://github.com/QoboLtd","followers_url":"https://api.github.com/users/QoboLtd/followers","following_url":"https://api.github.com/users/QoboLtd/following{/other_user}","gists_url":"https://api.github.com/users/QoboLtd/gists{/gist_id}","starred_url":"https://api.github.com/users/QoboLtd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QoboLtd/subscriptions","organizations_url":"https://api.github.com/users/QoboLtd/orgs","repos_url":"https://api.github.com/users/QoboLtd/repos","events_url":"https://api.github.com/users/QoboLtd/events{/privacy}","received_events_url":"https://api.github.com/users/QoboLtd/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/QoboLtd/cakephp-csv-views","description":"CakePHP plugin for generating standard views based on CSV configuration","fork":false,"url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views","forks_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/forks","keys_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/keys{/key_id}","collaborators_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/teams","hooks_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/hooks","issue_events_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues/events{/number}","events_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/events","assignees_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/assignees{/user}","branches_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/branches{/branch}","tags_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/tags","blobs_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/refs{/sha}","trees_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/trees{/sha}","statuses_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/statuses/{sha}","languages_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/languages","stargazers_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/stargazers","contributors_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/contributors","subscribers_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/subscribers","subscription_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/subscription","commits_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/commits{/sha}","git_commits_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/git/commits{/sha}","comments_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/comments{/number}","issue_comment_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues/comments{/number}","contents_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/contents/{+path}","compare_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/compare/{base}...{head}","merges_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/merges","archive_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/downloads","issues_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues{/number}","pulls_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls{/number}","milestones_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/milestones{/number}","notifications_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/labels{/name}","releases_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/releases{/id}","deployments_url":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/deployments","created_at":"2016-02-17T16:18:24Z","updated_at":"2016-02-18T07:14:47Z","pushed_at":"2016-03-02T15:58:27Z","git_url":"git://github.com/QoboLtd/cakephp-csv-views.git","ssh_url":"git@github.com:QoboLtd/cakephp-csv-views.git","clone_url":"https://github.com/QoboLtd/cakephp-csv-views.git","svn_url":"https://github.com/QoboLtd/cakephp-csv-views","homepage":null,"size":61,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/14"},"html":{"href":"https://github.com/QoboLtd/cakephp-csv-views/pull/14"},"issue":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues/14"},"comments":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/issues/14/comments"},"review_comments":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/14/comments"},"review_comment":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/pulls/14/commits"},"statuses":{"href":"https://api.github.com/repos/QoboLtd/cakephp-csv-views/statuses/f16de7bb820747cb711062cc22a1120e006e4bf5"}}}}