PathEval   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 16
dl 0
loc 65
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirstTokenValue() 0 2 1
A getFirstToken() 0 2 1
A gettok() 0 2 1
A __construct() 0 2 1
A subtok() 0 2 1
A pathStartsWithConfig() 0 2 1
A get() 0 2 1
A evalSubPath() 0 5 1
A find() 0 5 2
1
<?php
2
3
namespace Logikos\Util\Config;
4
5
use Logikos\Util\Config;
6
7
class PathEval {
8
  /**
9
   * @var Config
10
   */
11
  private $config;
12
13 5
  public function __construct(Config $config) {
14 5
    $this->config = $config;
15 5
  }
16
17 5
  public function find($path, $default=null, $delimiter='.') {
18 5
    if ($this->pathStartsWithConfig($path, $delimiter))
19 3
      return $this->evalSubPath($path, $delimiter, $default);
20
21 5
    return $this->get($this->getFirstToken($path, $delimiter), $default);
22
  }
23
24 5
  private function get($name, $default=null) {
25 5
    return $this->config->get($name, $default);
26
  }
27
28 5
  private function pathStartsWithConfig($path, $delimiter) {
29 5
    return $this->getFirstTokenValue($path, $delimiter) instanceof Config;
30
  }
31
32 5
  private function getFirstTokenValue($path, $delimiter) {
33 5
    return $this->get($this->getFirstToken($path, $delimiter));
34
  }
35
36 5
  private function getFirstToken($path, $delimiter) {
37 5
    return $this->gettok($path, $delimiter,0);
38
  }
39
40 3
  private function evalSubPath($path, $delimiter, $default) {
41 3
    return $this->getFirstTokenValue($path, $delimiter)->path(
42 3
        $this->subtok($path, $delimiter, 1),
43 3
        $default,
44 3
        $delimiter
45
    );
46
  }
47
48
  /**
49
   * subtok(string, delimiter, offset, length)
50
   *
51
   * Usage:
52
   *  subtok('a.b.c.d.e','.',0)     = 'a.b.c.d.e'
53
   *  subtok('a.b.c.d.e','.',0,2)   = 'a.b'
54
   *  subtok('a.b.c.d.e','.',2,1)   = 'c'
55
   *  subtok('a.b.c.d.e','.',2,-1)  = 'c.d'
56
   *  subtok('a.b.c.d.e','.',-4)    = 'b.c.d.e'
57
   *  subtok('a.b.c.d.e','.',-4,2)  = 'b.c'
58
   *  subtok('a.b.c.d.e','.',-4,-1) = 'b.c.d'
59
   *
60
   * @param  string   $string    The input string
61
   * @param  string   $delimiter The boundary string
62
   * @param  int      $offset    starting position, like in substr
63
   * @param  int|null $length    length, like in substr
64
   * @return string
65
   */
66 3
  private function subtok($string, $delimiter, $offset, $length = NULL) {
67 3
    return implode($delimiter, array_slice(explode($delimiter, $string), $offset, $length));
68
  }
69
70 5
  private function gettok($string, $delimiter, $offset) {
71 5
    return explode($delimiter, $string)[$offset];
72
  }
73
}