Paths::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
namespace gossi\swagger\collections;
3
4
use gossi\swagger\parts\ExtensionPart;
5
use gossi\swagger\Path;
6
use phootwork\collection\CollectionUtils;
7
use phootwork\collection\Map;
8
use phootwork\lang\Arrayable;
9
use phootwork\lang\Text;
10
use gossi\swagger\AbstractModel;
11
12
class Paths extends AbstractModel implements Arrayable, \Iterator {
13
14
	use ExtensionPart;
15
16
	/** @var Map */
17
	private $paths;
18
19 11
	public function __construct($contents = []) {
20 11
		$this->parse($contents === null ? [] : $contents);
21 11
	}
22
23 11
	private function parse($contents) {
24 11
		$data = CollectionUtils::toMap($contents);
25
26
		// paths
27 11
		$this->paths = new Map();
28 11
		foreach ($data as $p => $path) {
29 7
			if (!Text::create($p)->startsWith('x-')) {
30 7
				$this->paths->set($p, new Path($p, $path));
31 7
			}
32 11
		}
33
34
		// extensions
35 11
		$this->parseExtensions($data);
36 11
	}
37
38 9
	public function toArray() {
39
		return CollectionUtils::toArrayRecursive($this->paths);
40
	}
41
42 9
	public function size() {
43
		return $this->paths->size();
44
	}
45 1
46 1
	/**
47
	 * Returns whether a path with the given name exists
48
	 *
49
	 * @param string $path
50
	 * @return bool
51
	 */
52
	public function has($path) {
53
		return $this->paths->has($path);
54
	}
55 1
56 1
	/**
57
	 * Returns whether the given path exists
58
	 *
59
	 * @param Path $path
60
	 * @return bool
61
	 */
62
	public function contains(Path $path) {
63
		return $this->paths->contains($path);
64
	}
65 1
66 1
	/**
67
	 * Returns the path info for the given path
68
	 *
69
	 * @param string $path
70
	 * @return Path
71
	 */
72
	public function get($path) {
73
		if (!$this->paths->has($path)) {
74
			$this->paths->set($path, new Path($path));
75 2
		}
76 2
		return $this->paths->get($path);
77
	}
78
79 2
	/**
80
	 * Sets the path
81
	 *
82
	 * @param Path $path
83
	 * @return $this
84
	 */
85
	public function add(Path $path) {
86
		$this->paths->set($path->getPath(), $path);
87
		return $this;
88 1
	}
89 1
90 1
	/**
91
	 * Adds all operations from another paths collection. Will overwrite existing operations.
92
	 *
93
	 * @param Paths $paths
94
	 */
95
	public function addAll(Paths $paths) {
96
		foreach ($paths as $p) {
97
			$path = $this->get($p->getPath());
98
			foreach ($p->getMethods() as $method) {
99
				$path->setOperation($method, $p->getOperation($method));
100
			}
101
		}
102
	}
103
104
	/**
105
	 * Removes the given path
106
	 *
107
	 * @param string $path
108
	 */
109
	public function remove($path) {
110
		$this->paths->remove($path);
111
		return $this;
112 1
	}
113 1
114 1
	public function current() {
115
		return $this->paths->current();
116
	}
117
118
	public function key() {
119
		return $this->paths->key();
120
	}
121
122
	public function next() {
123
		return $this->paths->next();
124
	}
125
126
	public function rewind() {
127
		return $this->paths->rewind();
128
	}
129
130
	public function valid() {
131
		return $this->paths->valid();
132
	}
133
}
134