diff --git a/src/Transformation/Adjustment/Opacity.php b/src/Transformation/Adjustment/Opacity.php index ae057026..ad79b704 100644 --- a/src/Transformation/Adjustment/Opacity.php +++ b/src/Transformation/Adjustment/Opacity.php @@ -24,4 +24,5 @@ */ class Opacity extends BaseQualifier implements AdjustmentInterface { + const VALUE_CLASS = OpacityValue::class; } diff --git a/src/Transformation/Adjustment/OpacityValue.php b/src/Transformation/Adjustment/OpacityValue.php new file mode 100644 index 00000000..a7dadde0 --- /dev/null +++ b/src/Transformation/Adjustment/OpacityValue.php @@ -0,0 +1,22 @@ +value); + $value = (string)$this->value; /** @noinspection TypeUnsafeComparisonInspection */ return $value == '' ? '' : self::getKey() . static::KEY_VALUE_DELIMITER . $value; diff --git a/src/Transformation/Qualifier/QualifierValue/NormalizeQualifierMultiValueTrait.php b/src/Transformation/Qualifier/QualifierValue/NormalizeQualifierMultiValueTrait.php new file mode 100644 index 00000000..9755aeb8 --- /dev/null +++ b/src/Transformation/Qualifier/QualifierValue/NormalizeQualifierMultiValueTrait.php @@ -0,0 +1,31 @@ +normalize(ArrayUtils::implodeFiltered(static::VALUE_DELIMITER, [$values, $namedValues])); + } + + /** + * Normalizes a given value. + * + * Empty implementation, can be overridden in child classes. + * + * @param $value + * + * @return string + */ + protected function normalize($value) + { + return $value; } /** diff --git a/tests/Unit/Transformation/TransformationTest.php b/tests/Unit/Transformation/TransformationTest.php index 0b208f72..b33a12da 100644 --- a/tests/Unit/Transformation/TransformationTest.php +++ b/tests/Unit/Transformation/TransformationTest.php @@ -42,6 +42,7 @@ use Cloudinary\Transformation\Position; use Cloudinary\Transformation\PsdTools; use Cloudinary\Transformation\Qualifier; +use Cloudinary\Transformation\QualifiersAction; use Cloudinary\Transformation\Quality; use Cloudinary\Transformation\Resize; use Cloudinary\Transformation\Rotate; @@ -58,6 +59,23 @@ */ final class TransformationTest extends UnitTestCase { + const SOURCE_VALUE = "width * 2"; + const NORMALIZED_VALUE = "w_mul_2"; + const NORMALIZED_PARAMETERS = [ + 'angle', + 'aspect_ratio', + 'dpr', + 'effect', + 'height', + 'opacity', + 'quality', + 'radius', + 'width', + 'x', + 'y', + 'zoom', + ]; + public function testTransformation() { $t = new Transformation(); @@ -625,4 +643,77 @@ public function testUserVariableNamesContainingPredefinedNamesAreNotAffected() (string)$transformation ); } + + /** + * Data provider of `testNormalizationPredefinedParameters()` method. + * + * @return array[] + */ + public function normalizationParametersDataProvider() + { + return array_map( + static function ($value) { + return ['param' => $value]; + }, + self::NORMALIZED_PARAMETERS + ); + } + + /** + * Data provider of `testNonNormalizationPredefinedParameters()` method. + * + * @return array[] + */ + public function nonNormalizationParametersDataProvider() + { + return array_map( + static function ($value) { + return ['param' => $value]; + }, + array_diff( + array_merge( + array_keys(QualifiersAction::SIMPLE_QUALIFIERS), + [ + 'video_codec', + 'overlay', + 'underlay', + ] + ), + array_merge( + self::NORMALIZED_PARAMETERS, + ['if'] + ) + ) + ); + } + + /** + * Tests verify that normalize predefined parameters. + * + * @dataProvider normalizationParametersDataProvider + * + * @param $param + */ + public function testNormalizationPredefinedParameters($param) + { + $transformation = (string)new Transformation([$param => self::SOURCE_VALUE, 'crop' => 'scale']); + + self::assertContains(self::NORMALIZED_VALUE, $transformation, 'should normalize value of #' . $param); + self::assertNotContains(self::SOURCE_VALUE, $transformation, 'should normalize value of #' . $param); + } + + /** + * Tests verify that doesn't normalize predefined parameters. + * + * @dataProvider nonNormalizationParametersDataProvider + * + * @param $param + */ + public function testNonNormalizationPredefinedParameters($param) + { + $transformation = (string)new Transformation([$param => self::SOURCE_VALUE]); + self::assertContains(self::SOURCE_VALUE, $transformation, 'should not normalize value of #' . $param); + self::assertNotContains(self::NORMALIZED_VALUE, $transformation, 'should not normalize value of #' . $param); + } + }