Completed
Push — master ( 8619b3...819e89 )
by Thomas
05:42
created

Parameters::searchByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 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 {
11
	
12
	use RefPart;
13
	
14
	/** @var ArrayList */
15
	private $parameters;
16
	
17 11
	public function __construct($contents = null) {
18 11
		$this->parse($contents === null ? new ArrayList() : $contents);
19 11
	}
20
	
21 11
	private function parse($contents) {
22 11
		$data = CollectionUtils::toMap($contents);
23
		
24 11
		$this->parameters = new ArrayList();
25 11
		$this->parseRef($data);
26
		
27 11
		if (!$this->hasRef()) {
28 11
			foreach ($data as $param) {
29 3
				$this->parameters->add(new Parameter($param));
30 11
			}
31 11
		}
32 11
	}
33
	
34 7
	public function toArray() {
35 7
		if ($this->hasRef()) {
36 1
			return ['$ref' => $this->getRef()];
37
		}
38
		
39 6
		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
63
	 */
64 11
	public function findByName($name) {
65 1
		foreach ($this->parameters as $param) {
66 1
			if ($param->getName() == $name) {
67 1
				return $param;
68
			}
69 11
		}
70 1
	}
71
	
72
	/**
73
	 * Adds a parameter
74
	 * 
75
	 * @param Parameter $param
76
	 */
77 1
	public function add(Parameter $param) {
78 1
		$this->parameters->add($param);
79 1
	}
80
	
81
	/**
82
	 * Removes a parameter
83
	 * 
84
	 * @param Parameter $param
85
	 */
86 1
	public function remove(Parameter $param) {
87 1
		$this->parameters->remove($param);
88 1
	}
89
	
90
	/**
91
	 * Returns whether a given parameter exists
92
	 * 
93
	 * @param Parameter $param
94
	 * @return boolean
95
	 */
96 1
	public function contains(Parameter $param) {
97 1
		return $this->parameters->contains($param);
98
	}
99
}
100