Path::getSegments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Purl package, a project by Jonathan H. Wage.
5
 *
6
 * (c) 2013 Jonathan H. Wage
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Purl;
13
14
/**
15
 * Path represents the part of a Url after the domain suffix and before the hash-mark (#).
16
 *
17
 * @author      Jonathan H. Wage <[email protected]>
18
 */
19
class Path extends AbstractPart
20
{
21
  /**
22
   * @var string The original path string.
23
   */
24
  private $path;
25
26
  /**
27
   * Construct a new Path instance.
28
   *
29
   * @param string $path
30
   */
31 69
  public function __construct($path = null)
32
  {
33 69
    $this->path = $path;
34 69
  }
35
36
  /**
37
   * Builds a string path from this Path instance internal data and returns it.
38
   *
39
   * @return string
40
   */
41 29
  public function getPath(): string
42
  {
43 29
    $this->initialize();
44
45 29
    return \implode(
46 29
        '/',
47 29
        \array_map(
48 29
            function ($value) {
49 29
              return \str_replace(' ', '%20', $value);
50 29
            }, $this->data
51
        )
52
    );
53
  }
54
55
  /**
56
   * Set the string path for this Path instance and sets initialized to false.
57
   *
58
   * @param string
59
   */
60 2
  public function setPath($path)
61
  {
62 2
    $this->initialized = false;
63 2
    $this->path = $path;
64 2
  }
65
66
  /**
67
   * Get the array of segments that make up the path.
68
   *
69
   * @return array
70
   */
71 1
  public function getSegments(): array
72
  {
73 1
    $this->initialize();
74
75 1
    return $this->data;
76
  }
77
78
  /**
79
   * @inheritDoc
80
   */
81 27
  public function __toString()
82
  {
83 27
    return $this->getPath();
84
  }
85
86
  /**
87
   * @inheritDoc
88
   */
89 30
  protected function doInitialize()
90
  {
91 30
    $this->data = \explode('/', $this->path);
92 30
  }
93
}
94