View source
<?php
namespace Drupal\pardot;
class FormMap implements FormMapInterface {
protected $post_url;
public $settings = [];
public $mapping = [];
public function __construct($settings) {
$mapping = [];
$this->settings = $settings;
$form_map_plugin_manager = \Drupal::service('plugin.manager.pardot_form_map_formatter_plugin');
if (isset($this->settings['form_map'])) {
foreach ($this->settings['form_map'] as $key => $setting) {
if ($setting['plugin_type']) {
$plugin = $form_map_plugin_manager
->createInstance($setting['plugin']['id']);
$plugin
->setConfiguration($setting['plugin']);
}
else {
$plugin = [];
}
$class = $setting['class'];
$mapping[$key] = new $class($setting['pardot_key'], $setting['plugin_type'], $plugin);
}
}
$post_url = $this->settings['post_url'] ?? '';
$this
->setPostUrl($post_url);
$this
->setMappedFieldCollection($mapping);
}
public function getPostUrl() {
return $this->post_url;
}
public function setPostUrl(string $post_url) {
$this->post_url = $post_url;
}
public function getMappedFieldCollection() {
return $this->mapping;
}
public function setMappedFieldCollection(array $mapping) {
$this->mapping = $mapping;
}
public function appendMappedField(string $class = '\\Drupal\\pardot\\MappedField', string $pardot_key = '', string $plugin_type = '', $plugin = [], array $config = []) {
$this->mapping[] = new $class($pardot_key, $plugin_type, $plugin, $config);
}
public function getMappedField(int $key) {
if (isset($this->mapping[$key])) {
return $this->mapping[$key];
}
return NULL;
}
public function removeMappedField(int $key) {
if (isset($this->mapping[$key])) {
unset($this->mapping[$key]);
}
}
public function toArray() {
$mapping = $this->mapping;
$mapping = array_map(function (MappedFieldInterface $mapping) {
return $mapping
->toArray();
}, $mapping);
$this->settings['post_url'] = $this
->getPostUrl();
$this->settings['form_map'] = $mapping;
return $this->settings;
}
}