Completed
Pull Request — master (#1)
by Guilh
07:45
created

Parameters   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 52.17%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 28
c 7
b 0
f 1
lcom 1
cbo 4
dl 0
loc 148
ccs 36
cts 69
cp 0.5217
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A size() 0 3 1
A __construct() 0 3 2
A parse() 0 12 3
A toArray() 0 7 2
A searchByName() 0 5 1
A findByName() 0 7 3
A getByName() 0 10 2
A find() 0 7 4
A get() 0 11 2
A add() 0 3 1
A remove() 0 3 1
A contains() 0 3 1
A current() 0 3 1
A key() 0 3 1
A next() 0 3 1
A rewind() 0 3 1
A valid() 0 3 1
1
<?php
2
namespace gossi\swagger\collections;
3
4
use gossi\swagger\parts\RefPart;
5
use phootwork\collection\ArrayList;
6
use phootwork\collection\CollectionUtils;
7
use phootwork\lang\Arrayable;
8
use gossi\swagger\Parameter;
9
10
class Parameters implements Arrayable, \Iterator {
11
	
12
	use RefPart;
13
	
14
	/** @var ArrayList */
15
	private $parameters;
16
	
17 12
	public function __construct($contents = []) {
18 12
		$this->parse($contents === null ? [] : $contents);
19 12
	}
20
	
21 12
	private function parse($contents) {
22 12
		$data = CollectionUtils::toMap($contents);
23
		
24 12
		$this->parameters = new ArrayList();
25 12
		$this->parseRef($data);
26
		
27 12
		if (!$this->hasRef()) {
28 12
			foreach ($data as $param) {
29 4
				$this->parameters->add(new Parameter($param));
30 12
			}
31 12
		}
32 12
	}
33
	
34 8
	public function toArray() {
35 8
		if ($this->hasRef()) {
36 1
			return ['$ref' => $this->getRef()];
37
		}
38
		
39 7
		return $this->parameters->toArray();
40
	}
41
	
42 1
	public function size() {
43 1
		return $this->parameters->size();
44
	}
45
46
	/**
47
	 * Searches whether a parameter with the given name exists
48
	 * 
49
	 * @param string $name
50
	 * @return boolean
51
	 */
52
	public function searchByName($name) {
53 1
		return $this->parameters->search($name, function(Parameter $param, $name) {
54 1
			return $param->getName() == $name;
55 1
		});
56
	}
57
	
58
	/**
59
	 * Returns parameter with the given name if it exists
60
	 * 
61
	 * @param string $name
62
	 * @return Parameter|void
63
	 */
64 12
	public function findByName($name) {
65 1
		foreach ($this->parameters as $param) {
66 1
			if ($param->getName() == $name) {
67 1
				return $param;
68
			}
69 12
		}
70 1
	}
71
	
72
	/**
73
	 * Searches for the parameter with the given name. Creates a parameter with the given name
74
	 * if none exists
75
	 * 
76
	 * @param string $name
77
	 * @return Parameter
78
	 */
79
	public function getByName($name) {
80
		$param = $this->findByName($name);
81
		if (empty($param)) {
82
			$param = new Parameter();
83
			$param->setName($name);
84
			$this->parameters->add($param);
85
		}
86
		
87
		return $param;
88
	}
89
	
90
	public function find($in, $name) {
91
		foreach ($this->parameters as $param) {
92
			if ($param->getIn() == $in && $param->getName() == $name) {
93
				return $param;
94
			}
95
		}
96
	}
97
	
98
	public function get($in, $name) {
99
		$param = $this->find($in, $name);
100
		if (empty($param)) {
101
			$param = new Parameter();
102
			$param->setIn($name);
103
			$param->setName($name);
104
			$this->parameters->add($param);
105
		}
106
		
107
		return $param;
108
	}
109
	
110
	/**
111
	 * Adds a parameter
112
	 * 
113
	 * @param Parameter $param
114
	 */
115 1
	public function add(Parameter $param) {
116 1
		$this->parameters->add($param);
117 1
	}
118
	
119
	/**
120
	 * Removes a parameter
121
	 * 
122
	 * @param Parameter $param
123
	 */
124 1
	public function remove(Parameter $param) {
125 1
		$this->parameters->remove($param);
126 1
	}
127
	
128
	/**
129
	 * Returns whether a given parameter exists
130
	 * 
131
	 * @param Parameter $param
132
	 * @return boolean
133
	 */
134 1
	public function contains(Parameter $param) {
135 1
		return $this->parameters->contains($param);
136
	}
137
	
138
	public function current() {
139
		return $this->parameters->current();
140
	}
141
	
142
	public function key() {
143
		return $this->parameters->key();
144
	}
145
	
146
	public function next() {
147
		return $this->parameters->next();
148
	}
149
	
150
	public function rewind() {
151
		return $this->parameters->rewind();
152
	}
153
	
154
	public function valid() {
155
		return $this->parameters->valid();
156
	}
157
}
158