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

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

http://github.com/drupal/drupal
PHP | 90 lines | 23 code | 6 blank | 61 comment | 4 complexity | 10ab0dedf51fa0640c7360f1d50d3369 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\MigrateExecutableInterface;
  5. use Drupal\migrate\Row;
  6. use Drupal\migrate\MigrateException;
  7. /**
  8. * Returns a substring of the input value.
  9. *
  10. * The substr process plugin returns the portion of the input value specified by
  11. * the start and length parameters. This is a wrapper around mb_substr().
  12. *
  13. * Available configuration keys:
  14. * - start: (optional) The returned string will start this many characters after
  15. * the beginning of the string, defaults to 0.
  16. * - length: (optional) The maximum number of characters in the returned
  17. * string, defaults to NULL.
  18. *
  19. * If start is 0 and length is an integer, the start position is the
  20. * beginning of the string. If start is an integer and length is NULL, the
  21. * substring starting from the start position until the end of the string will
  22. * be returned. If start is 0 and length is NULL the entire string is returned.
  23. *
  24. * Example:
  25. *
  26. * @code
  27. * process:
  28. * new_text_field:
  29. * plugin: substr
  30. * source: some_text_field
  31. * start: 6
  32. * length: 10
  33. * @endcode
  34. * If some_text_field was 'Marie Skłodowska Curie' then
  35. * $destination['new_text_field'] would be 'Skłodowska'.
  36. *
  37. * The PHP equivalent of this is:
  38. * @code
  39. * $destination['new_text_field'] = substr($source['some_text_field'], 6, 10);
  40. * @endcode
  41. *
  42. * The substr plugin requires that the source value is not empty. If empty
  43. * values are expected, combine skip_on_empty process plugin to the pipeline:
  44. * @code
  45. * process:
  46. * new_text_field:
  47. * -
  48. * plugin: skip_on_empty
  49. * method: process
  50. * source: some_text_field
  51. * -
  52. * plugin: substr
  53. * source: some_text_field
  54. * start: 6
  55. * length: 10
  56. * @endcode
  57. *
  58. * @see \Drupal\migrate\Plugin\MigrateProcessInterface
  59. *
  60. * @MigrateProcessPlugin(
  61. * id = "substr"
  62. * )
  63. */
  64. class Substr extends ProcessPluginBase {
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  69. $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
  70. if (!is_int($start)) {
  71. throw new MigrateException('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
  72. }
  73. $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
  74. if (!is_null($length) && !is_int($length)) {
  75. throw new MigrateException('The character length configuration value should be an integer. Omit this key to capture from the start position to the end of the string.');
  76. }
  77. if (!is_string($value)) {
  78. throw new MigrateException('The input value must be a string.');
  79. }
  80. // Use optional start or length to return a portion of $value.
  81. $new_value = mb_substr($value, $start, $length);
  82. return $new_value;
  83. }
  84. }