Completed
Push — master ( dd4e5e...557dd8 )
by Thomas
03:57
created

SecurityRequirements   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 4
dl 0
loc 101
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A parse() 0 11 3
A toArray() 0 10 3
A size() 0 3 1
A has() 0 3 1
A get() 0 6 2
A add() 0 4 1
A remove() 0 4 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\AbstractModel;
5
use phootwork\collection\CollectionUtils;
6
use phootwork\collection\Map;
7
use phootwork\collection\Set;
8
use phootwork\lang\Arrayable;
9
10
class SecurityRequirements extends AbstractModel implements Arrayable, \Iterator {
11
12
	/** @var Map */
13
	private $securities;
14
15
	public function __construct($contents = []) {
16
		$this->parse($contents === null ? [] : $contents);
17
	}
18
19
	private function parse($contents) {
20
		$data = CollectionUtils::toList($contents);
21
22
		// schemes
23
		$this->securities = new Map();
24
		foreach ($data as $security) {
25
			foreach ($security as $id => $scopes) {
26
				$this->securities->set($id, new Set($scopes));
27
			}
28
		}
29
	}
30
31
	public function toArray() {
32
		$out = [];
33
		foreach ($this->securities as $id => $scopes) {
34
			$out[$id] = $scopes->toArray();
35
		}
36
		if (count($out)) {
37
			$out = [$out];
38
		}
39
		return $out;
40
	}
41
42
	public function size() {
43
		return $this->securities->size();
44
	}
45
46
	/**
47
	 * Returns whether a security with the given id exists
48
	 *
49
	 * @param string $id
50
	 * @return bool
51
	 */
52
	public function has($id) {
53
		return $this->securities->has($id);
54
	}
55
56
	/**
57
	 * Returns the scopes for the given id
58
	 *
59
	 * @param string $id
60
	 * @return Set
61
	 */
62
	public function get($id) {
63
		if (!$this->securities->has($id)) {
64
			$this->securities->set($id, new Set());
65
		}
66
		return $this->securities->get($id);
67
	}
68
69
	/**
70
	 * Sets the scheme
71
	 *
72
	 * @param string $id
73
	 * @param Set $scopes
74
	 * @return $this
75
	 */
76
	public function add($id, Set $scopes) {
77
		$this->securities->set($id, $scopes);
78
		return $this;
79
	}
80
81
	/**
82
	 * Removes the given security
83
	 *
84
	 * @param string $id
85
	 */
86
	public function remove($id) {
87
		$this->securities->remove($id);
88
		return $this;
89
	}
90
91
	public function current() {
92
		return $this->securities->current();
93
	}
94
95
	public function key() {
96
		return $this->securities->key();
97
	}
98
99
	public function next() {
100
		return $this->securities->next();
101
	}
102
103
	public function rewind() {
104
		return $this->securities->rewind();
105
	}
106
107
	public function valid() {
108
		return $this->securities->valid();
109
	}
110
}
111