Passed
Branch master (989b89)
by Wanderson
01:17
created

Data   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 11 4
A all() 0 2 1
A clear() 0 2 1
A set() 0 8 3
A delete() 0 2 1
1
<?php
2
3
namespace Win\Data;
4
5
use Win\DesignPattern\SingletonTrait;
6
7
/**
8
 * Dados
9
 */
10
class Data implements DataInterface {
11
12
	use SingletonTrait;
13
14
	/** @var mixed[] */
15
	protected $data = [];
16
17
	/** @return mixed[] */
18
	public function all() {
19
		return $this->data;
20
	}
21
22
	/** Exclui todos os dados */
23
	public function clear() {
24
		$this->data = [];
25
	}
26
27
	/**
28
	 * Define um valor
29
	 * @param string $key
30
	 * @param mixed $value
31
	 */
32
	public function set($key, $value) {
33
		$p = &$this->data;
34
		$keys = explode('.', $key);
35
		foreach ($keys as $k) {
36
			$p = &$p[$k];
37
		}
38
		if ($value) {
39
			$p = $value;
40
		}
41
	}
42
43
	/**
44
	 * Retorna um valor
45
	 * @param string $key
46
	 * @param string $default Valor default, caso a $key não exista
47
	 */
48
	public function get($key, $default = '') {
49
		$data = $this->data;
50
		$keys = explode('.', $key);
51
		foreach ($keys as $k) {
52
			if (is_array($data) && array_key_exists($k, $data)) {
53
				$data = $data[$k];
54
			} else {
55
				return $default;
56
			}
57
		}
58
		return $data;
59
	}
60
61
	/** @param string $key */
62
	public function delete($key) {
63
		unset($this->data[$key]);
64
	}
65
66
}
67