View source
<?php
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
class DomStrReplace extends DomProcessBase {
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configuration += [
'case_insensitive' => FALSE,
'regex' => FALSE,
];
$options_validation = [
'xpath' => NULL,
'mode' => [
'attribute' => [
'attribute_options' => NULL,
],
'element' => [],
],
'search' => NULL,
'replace' => NULL,
];
foreach ($options_validation as $option_name => $possible_values) {
if (empty($this->configuration[$option_name])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option '{$option_name}' is required.");
}
if (!empty($possible_values) && !array_key_exists($this->configuration[$option_name], $possible_values)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), sprintf('Configuration option "%s" only accepts the following values: %s.', $option_name, implode(', ', array_keys($possible_values))));
}
}
$mode = $this->configuration['mode'];
$mode_validation = $options_validation['mode'][$mode];
foreach ($mode_validation as $option_name => $possible_values) {
if (empty($this->configuration[$option_name])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option '{$option_name}' is required for mode '{$mode}'.");
}
if (!is_null($possible_values) && !in_array($this->configuration[$option_name], $possible_values)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), sprintf('Configuration option "%s" only accepts the following values: %s.', $option_name, implode(', ', $possible_values)));
}
}
$mode_validation = $options_validation['mode'][$this->configuration['mode']];
foreach ($mode_validation as $option_name => $possible_values) {
if (empty($this->configuration[$option_name])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option '{$option_name}' is required for mode '{$mode}'.");
}
if (!is_null($possible_values) && !in_array($this->configuration[$option_name], $possible_values)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), sprintf('Configuration option "%s" only accepts the following values: %s.', $option_name, implode(', ', $possible_values)));
}
}
$mode_validation = $options_validation['mode'][$this->configuration['mode']];
foreach ($mode_validation as $option_name => $possible_values) {
if (empty($this->configuration[$option_name])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option '{$option_name}' is required for mode '{$mode}'.");
}
if (!is_null($possible_values) && !in_array($this->configuration[$option_name], $possible_values)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), sprintf('Configuration option "%s" only accepts the following values: %s.', $option_name, implode(', ', $possible_values)));
}
}
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$this
->init($value, $destination_property);
foreach ($this->xpath
->query($this->configuration['xpath']) as $html_node) {
$subject = $this
->getSubject($html_node);
if (empty($subject)) {
continue;
}
$search = $this
->getSearch();
$replace = $this
->getReplace();
$this
->doReplace($html_node, $search, $replace, $subject);
}
return $this->document;
}
protected function getSubject(\DOMElement $node) {
switch ($this->configuration['mode']) {
case 'attribute':
return $node
->getAttribute($this->configuration['attribute_options']['name']);
case 'element':
return $node->nodeName;
}
}
protected function getSearch() {
switch ($this->configuration['mode']) {
case 'attribute':
case 'element':
return $this->configuration['search'];
}
}
protected function getReplace() {
switch ($this->configuration['mode']) {
case 'attribute':
case 'element':
return $this->configuration['replace'];
}
}
protected function doReplace(\DOMElement $html_node, $search, $replace, $subject) {
if ($this->configuration['regex']) {
$function = 'preg_replace';
}
elseif ($this->configuration['case_insensitive']) {
$function = 'str_ireplace';
}
else {
$function = 'str_replace';
}
$new_subject = $function($search, $replace, $subject);
$this
->postReplace($html_node, $new_subject);
}
protected function postReplace(\DOMElement $html_node, $new_subject) {
switch ($this->configuration['mode']) {
case 'attribute':
$html_node
->setAttribute($this->configuration['attribute_options']['name'], $new_subject);
break;
case 'element':
$new_node = $this->document
->createElement($new_subject);
foreach ($html_node->childNodes as $child) {
$new_node
->appendChild($child
->cloneNode(TRUE));
}
foreach ($html_node->attributes as $attribute) {
$new_node
->setAttribute($attribute->name, $attribute->value);
}
$html_node->parentNode
->replaceChild($new_node, $html_node);
break;
}
}
}