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

DriverNameMatcher::identifyByLabelExactly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace Drupal\Driver\Plugin;
4
5
/**
6
 * Matching text input with entities using their machine names or labels.
7
 */
8
class DriverNameMatcher {
9
10
  /**
11
   * A set of items needing to be identified.
12
   *
13
   * The key is some human-friendly name, the value is preserved and is not
14
   * used for identification.
15
   *
16
   * @var array
17
   */
18
  protected $targets;
19
20
  /**
21
   * A set of items to match.
22
   *
23
   * The array keys are the items machine names and the values are the items
24
   * labels.
25
   *
26
   * @var array
27
   */
28
  protected $candidates;
29
30
  /**
31
   * A string that may precede the candidate's machine names.
32
   *
33
   * It should be ignored for identification purposes.
34
   *
35
   * @var string
36
   */
37
  protected $prefix;
38
39
  /**
40
   * A set of successfully matched items.
41
   *
42
   * @var array
43
   */
44
  protected $results;
45
46
  /**
47
   * Construct a driver name matcher object.
48
   *
49
   * @param mixed $candidates
50
   *   A set of candidate items where the keys are the items labels
51
   *   and the values are the items machine names.
52
   * @param string $prefix
53
   *   A string that may precede the candidate's machine names and should be
54
   *   ignored for identification purposes.
55
   */
56
  public function __construct($candidates, $prefix = NULL) {
57
    if (is_array($candidates)) {
58
      $this->candidates = $candidates;
59
    }
60
    else {
61
      throw new \Exception("Candidates for identification must be passed as an array with the machine names as the keys and the labels as the values.");
62
    }
63
64
    $this->prefix = $prefix;
65
    $this->results = [];
66
  }
67
68
  /**
69
   * Identifies a target from the pool of candidates.
70
   *
71
   * @param string $target
72
   *   A single string needing to be identified as an item in the candidates.
73
   *
74
   * @return string
75
   *   The machine name of the matching candidate, or NULL if none matched.
76
   */
77
  public function identify($target) {
78
    // Wrap the target in the appropriate array for identifySet().
79
    $targets = [$target => $target];
80
    $results = $this->identifySet($targets);
81
    // Get the first key of the results.
82
    reset($results);
83
    return key($results);
84
  }
85
86
  /**
87
   * Identifies the targets from the pool of candidates.
88
   *
89
   * @param mixed $targets
90
   *   A set of items needing to be identified. The key is some human-friendly
91
   *   name, the value is preserved and is not used for identification.
92
   *
93
   * @return array
94
   *   For each matched target, the key will be replaced with the machine name
95
   *   of the matching candidate, & the value will be preserved. Order may vary.
96
   */
97
  public function identifySet($targets) {
98
    if (is_array($targets)) {
99
      $this->targets = $targets;
100
    }
101
    else {
102
      throw new \Exception("Targets to be identified must be passed as an array with their human-friendly name as the keys and anything as the values.");
103
    }
104
105
    $mayHavePrefix = !is_null($this->prefix);
106
    $this->identifyByMethod("MachineNameExactly");
107
    $this->identifyByMethod("LabelExactly");
108
    if ($mayHavePrefix) {
109
      $this->identifyByMethod("MachineNameWithoutPrefix");
110
    }
111
    $this->identifyByMethod("MachineNameWithoutUnderscores");
112
    if ($mayHavePrefix) {
113
      $this->identifyByMethod("MachineNameWithoutPrefixAndUnderscores");
114
    }
115
    return $this->results;
116
  }
117
118
  /**
119
   * Gets the candidates that were not a match for any target.
120
   *
121
   * @return array
122
   *   An array of candidates.
123
   */
124
  public function getUnmatchedCandidates() {
125
    return $this->candidates;
126
  }
127
128
  /**
129
   * Gets the targets that were not a match for any candidate.
130
   *
131
   * @return array
132
   *   An array of targets.
133
   */
134
  public function getUnmatchedTargets() {
135
    return $this->targets;
136
  }
137
138
  /**
139
   * Iterates over candidates and targets looking for a match.
140
   *
141
   * @param string $method
142
   *   The last part of the name of a method of matching.
143
   */
144
  protected function identifyByMethod($method) {
145
    $methodFunctionName = "identifyBy" . $method;
146
    $matchedCandidates = [];
147
    foreach ($this->targets as $identifier => $value) {
148
      foreach ($this->candidates as $label => $machineName) {
149
        // Skip over candidates that describe fields already matched.
150
        if (in_array($machineName, $matchedCandidates)) {
151
          continue;
152
        }
153
        // If the identification method determines a match, remove the candidate
154
        // and target from future consideration, and save the result.
155
        if ($this->$methodFunctionName($identifier, $machineName, $label)) {
156
          // $this->candidates = array_filter(
157
          // $this->candidates, function ($value, $key) use ($machineName) {
158
          // return $value === $machineName;
159
          // }, ARRAY_FILTER_USE_BOTH);.
160
          $matchedCandidates[] = $machineName;
161
          // unset($this->candidates[$label]);.
162
          unset($this->targets[$identifier]);
163
          $this->results[$machineName] = $value;
164
          break;
165
        }
166
      }
167
    }
168
169
    // Strip out the successfully matched candidates.
170
    $this->candidates = array_filter($this->candidates, function ($machineName, $label) use ($matchedCandidates) {
0 ignored issues
show
Unused Code introduced by
The parameter $label is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
171
      return !in_array($machineName, $matchedCandidates);
172
    }, ARRAY_FILTER_USE_BOTH);
173
  }
174
175
  /**
176
   * Matches an identifer against a machine name exactly.
177
   *
178
   * @param string $identifier
179
   *   The human-friendly name of the target.
180
   * @param string $machineName
181
   *   The machine name of the candidate.
182
   * @param string $label
183
   *   The label of the candidate.
184
   *
185
   * @return bool
186
   *   Whether a match was found using the identifier.
187
   */
188
  protected function identifyByMachineNameExactly($identifier, $machineName, $label) {
0 ignored issues
show
Unused Code introduced by
The parameter $label is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
    return (mb_strtolower($identifier) === mb_strtolower($machineName));
190
  }
191
192
  /**
193
   * Matches an identifer against a label exactly.
194
   *
195
   * @param string $identifier
196
   *   The human-friendly name of the target.
197
   * @param string $machineName
198
   *   The machine name of the candidate.
199
   * @param string $label
200
   *   The label of the candidate.
201
   *
202
   * @return bool
203
   *   Whether a match was found using the identifier.
204
   */
205
  protected function identifyByLabelExactly($identifier, $machineName, $label) {
0 ignored issues
show
Unused Code introduced by
The parameter $machineName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
206
    return (mb_strtolower($identifier) === mb_strtolower($label));
207
  }
208
209
  /**
210
   * Matches an identifer against a machine name removing the prefix.
211
   *
212
   * @param string $identifier
213
   *   The human-friendly name of the target.
214
   * @param string $machineName
215
   *   The machine name of the candidate.
216
   * @param string $label
217
   *   The label of the candidate.
218
   *
219
   * @return bool
220
   *   Whether a match was found using the identifier.
221
   */
222
  protected function identifyByMachineNameWithoutPrefix($identifier, $machineName, $label) {
0 ignored issues
show
Unused Code introduced by
The parameter $label is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
223
    if (substr($machineName, 0, 6) === $this->prefix) {
224
      $machineName = substr($machineName, 6);
225
    }
226
    return (mb_strtolower($identifier) === mb_strtolower($machineName));
227
  }
228
229
  /**
230
   * Matches an identifer against a machine name removing underscores from it.
231
   *
232
   * @param string $identifier
233
   *   The human-friendly name of the target.
234
   * @param string $machineName
235
   *   The machine name of the candidate.
236
   * @param string $label
237
   *   The label of the candidate.
238
   *
239
   * @return bool
240
   *   Whether a match was found using the identifier.
241
   */
242
  protected function identifyByMachineNameWithoutUnderscores($identifier, $machineName, $label) {
0 ignored issues
show
Unused Code introduced by
The parameter $label is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
243
    $machineName = str_replace('_', ' ', $machineName);
244
    return (mb_strtolower($identifier) === mb_strtolower($machineName));
245
  }
246
247
  /**
248
   * Matches an identifer against a machine name, removing prefix & underscores.
249
   *
250
   * @param string $identifier
251
   *   The human-friendly name of the target.
252
   * @param string $machineName
253
   *   The machine name of the candidate.
254
   * @param string $label
255
   *   The label of the candidate.
256
   *
257
   * @return bool
258
   *   Whether a match was found using the identifier.
259
   */
260
  protected function identifyByMachineNameWithoutPrefixAndUnderscores($identifier, $machineName, $label) {
0 ignored issues
show
Unused Code introduced by
The parameter $label is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
261
    if (substr($machineName, 0, 6) === "field_") {
262
      $machineName = substr($machineName, 6);
263
    }
264
    $machineName = str_replace('_', ' ', $machineName);
265
    return (mb_strtolower($identifier) === mb_strtolower($machineName));
266
  }
267
268
}
269