PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/migrate/src/Plugin/migrate/process/Explode.php

http://github.com/drupal/drupal
PHP | 132 lines | 31 code | 9 blank | 92 comment | 7 complexity | 60f63fee123d17487ace0a0e91f16555 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\migrate\Plugin\migrate\process;
  3. use Drupal\migrate\ProcessPluginBase;
  4. use Drupal\migrate\MigrateException;
  5. use Drupal\migrate\MigrateExecutableInterface;
  6. use Drupal\migrate\Row;
  7. /**
  8. * Splits the source string into an array of strings, using a delimiter.
  9. *
  10. * This plugin creates an array of strings by splitting the source parameter on
  11. * boundaries formed by the delimiter.
  12. *
  13. * Available configuration keys:
  14. * - source: The source string.
  15. * - limit: (optional)
  16. * - If limit is set and positive, the returned array will contain a maximum
  17. * of limit elements with the last element containing the rest of string.
  18. * - If limit is set and negative, all components except the last -limit are
  19. * returned.
  20. * - If the limit parameter is zero, then this is treated as 1.
  21. * - delimiter: The boundary string.
  22. * - strict: (optional) When this boolean is TRUE, the source should be strictly
  23. * a string. If FALSE is passed, the source value is casted to a string before
  24. * being split. Also, in this case, the values casting to empty strings are
  25. * converted to empty arrays, instead of an array with a single empty string
  26. * item ['']. Defaults to TRUE.
  27. *
  28. * Example:
  29. *
  30. * @code
  31. * process:
  32. * bar:
  33. * plugin: explode
  34. * source: foo
  35. * delimiter: /
  36. * @endcode
  37. *
  38. * If foo is "node/1", then bar will be ['node', '1']. The PHP equivalent of
  39. * this would be:
  40. *
  41. * @code
  42. * $bar = explode('/', $foo);
  43. * @endcode
  44. *
  45. * @code
  46. * process:
  47. * bar:
  48. * plugin: explode
  49. * source: foo
  50. * limit: 1
  51. * delimiter: /
  52. * @endcode
  53. *
  54. * If foo is "node/1/edit", then bar will be ['node', '1/edit']. The PHP
  55. * equivalent of this would be:
  56. *
  57. * @code
  58. * $bar = explode('/', $foo, 1);
  59. * @endcode
  60. *
  61. * If the 'strict' configuration is set to FALSE, the input value is casted to a
  62. * string before being spilt:
  63. *
  64. * @code
  65. * process:
  66. * bar:
  67. * plugin: explode
  68. * source: foo
  69. * delimiter: /
  70. * strict: false
  71. * @endcode
  72. *
  73. * If foo is 123 (as integer), then bar will be ['123']. If foo is TRUE, then
  74. * bar will be ['1']. The PHP equivalent of this would be:
  75. *
  76. * @code
  77. * $bar = explode('/', (string) 123);
  78. * $bar = explode('/', (string) TRUE);
  79. * @endcode
  80. *
  81. * If the 'strict' configuration is set to FALSE, the source value casting to
  82. * an empty string are converted to an empty array. For example, with the last
  83. * configuration, if foo is '', NULL or FALSE, then bar will be [].
  84. *
  85. * @see \Drupal\migrate\Plugin\MigrateProcessInterface
  86. *
  87. * @MigrateProcessPlugin(
  88. * id = "explode"
  89. * )
  90. */
  91. class Explode extends ProcessPluginBase {
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  96. if (empty($this->configuration['delimiter'])) {
  97. throw new MigrateException('delimiter is empty');
  98. }
  99. $strict = array_key_exists('strict', $this->configuration) ? $this->configuration['strict'] : TRUE;
  100. if ($strict && !is_string($value)) {
  101. throw new MigrateException(sprintf('%s is not a string', var_export($value, TRUE)));
  102. }
  103. elseif (!$strict) {
  104. // Check if the incoming value can cast to a string.
  105. $original = $value;
  106. if (!is_string($original) && ($original != ($value = @strval($value)))) {
  107. throw new MigrateException(sprintf('%s cannot be casted to a string', var_export($original, TRUE)));
  108. }
  109. // Empty strings should be exploded to empty arrays.
  110. if ($value === '') {
  111. return [];
  112. }
  113. }
  114. $limit = isset($this->configuration['limit']) ? $this->configuration['limit'] : PHP_INT_MAX;
  115. return explode($this->configuration['delimiter'], $value, $limit);
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function multiple() {
  121. return TRUE;
  122. }
  123. }