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

recreate BackedEnums when applying Manipulations #3584

Merged
merged 5 commits into from
May 24, 2024

Conversation

tkempf
Copy link
Contributor

@tkempf tkempf commented Apr 16, 2024

Fixes Issue #3582

Copy link
Member

@Nielsvanpach Nielsvanpach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case to make sure this fix solves the problem?

@@ -52,6 +60,34 @@ public function isEmpty(): bool
public function apply(ImageDriver $image): void
{
foreach ($this->manipulations as $manipulationName => $parameters) {
switch($manipulationName){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a match operator instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using neither match nor switch. Just recreate all Enums if the parameter is present independent of thy type of manipulation

Suggested change
switch($manipulationName){
(isset($parameters['type'])) && $parameters['type'] = BorderType::from($parameters['type']) ;
(isset($parameters['fit'])) && $parameters['fit'] = Fit::from($parameters['fit']);
(isset($parameters['position'])) && $parameters['position'] = AlignPosition::from($parameters['position']);
(isset($parameters['colorFormat'])) && $parameters['colorFormat'] = ColorFormat::from($parameters['colorFormat']);
(isset($parameters['constraints'])) && $parameters['constraints'] = Constraint::from($parameters['constraints']);
(isset($parameters['position'])) && $parameters['position'] = CropPosition::from($parameters['position']);
(isset($parameters['fit'])) && $parameters['fit'] = Fit::from($parameters['fit']);
(isset($parameters['flip'])) && $parameters['flip'] = FlipDirection::from($parameters['flip']);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a match we be more clear in this case

@@ -52,6 +60,34 @@ public function isEmpty(): bool
public function apply(ImageDriver $image): void
{
foreach ($this->manipulations as $manipulationName => $parameters) {
switch($manipulationName){
case 'border':
(isset($parameters['type'])) && $parameters['type'] = BorderType::from($parameters['type']) ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(isset($parameters['type'])) && $parameters['type'] = BorderType::from($parameters['type']) ;
(isset($parameters['type'])) && $parameters['type'] = BorderType::from($parameters['type']);

@MrMeshok
Copy link
Contributor

MrMeshok commented May 7, 2024

There is also valid usage without named parameters

$media->manipulations = [
    'default' => [
        'border' => [
            1,
            BorderType::Expand,
            '000000'
        ]
    ]
];

I see 2 solutions there:

  1. Try to find enum from parameters array by named parameter key or numeric position e.g. BorderType::from($parameters['type'] ?? $parameters[1])
  2. Use reflection and check ImageDriver method parameters before calling it

Obviously reflection would be better, but there performance concerns, so it needs benchmarking.

I could take over this issue as this also need for my use case

@timvandijck
Copy link
Member

@tkempf thank you for your work!

@MrMeshok I'm going to already merge this PR, but if you have some more improvements on this topic I would be happy to look at a new PR from you.

@timvandijck timvandijck reopened this May 24, 2024
@timvandijck timvandijck merged commit 3afe621 into spatie:main May 24, 2024
18 checks passed
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.

None yet

5 participants