XMLContent   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 22
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolveValues() 0 15 4
1
<?php
2
3
namespace Drupal\graphql_xml\Plugin\GraphQL\Fields;
4
5
use Drupal\graphql\GraphQL\Execution\ResolveContext;
6
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
7
use GraphQL\Type\Definition\ResolveInfo;
8
9
/**
10
 * Get an xml elements inner content string.
11
 *
12
 * @GraphQLField(
13
 *   id = "xml_content",
14
 *   secure = true,
15
 *   type = "String",
16
 *   name = "content",
17
 *   parents = { "XMLElement" }
18
 * )
19
 */
20
class XMLContent extends FieldPluginBase {
21
22
  /**
23
   * {@inheritdoc}
24
   */
25
  protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
26
    if ($value instanceof \DOMElement) {
27
      yield implode('', array_map(function ($child) {
28
        if ($child instanceof \DOMText) {
29
          return $child->nodeValue;
30
        }
31
        elseif ($child instanceof \DOMElement) {
32
          return $child->ownerDocument->saveXML($child);
33
        }
34
        else {
35
          return '';
36
        }
37
      }, iterator_to_array($value->childNodes)));
38
    }
39
  }
40
41
}
42