{"action":"created","comment":{"url":"https://api.github.com/repos/Sylius/Sylius/pulls/comments/39369838","id":39369838,"diff_hunk":"@@ -0,0 +1,139 @@\n+<?php\n+\n+/*\n+ * This file is part of the Sylius package.\n+ *\n+ * (c) Paweł Jędrzejewski\n+ *\n+ * For the full copyright and license information, please view the LICENSE\n+ * file that was distributed with this source code.\n+ */\n+\n+namespace Sylius\\Bundle\\LocaleBundle\\Form\\Extension;\n+\n+use Sylius\\Bundle\\LocaleBundle\\Form\\DataTransformer\\ArrayGregorianToCalendarSystemTransformer;\n+use Sylius\\Bundle\\LocaleBundle\\Form\\DataTransformer\\StringGregorianToCalendarSystemTransformer;\n+use Sylius\\Bundle\\LocaleBundle\\Templating\\Helper\\LocaleHelper;\n+use Symfony\\Component\\Form\\FormBuilderInterface;\n+use Symfony\\Component\\Form\\FormInterface;\n+use Symfony\\Component\\Form\\FormView;\n+use Symfony\\Component\\Form\\AbstractTypeExtension;\n+\n+/**\n+ * Extension for Symfony's DateType for a localized calendar-aware date.\n+ */\n+class DateExtension extends AbstractTypeExtension {\n+\n+    /**\n+     * @var LocaleHelper\n+     */\n+    protected $localeHelper;\n+\n+    /**\n+     * @param LocaleHelper $helper\n+     */\n+    public function __construct(LocaleHelper $helper)\n+    {\n+        $this->localeHelper = $helper;\n+    }\n+\n+    /**\n+     * {@inheritdoc}\n+     */\n+    public function buildForm(FormBuilderInterface $builder, array $options)\n+    {\n+        if ($this->localeHelper->getCurrentCalendar() === 'gregorian') {\n+            // We only need to alter default behaviour when\n+            // we're having a traditional calendar system.\n+            return;\n+        }\n+\n+        if ('single_text' === $options['widget']) {\n+            // When date is in string format\n+            $builder->addViewTransformer(new StringGregorianToCalendarSystemTransformer(\n+                'Y-m-d',\n+                $this->localeHelper\n+            ));\n+\n+        } elseif ('array' === $options['input']) {\n+            // When date is in array (of `year`, `month`, `day`) format\n+            $builder->addViewTransformer(new ArrayGregorianToCalendarSystemTransformer(\n+                $this->localeHelper\n+            ));\n+        }\n+\n+        switch ($options['widget']) {\n+\n+            // If widget is type of `choice` so we need to translate choice items\n+            // stored in `choices` option of the field to their localized version.\n+            case 'choice':\n+\n+                $this->translateChoices($builder, 'year', 'Y');\n+                $this->translateChoices($builder, 'month', 'm');\n+                $this->translateChoices($builder, 'day', 'd');\n+\n+                break;\n+        }\n+    }\n+\n+    /**\n+     * {@inheritdoc}\n+     */\n+    public function finishView(FormView $view, FormInterface $form, array $options)\n+    {\n+        if ('single_text' === $options['widget']) {\n+            // Native browsers do not support different calendar systems\n+            // so we have to use \"text\" type instead of \"date\" for the input.\n+            if ($this->localeHelper->getCurrentCalendar() !== 'gregorian') {\n+                // We only need to alter default behaviour when\n+                // we're having a traditional calendar system.\n+                $view->vars['type'] = 'text';\n+            }\n+        }\n+    }\n+\n+    /**\n+     * A helper method to translate choices of a date-related field\n+     * to their localized version.\n+     *\n+     * e.g. For a year field it should\n+     *      translate 2014 (gregorian) choice to to 1393 (persian)\n+     *\n+     * @param FormBuilderInterface $builder Form builder instance\n+     * @param string $fieldName Field to translate its choices\n+     * @param string $format PHP-compatible date format to read choices as!\n+     */\n+    private function translateChoices(FormBuilderInterface $builder, $fieldName, $format)\n+    {\n+        $field   = $builder->get($fieldName);\n+\n+        $options = $field->getOptions();\n+        $type    = $field->getType()->getName();\n+\n+        if (isset($options['choices'])) {\n+            $newValues = array();\n+            $values = $options['choices'];\n+\n+            // Translate values\n+            foreach ($values as $value => $presentation) {","path":"src/Sylius/Bundle/LocaleBundle/Form/Extension/DateExtension.php","position":118,"original_position":118,"commit_id":"eb364c2db641fcfc1b76b46a13a02253c7d2472d","original_commit_id":"eb364c2db641fcfc1b76b46a13a02253c7d2472d","user":{"login":"lchrusciel","id":6213903,"avatar_url":"https://avatars.githubusercontent.com/u/6213903?v=3","gravatar_id":"","url":"https://api.github.com/users/lchrusciel","html_url":"https://github.com/lchrusciel","followers_url":"https://api.github.com/users/lchrusciel/followers","following_url":"https://api.github.com/users/lchrusciel/following{/other_user}","gists_url":"https://api.github.com/users/lchrusciel/gists{/gist_id}","starred_url":"https://api.github.com/users/lchrusciel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lchrusciel/subscriptions","organizations_url":"https://api.github.com/users/lchrusciel/orgs","repos_url":"https://api.github.com/users/lchrusciel/repos","events_url":"https://api.github.com/users/lchrusciel/events{/privacy}","received_events_url":"https://api.github.com/users/lchrusciel/received_events","type":"User","site_admin":false},"body":"I would also extract this logic to avoid nested foreach.","created_at":"2015-09-14T08:21:26Z","updated_at":"2015-09-14T08:21:26Z","html_url":"https://github.com/Sylius/Sylius/pull/3232#discussion_r39369838","pull_request_url":"https://api.github.com/repos/Sylius/Sylius/pulls/3232","_links":{"self":{"href":"https://api.github.com/repos/Sylius/Sylius/pulls/comments/39369838"},"html":{"href":"https://github.com/Sylius/Sylius/pull/3232#discussion_r39369838"},"pull_request":{"href":"https://api.github.com/repos/Sylius/Sylius/pulls/3232"}}},"pull_request":{"url":"https://api.github.com/repos/Sylius/Sylius/pulls/3232","id":44397259,"html_url":"https://github.com/Sylius/Sylius/pull/3232","diff_url":"https://github.com/Sylius/Sylius/pull/3232.diff","patch_url":"https://github.com/Sylius/Sylius/pull/3232.patch","issue_url":"https://api.github.com/repos/Sylius/Sylius/issues/3232","number":3232,"state":"open","locked":false,"title":"[PoC][LocaleBundle] Add traditional calendars support","user":{"login":"aramalipoor","id":1164589,"avatar_url":"https://avatars.githubusercontent.com/u/1164589?v=3","gravatar_id":"","url":"https://api.github.com/users/aramalipoor","html_url":"https://github.com/aramalipoor","followers_url":"https://api.github.com/users/aramalipoor/followers","following_url":"https://api.github.com/users/aramalipoor/following{/other_user}","gists_url":"https://api.github.com/users/aramalipoor/gists{/gist_id}","starred_url":"https://api.github.com/users/aramalipoor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aramalipoor/subscriptions","organizations_url":"https://api.github.com/users/aramalipoor/orgs","repos_url":"https://api.github.com/users/aramalipoor/repos","events_url":"https://api.github.com/users/aramalipoor/events{/privacy}","received_events_url":"https://api.github.com/users/aramalipoor/received_events","type":"User","site_admin":false},"body":"Based on #2275.\r\n\r\nQ  | A\r\n------------- | -------------\r\nBug fix?  | no\r\nNew feature?  | yes\r\nBC breaks? | no\r\nDeprecations? | no\r\nFixed tickets | -\r\nLicense | MIT\r\nDoc PR | -\r\n\r\nHere is a list of twig filters/functions I've added:\r\n- [x] `sylius_localized_date([date], [format], [timezone])` Twig date filter with calendar-aware conversion, based on current locale.\r\n- [x] `sylius_language([code])` Returns localized language name of current (or provided) locale code.\r\n- [x] `sylius_rtl()` Returns true if current locale code is of an RTL-type language. e.g  If current locale is RTL so that we can load RTL stylesheets and whatnot.\r\n- [x] `sylius_direction()` Returns current locale language direction. (`rtl`, `ltr`)\r\n- [x] `sylius_calendar()` Returns current locale calendar system. (`gregorian`, `persian`, `hebrew`, `islamic`, etc)\r\n\r\n* I've created a TypeExtension for Symfony's `date` FormType to support calendar-aware dates.\r\n - [x] Simple `<select>` tags picker\r\n - [ ] jQuery date picker widget","created_at":"2015-09-08T06:39:37Z","updated_at":"2015-09-14T08:21:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":"61f8f095ce1cf4ca736f505c166b4a7bd4349a99","assignee":null,"milestone":{"url":"https://api.github.com/repos/Sylius/Sylius/milestones/4","html_url":"https://github.com/Sylius/Sylius/milestones/v0.17.0","labels_url":"https://api.github.com/repos/Sylius/Sylius/milestones/4/labels","id":1104562,"number":4,"title":"v0.17.0","description":"Version v0.17.0 of Sylius platform.","creator":{"login":"pjedrzejewski","id":614970,"avatar_url":"https://avatars.githubusercontent.com/u/614970?v=3","gravatar_id":"","url":"https://api.github.com/users/pjedrzejewski","html_url":"https://github.com/pjedrzejewski","followers_url":"https://api.github.com/users/pjedrzejewski/followers","following_url":"https://api.github.com/users/pjedrzejewski/following{/other_user}","gists_url":"https://api.github.com/users/pjedrzejewski/gists{/gist_id}","starred_url":"https://api.github.com/users/pjedrzejewski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pjedrzejewski/subscriptions","organizations_url":"https://api.github.com/users/pjedrzejewski/orgs","repos_url":"https://api.github.com/users/pjedrzejewski/repos","events_url":"https://api.github.com/users/pjedrzejewski/events{/privacy}","received_events_url":"https://api.github.com/users/pjedrzejewski/received_events","type":"User","site_admin":false},"open_issues":17,"closed_issues":1,"state":"open","created_at":"2015-05-11T11:10:11Z","updated_at":"2015-09-11T07:15:39Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/Sylius/Sylius/pulls/3232/commits","review_comments_url":"https://api.github.com/repos/Sylius/Sylius/pulls/3232/comments","review_comment_url":"https://api.github.com/repos/Sylius/Sylius/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Sylius/Sylius/issues/3232/comments","statuses_url":"https://api.github.com/repos/Sylius/Sylius/statuses/eb364c2db641fcfc1b76b46a13a02253c7d2472d","head":{"label":"ravaj-group:traditional-calendar-systems","ref":"traditional-calendar-systems","sha":"eb364c2db641fcfc1b76b46a13a02253c7d2472d","user":{"login":"ravaj-group","id":12772603,"avatar_url":"https://avatars.githubusercontent.com/u/12772603?v=3","gravatar_id":"","url":"https://api.github.com/users/ravaj-group","html_url":"https://github.com/ravaj-group","followers_url":"https://api.github.com/users/ravaj-group/followers","following_url":"https://api.github.com/users/ravaj-group/following{/other_user}","gists_url":"https://api.github.com/users/ravaj-group/gists{/gist_id}","starred_url":"https://api.github.com/users/ravaj-group/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ravaj-group/subscriptions","organizations_url":"https://api.github.com/users/ravaj-group/orgs","repos_url":"https://api.github.com/users/ravaj-group/repos","events_url":"https://api.github.com/users/ravaj-group/events{/privacy}","received_events_url":"https://api.github.com/users/ravaj-group/received_events","type":"Organization","site_admin":false},"repo":{"id":37514767,"name":"Sylius","full_name":"ravaj-group/Sylius","owner":{"login":"ravaj-group","id":12772603,"avatar_url":"https://avatars.githubusercontent.com/u/12772603?v=3","gravatar_id":"","url":"https://api.github.com/users/ravaj-group","html_url":"https://github.com/ravaj-group","followers_url":"https://api.github.com/users/ravaj-group/followers","following_url":"https://api.github.com/users/ravaj-group/following{/other_user}","gists_url":"https://api.github.com/users/ravaj-group/gists{/gist_id}","starred_url":"https://api.github.com/users/ravaj-group/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ravaj-group/subscriptions","organizations_url":"https://api.github.com/users/ravaj-group/orgs","repos_url":"https://api.github.com/users/ravaj-group/repos","events_url":"https://api.github.com/users/ravaj-group/events{/privacy}","received_events_url":"https://api.github.com/users/ravaj-group/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ravaj-group/Sylius","description":"Modern e-commerce for PHP, based on the Symfony2 framework.","fork":true,"url":"https://api.github.com/repos/ravaj-group/Sylius","forks_url":"https://api.github.com/repos/ravaj-group/Sylius/forks","keys_url":"https://api.github.com/repos/ravaj-group/Sylius/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ravaj-group/Sylius/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ravaj-group/Sylius/teams","hooks_url":"https://api.github.com/repos/ravaj-group/Sylius/hooks","issue_events_url":"https://api.github.com/repos/ravaj-group/Sylius/issues/events{/number}","events_url":"https://api.github.com/repos/ravaj-group/Sylius/events","assignees_url":"https://api.github.com/repos/ravaj-group/Sylius/assignees{/user}","branches_url":"https://api.github.com/repos/ravaj-group/Sylius/branches{/branch}","tags_url":"https://api.github.com/repos/ravaj-group/Sylius/tags","blobs_url":"https://api.github.com/repos/ravaj-group/Sylius/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ravaj-group/Sylius/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ravaj-group/Sylius/git/refs{/sha}","trees_url":"https://api.github.com/repos/ravaj-group/Sylius/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ravaj-group/Sylius/statuses/{sha}","languages_url":"https://api.github.com/repos/ravaj-group/Sylius/languages","stargazers_url":"https://api.github.com/repos/ravaj-group/Sylius/stargazers","contributors_url":"https://api.github.com/repos/ravaj-group/Sylius/contributors","subscribers_url":"https://api.github.com/repos/ravaj-group/Sylius/subscribers","subscription_url":"https://api.github.com/repos/ravaj-group/Sylius/subscription","commits_url":"https://api.github.com/repos/ravaj-group/Sylius/commits{/sha}","git_commits_url":"https://api.github.com/repos/ravaj-group/Sylius/git/commits{/sha}","comments_url":"https://api.github.com/repos/ravaj-group/Sylius/comments{/number}","issue_comment_url":"https://api.github.com/repos/ravaj-group/Sylius/issues/comments{/number}","contents_url":"https://api.github.com/repos/ravaj-group/Sylius/contents/{+path}","compare_url":"https://api.github.com/repos/ravaj-group/Sylius/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ravaj-group/Sylius/merges","archive_url":"https://api.github.com/repos/ravaj-group/Sylius/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ravaj-group/Sylius/downloads","issues_url":"https://api.github.com/repos/ravaj-group/Sylius/issues{/number}","pulls_url":"https://api.github.com/repos/ravaj-group/Sylius/pulls{/number}","milestones_url":"https://api.github.com/repos/ravaj-group/Sylius/milestones{/number}","notifications_url":"https://api.github.com/repos/ravaj-group/Sylius/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ravaj-group/Sylius/labels{/name}","releases_url":"https://api.github.com/repos/ravaj-group/Sylius/releases{/id}","created_at":"2015-06-16T07:23:39Z","updated_at":"2015-08-26T09:53:57Z","pushed_at":"2015-09-14T07:46:27Z","git_url":"git://github.com/ravaj-group/Sylius.git","ssh_url":"git@github.com:ravaj-group/Sylius.git","clone_url":"https://github.com/ravaj-group/Sylius.git","svn_url":"https://github.com/ravaj-group/Sylius","homepage":"Sylius.org","size":18355,"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":"Sylius:master","ref":"master","sha":"96d81af9859b8efc21a4ae10c64cf125b93ad65b","user":{"login":"Sylius","id":719423,"avatar_url":"https://avatars.githubusercontent.com/u/719423?v=3","gravatar_id":"","url":"https://api.github.com/users/Sylius","html_url":"https://github.com/Sylius","followers_url":"https://api.github.com/users/Sylius/followers","following_url":"https://api.github.com/users/Sylius/following{/other_user}","gists_url":"https://api.github.com/users/Sylius/gists{/gist_id}","starred_url":"https://api.github.com/users/Sylius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sylius/subscriptions","organizations_url":"https://api.github.com/users/Sylius/orgs","repos_url":"https://api.github.com/users/Sylius/repos","events_url":"https://api.github.com/users/Sylius/events{/privacy}","received_events_url":"https://api.github.com/users/Sylius/received_events","type":"Organization","site_admin":false},"repo":{"id":2717549,"name":"Sylius","full_name":"Sylius/Sylius","owner":{"login":"Sylius","id":719423,"avatar_url":"https://avatars.githubusercontent.com/u/719423?v=3","gravatar_id":"","url":"https://api.github.com/users/Sylius","html_url":"https://github.com/Sylius","followers_url":"https://api.github.com/users/Sylius/followers","following_url":"https://api.github.com/users/Sylius/following{/other_user}","gists_url":"https://api.github.com/users/Sylius/gists{/gist_id}","starred_url":"https://api.github.com/users/Sylius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sylius/subscriptions","organizations_url":"https://api.github.com/users/Sylius/orgs","repos_url":"https://api.github.com/users/Sylius/repos","events_url":"https://api.github.com/users/Sylius/events{/privacy}","received_events_url":"https://api.github.com/users/Sylius/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Sylius/Sylius","description":"Modern e-commerce for PHP, based on the Symfony2 framework.","fork":false,"url":"https://api.github.com/repos/Sylius/Sylius","forks_url":"https://api.github.com/repos/Sylius/Sylius/forks","keys_url":"https://api.github.com/repos/Sylius/Sylius/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Sylius/Sylius/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Sylius/Sylius/teams","hooks_url":"https://api.github.com/repos/Sylius/Sylius/hooks","issue_events_url":"https://api.github.com/repos/Sylius/Sylius/issues/events{/number}","events_url":"https://api.github.com/repos/Sylius/Sylius/events","assignees_url":"https://api.github.com/repos/Sylius/Sylius/assignees{/user}","branches_url":"https://api.github.com/repos/Sylius/Sylius/branches{/branch}","tags_url":"https://api.github.com/repos/Sylius/Sylius/tags","blobs_url":"https://api.github.com/repos/Sylius/Sylius/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Sylius/Sylius/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Sylius/Sylius/git/refs{/sha}","trees_url":"https://api.github.com/repos/Sylius/Sylius/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Sylius/Sylius/statuses/{sha}","languages_url":"https://api.github.com/repos/Sylius/Sylius/languages","stargazers_url":"https://api.github.com/repos/Sylius/Sylius/stargazers","contributors_url":"https://api.github.com/repos/Sylius/Sylius/contributors","subscribers_url":"https://api.github.com/repos/Sylius/Sylius/subscribers","subscription_url":"https://api.github.com/repos/Sylius/Sylius/subscription","commits_url":"https://api.github.com/repos/Sylius/Sylius/commits{/sha}","git_commits_url":"https://api.github.com/repos/Sylius/Sylius/git/commits{/sha}","comments_url":"https://api.github.com/repos/Sylius/Sylius/comments{/number}","issue_comment_url":"https://api.github.com/repos/Sylius/Sylius/issues/comments{/number}","contents_url":"https://api.github.com/repos/Sylius/Sylius/contents/{+path}","compare_url":"https://api.github.com/repos/Sylius/Sylius/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Sylius/Sylius/merges","archive_url":"https://api.github.com/repos/Sylius/Sylius/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Sylius/Sylius/downloads","issues_url":"https://api.github.com/repos/Sylius/Sylius/issues{/number}","pulls_url":"https://api.github.com/repos/Sylius/Sylius/pulls{/number}","milestones_url":"https://api.github.com/repos/Sylius/Sylius/milestones{/number}","notifications_url":"https://api.github.com/repos/Sylius/Sylius/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Sylius/Sylius/labels{/name}","releases_url":"https://api.github.com/repos/Sylius/Sylius/releases{/id}","created_at":"2011-11-05T22:05:52Z","updated_at":"2015-09-13T05:28:16Z","pushed_at":"2015-09-14T07:46:27Z","git_url":"git://github.com/Sylius/Sylius.git","ssh_url":"git@github.com:Sylius/Sylius.git","clone_url":"https://github.com/Sylius/Sylius.git","svn_url":"https://github.com/Sylius/Sylius","homepage":"Sylius.org","size":66725,"stargazers_count":1934,"watchers_count":1934,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":700,"mirror_url":null,"open_issues_count":510,"forks":700,"open_issues":510,"watchers":1934,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Sylius/Sylius/pulls/3232"},"html":{"href":"https://github.com/Sylius/Sylius/pull/3232"},"issue":{"href":"https://api.github.com/repos/Sylius/Sylius/issues/3232"},"comments":{"href":"https://api.github.com/repos/Sylius/Sylius/issues/3232/comments"},"review_comments":{"href":"https://api.github.com/repos/Sylius/Sylius/pulls/3232/comments"},"review_comment":{"href":"https://api.github.com/repos/Sylius/Sylius/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Sylius/Sylius/pulls/3232/commits"},"statuses":{"href":"https://api.github.com/repos/Sylius/Sylius/statuses/eb364c2db641fcfc1b76b46a13a02253c7d2472d"}}}}