diff -u b/src/Plugin/migrate/process/CallbackArray.php b/src/Plugin/migrate/process/CallbackArray.php --- b/src/Plugin/migrate/process/CallbackArray.php +++ b/src/Plugin/migrate/process/CallbackArray.php @@ -13,6 +13,11 @@ * parameters, whereas the original Callback plugin only calls call_user_func() * with the source value. * + * Available configuration keys: + * - callable: The function name to call. + * - index: position where use the input value from the source. + * - allow_constant: To allow constant values, without it, the constants values + * won't be replaced. * Example: * * @code @@ -65,7 +70,10 @@ * {@inheritdoc} */ public function __construct(array $configuration, $plugin_id, $plugin_definition) { - $configuration += ['index' => 0]; + $configuration += [ + 'index' => 0, + ]; + if (is_null($configuration['parameters'])) { throw new \InvalidArgumentException('The "parameters" must be set.'); } @@ -90,9 +98,14 @@ foreach ($this->configuration['parameters'] as $parameter) { - $parameters[] = $row->get($parameter) ? $row->get($parameter) : $parameter; + $parameter = $row->get($parameter) ? $row->get($parameter) : $parameter; + + // Allow the use of PHP constants. + if (isset($this->configuration['allow_constant']) && defined($parameter)) { + $parameter = constant($parameter); + } + $parameters[] = $parameter; } array_splice($parameters, $this->configuration['index'], 0, [$value]); return call_user_func_array($this->configuration['callable'], $parameters); } - } diff -u b/tests/src/Unit/process/CallbackArrayTest.php b/tests/src/Unit/process/CallbackArrayTest.php --- b/tests/src/Unit/process/CallbackArrayTest.php +++ b/tests/src/Unit/process/CallbackArrayTest.php @@ -27,6 +27,7 @@ $this->propertyName => 'text_replaced', 'constants' => [ 'type' => 'example_type', + 'sort_regular' => 'SORT_REGULAR' ], ]); } @@ -39,7 +40,8 @@ $configuration = [ 'parameters' => NULL, ]; - $this->setExpectedException(InvalidArgumentException::class, 'The "parameters" must be set.'); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The "parameters" must be set.'); new CallbackArray($configuration, 'callback_array', []); } @@ -50,7 +52,8 @@ $configuration = [ 'parameters' => 'bad way', ]; - $this->setExpectedException(InvalidArgumentException::class, 'The "parameters" must be a non-empty array.'); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The "parameters" must be a non-empty array.'); new CallbackArray($configuration, 'callback_array', []); } @@ -64,7 +67,8 @@ ], 'index' => 2, ]; - $this->setExpectedException(InvalidArgumentException::class, 'The "index" may not exceed the amount of "parameters".'); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The "index" may not exceed the amount of "parameters".'); new CallbackArray($configuration, 'callback_array', []); } @@ -92,6 +96,54 @@ } /** + * Test the transform method with constant values. + */ + public function testTransformWithConstant() { + + // Base case using strings. + $value = ["a" => "2", "red", 1 => 2, "blue", "red"]; + $expected = ['a' => '2', 0 => 'red', 1 => 2, 2 => 'blue']; + + $configuration = [ + 'callable' => 'array_unique', + 'index' => 0, + 'allow_constant' => '1', + 'parameters' => [ + 'SORT_REGULAR', + ], + ]; + + $this->plugin = new CallbackArray($configuration, 'callback_array', []); + $value = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame($expected, $value); + } + + /** + * Test the transform method with constant values. + */ + public function testTransformWithoutConstant() { + + $value = ["a" => "2", "red", 1 => 2, "blue", "red"]; + $configuration = [ + 'callable' => 'array_unique', + 'index' => 0, + 'parameters' => [ + 'SORT_REGULAR', + ], + ]; + + // SORT_REGULAR as string is not a valid value for array_unique. + $this->plugin = new CallbackArray($configuration, 'callback_array', []); + try { + $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + catch (\Exception $e) { + $this->assertEquals('array_unique() expects parameter 2 to be int, string given', $e->getMessage()); + } + + } + + /** * Test the transform method using params as inputs. */ public function testTransformWithSourceParams() {