Skip to content
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

Merged
merged 2 commits into from
Jun 23, 2016
Merged

Twig integration #34

merged 2 commits into from
Jun 23, 2016

Conversation

soullivaneuh
Copy link
Contributor

@soullivaneuh soullivaneuh commented Jun 21, 2016

Add an optional Twig integration to have an extension for enum labels.

  • Working extension
  • Extension tests
  • Bundle service loading
  • Bundle integration tests
  • Documentation update (for both AbstractEnum and twig integration)

@@ -70,6 +72,28 @@
}

/**
* @param callable|null $callback
* @param string|null $separator
Copy link
Owner

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?

@soullivaneuh
Copy link
Contributor Author

https://travis-ci.org/greg0ire/enum/jobs/139421537#L388

I think I have to review my plugin...

@greg0ire
Copy link
Owner

https://travis-ci.org/greg0ire/enum/jobs/139421537#L388

I think I have to review my plugin...

O_o indeed…

@soullivaneuh
Copy link
Contributor Author

soullivaneuh commented Jun 22, 2016

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",

*
* @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 = '_')
Copy link
Contributor Author

@soullivaneuh soullivaneuh Jun 22, 2016

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.

Copy link
Owner

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…

Copy link
Contributor Author

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.

@soullivaneuh
Copy link
Contributor Author

Not sure about that, but maybe we can make a enum_label_trans filter. A combination of both enum_label and trans workflow.

What do you think?

@greg0ire
Copy link
Owner

What do you think?

I was just thinking about that :) !

@soullivaneuh
Copy link
Contributor Author

In this case, we have to consider this before: #34 (comment)

@greg0ire
Copy link
Owner

In this case, we have to consider this before: #34 (comment)

Not sure about that: Here is what would be best IMO : enum_trans(object, fieldName)

The class can be retrieved from the object, and used to build the catalog name for trans. So no need for a class prefix IMO.

@soullivaneuh
Copy link
Contributor Author

And how the filter is supposed to know which enum is used? It will just get a value...

and used to build the catalog name for trans. So no need for a class prefix IMO.

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.

@greg0ire
Copy link
Owner

And how the filter is supposed to know which enum is used?

This is why I'm recommending a function with two arguments instead of a filter

and used to build the catalog name for trans. So no need for a class prefix IMO.

Maybe this could be the default, while a third argument would provide the ability to change that ? And let's drop the trans suffix, translation might not be enabled on the project

enum(object, fieldname [, catalog])

@soullivaneuh
Copy link
Contributor Author

This is why I'm recommending a function with two arguments instead of a filter

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, object will be a OrganizationMember and the value could be 500 or 1000.

So tell me how the filter, with an int as value will know this is related to OrganizationAccessLevel enum. 😉

Maybe this could be the default, while a third argument would provide the ability to change that ?

Already the case with the separator argument.

And let's drop the trans suffix, translation might not be enabled on the project

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.

@greg0ire
Copy link
Owner

greg0ire commented Jun 22, 2016

the filter

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)

@greg0ire
Copy link
Owner

Until #37 is done, the function should actually have one more argument: the enum class name

@soullivaneuh
Copy link
Contributor Author

Function or filter, the argument is the same: how the filter, with an int as value will know this is related to OrganizationAccessLevel enum?

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. 😉

@greg0ire
Copy link
Owner

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.
Copy link
Owner

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

@soullivaneuh
Copy link
Contributor Author

@greg0ire Review fixed!

@soullivaneuh
Copy link
Contributor Author

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.
Copy link
Owner

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).

@soullivaneuh
Copy link
Contributor Author

@greg0ire Done.

@soullivaneuh
Copy link
Contributor Author

Wait a little, I forgot the documentation.

@greg0ire
Copy link
Owner

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
Copy link
Owner

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;
Copy link
Owner

@greg0ire greg0ire Jun 23, 2016

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));
Copy link
Owner

@greg0ire greg0ire Jun 23, 2016

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));

Copy link
Contributor Author

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.

Copy link
Owner

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

@soullivaneuh
Copy link
Contributor Author

soullivaneuh commented Jun 23, 2016

PR rebased and documentation updated, ready to go! 👍

@greg0ire
Copy link
Owner

RTM, let's wait for Travis.

@greg0ire greg0ire merged commit 0dd3b3a into greg0ire:master Jun 23, 2016
@greg0ire
Copy link
Owner

Thanks @soullivaneuh !!!

@soullivaneuh soullivaneuh deleted the twig branch June 23, 2016 08:28
@soullivaneuh
Copy link
Contributor Author

You're welcome! Any chance for a release? :-)

@greg0ire
Copy link
Owner

Let me merge remaining PRs first, I'll release just after that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants