Completed
Pull Request — 8.x-1.x (#20)
by
unknown
01:16
created

TokenHandler::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Shamelessly stolen from: https://git.drupalcode.org/project/eva/blob/8.x-2.x/src/TokenHandler.php
4
 */
5
namespace Drupal\graphql_views;
6
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Drupal\Core\Utility\Token;
9
10
/**
11
 * Token handling service.
12
 */
13
class TokenHandler {
14
15
  /**
16
   * The token service.
17
   *
18
   * @var \Drupal\Core\Utility\Token
19
   */
20
  protected $token;
21
22
  /**
23
   * Inject token dependencies.
24
   *
25
   * @param \Drupal\Core\Utility\Token $token
26
   *   The token service.
27
   */
28
  public function __construct(Token $token) {
29
    $this->token = $token;
30
  }
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  public static function create(ContainerInterface $container) {
36
    return new static(
37
      $container->get('token')
38
    );
39
  }
40
41
  /**
42
   * Get view arguments array from string that contains tokens.
43
   *
44
   * @param string $string
45
   *   The token string defined by the view.
46
   * @param string $type
47
   *   The token type.
48
   * @param object $object
49
   *   The object being used for replacement data (typically a node).
50
   *
51
   * @return array
52
   *   An array of argument values.
53
   */
54
  public function getArgumentsFromTokenString($string, $type, $object) {
55
    $args = trim($string);
56
    if (empty($args)) {
57
      return [];
58
    }
59
    $args = $this->token->replace($args, [$type => $object], ['sanitize' => FALSE, 'clear' => FALSE]);
60
    return explode('/', $args);
61
  }
62
63
}
64