Completed
Pull Request — master (#157)
by
unknown
01:35
created

DriverFieldPluginBase   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 114
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 12 1
A isFinal() 0 4 1
A processValues() 0 12 3
A assignPropertyNames() 0 13 4
A getMainPropertyName() 0 7 2
A processValue() 0 4 1
1
<?php
2
3
namespace Drupal\Driver\Plugin;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
9
/**
10
 * Base class for Driver field plugins.
11
 */
12
class DriverFieldPluginBase extends PluginBase implements DriverFieldPluginInterface, ContainerFactoryPluginInterface
13
{
14
15
  /**
16
   * The field object this plugin is processing values for.
17
   *
18
   * @var \Drupal\Driver\Wrapper\Field\DriverFieldInterface
19
   */
20
    protected $field;
21
22
  /**
23
   * {@inheritdoc}
24
   */
25
    public function __construct(
26
        array $configuration,
27
        $plugin_id,
28
        $plugin_definition
29
    ) {
30
        parent::__construct($configuration, $plugin_id, $plugin_definition);
31
32
        $this->field = $configuration['field'];
33
    }
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
    public static function create(
39
        ContainerInterface $container,
40
        array $configuration,
41
        $plugin_id,
42
        $plugin_definition
43
    ) {
44
        return new static(
45
        $configuration,
46
        $plugin_id,
47
        $plugin_definition
48
        );
49
    }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54
    public function isFinal($field)
55
    {
56
        return $this->pluginDefinition['final'];
57
    }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
    public function processValues($values)
63
    {
64
        if (!is_array($values)) {
65
            throw new \Exception("Values must be an array");
66
        }
67
        $processed = [];
68
        foreach ($values as $value) {
69
            $value = $this->assignPropertyNames($value);
70
            $processed[] = $this->processValue($value);
71
        }
72
        return $processed;
73
    }
74
75
  /**
76
   * Converts a single instruction into an array of field properties for
77
   * content fields. We want to allow plugins to be called with field properties
78
   * already explicitly specified, but also need to allow for more cryptic
79
   * inputs that the plugin has to decipher.
80
   *
81
   * @return string|array
82
   *   returns the array of field properties for one field value.
83
   */
84
    protected function assignPropertyNames($value)
85
    {
86
        // Keep config properties simple.
87
        if ($this->field->isConfigProperty()) {
88
        } // Convert simple string
89
        elseif (!is_array($value)) {
90
            $value = [$this->getMainPropertyName() => $value];
91
        } // Convert single item unkeyed array.
92
        elseif (array_keys($value) === [0]) {
93
            $value = [$this->getMainPropertyName() => $value[0]];
94
        }
95
        return $value;
96
    }
97
98
  /**
99
   * Gets the default column name to use for field values.
100
   *
101
   * @return string
102
   *   The default column name for this field type.
103
   */
104
    protected function getMainPropertyName()
105
    {
106
        if ($this->field->isConfigProperty()) {
107
            throw new \Exception("Main property name not used when processing config properties.");
108
        }
109
        return $this->pluginDefinition['mainPropertyName'];
110
    }
111
112
  /**
113
   * Processes the properties for a single field value.
114
   *
115
   * @param array $value
116
   *   An array of field properties.
117
   *
118
   * @return array
119
   *   returns the array of column values for one field value.
120
   */
121
    protected function processValue($value)
122
    {
123
        return $value;
124
    }
125
}
126