-
Notifications
You must be signed in to change notification settings - Fork 4
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
Twig integration #34
Twig integration #34
Conversation
@@ -70,6 +72,28 @@ | |||
} | |||
|
|||
/** | |||
* @param callable|null $callback | |||
* @param string|null $separator |
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.
Please document this. What is the callback called on? What arguments should it have? What does the separator separate?
4116aaa
to
c4696da
Compare
https://travis-ci.org/greg0ire/enum/jobs/139421537#L388 I think I have to review my plugin... |
https://travis-ci.org/greg0ire/enum/jobs/139421537#L388 I think I have to review my plugin... O_o indeed… |
Or... I have to get new eyes. diff --git a/composer.json b/composer.json
index 9eb6c0c..337b589 100644
--- a/composer.json
+++ b/composer.json
@@ -17,8 +17,8 @@
"require-dev": {
"phpunit/phpunit": "^4.1",
"sllh/php-cs-fixer-styleci-bridge": "^2.0",
- "symfony/config": " ^2.7 || ^3.0",
- "symfony/dependency-injection": " ^2.7 || ^3.0",
+ "symfony/config": "^2.7 || ^3.0",
+ "symfony/dependency-injection": "^2.7 || ^3.0",
"symfony/form": "^2.7 || ^3.0",
"symfony/http-kernel": "^2.7 || ^3.0",
"symfony/twig-bundle": "^2.7 || ^3.0", |
cf0e91d
to
b60793a
Compare
* | ||
* @return array a hash with your constants and their value. Useful for | ||
* building a choice widget | ||
*/ | ||
final public static function getConstants($keysCallback = null) | ||
final public static function getConstants($keysCallback = null, $classPrefixed = false, $namespaceSeparator = '_') |
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.
Maybe $classPrefixed
can be true
by default?
For most of the cases, translator will be used for a proper label format. Using class prefix make more sense IMHO.
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.
Most of the key, translator will be used for a proper format. Using class prefix make more sense IMHO.
Can you proofread your sentence ? It looks strange…
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.
key
-> cases
.
Don't ask me why, I don't know.
Not sure about that, but maybe we can make a What do you think? |
I was just thinking about that :) ! |
In this case, we have to consider this before: #34 (comment) |
Not sure about that: Here is what would be best IMO : The class can be retrieved from the object, and used to build the catalog name for |
And how the filter is supposed to know which enum is used? It will just get a value...
I don't agree, in this case you don't let the choice of the translations keys. This is also why I added a separator option. |
This is why I'm recommending a function with two arguments instead of a filter
Maybe this could be the default, while a third argument would provide the ability to change that ? And let's drop the
|
Example: class OrganizationMember
{
private $accessLevel = OrganizationAccessLevel::MEMBER;
} And the related enum: use Greg0ire\Enum\AbstractEnum;
final class OrganizationAccessLevel extends AbstractEnum
{
const MEMBER = 500;
const OWNER = 1000;
} With your method, So tell me how the filter, with an int as value will know this is related to
Already the case with the separator argument.
And what happen if the translator is enabled, the related key exist but the user does not want to translate it? Note: Translator always have a default catalog. |
The function. With 2 arguments. There won't be a filter in my mind. You won't pass the int to it, you will pass the object and the field name (not its value) |
Until #37 is done, the function should actually have one more argument: the enum class name |
Function or filter, the argument is the same: how the filter, with an int as value will know this is related to And for me a filter makes more sens because of transformation. The translator is a filter BTW. But again, we can do both and let the end user choose. 😉 |
Yeah sorry I was confusing both classes, see my last comment. |
@@ -194,6 +204,53 @@ $view = $this->factory->create(EnumType::class, null, array( | |||
))->createView(); | |||
``` | |||
|
|||
#### Twig extension | |||
|
|||
This package come with a `enum_label` filter, available on the `EnumExtension` Twig class. |
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.
come => comes
a => an
on => thanks to
8d61ba8
to
afbc75f
Compare
@greg0ire Review fixed! |
PR rebased and conflicts fixed. |
* - 'ø': I want 'ø' as a separator, which implies to prefix with a class. | ||
* - false: I don't want any separator, because I don't even need a prefix. | ||
* By default, the value is the configured one if the translator | ||
* is available and enabled, false otherwise. |
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.
After more consideration, this multi value argument does not look like such a good idea, sorry. Please split it back into two arguments ($classPrefix, boolean, and $separator).
@greg0ire Done. |
Wait a little, I forgot the documentation. |
Can you please rebase @soullivaneuh ? |
* string: Use the specified one | ||
* null: Use the default one | ||
* false: Do not use the translator | ||
* @param bool $classPrefixed Prefix the label with the enum class. Default to true if the translator |
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.
"Default" => "Defaults"
The goal is to extract the class prefix logic from the form type to be used elsewhere.
return $translatedLabel ?: $label; | ||
} | ||
|
||
return $label; |
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.
This is way easier to understand @soullivaneuh , good job! 👏
Add an optional Twig integration to have an extension for enum labels.
{ | ||
// Determine if the translator can be used or not. | ||
$useTranslation = $this->translator instanceof TranslatorInterface | ||
&& (is_null($translationDomain) || is_string($translationDomain)); |
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.
Linebreaking at the ||
instead would be more logical, wouldn't it?
$useTranslation = $this->translator instanceof TranslatorInterface && (is_null($translationDomain)
|| is_string($translationDomain));
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.
Honestly? No. The is_null
and is_string
are linked together for one conditon part.
This is harder to read with your way IMHO.
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.
Oh sorry, you're actually right, didn't see the parenthesis
PR rebased and documentation updated, ready to go! 👍 |
RTM, let's wait for Travis. |
Thanks @soullivaneuh !!! |
You're welcome! Any chance for a release? :-) |
Let me merge remaining PRs first, I'll release just after that. |
Add an optional Twig integration to have an extension for enum labels.
AbstractEnum
and twig integration)