Passed
Push — master ( 6a78e3...31c3ef )
by Jean-Christophe
19:36
created

UConfigFile   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
eloc 16
c 1
b 0
f 1
dl 0
loc 53
ccs 0
cts 22
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 2 1
A getData() 0 2 1
A set() 0 3 1
A save_() 0 2 1
A load() 0 2 1
A save() 0 2 1
A setData() 0 3 1
A __construct() 0 2 1
A load_() 0 5 2
1
<?php
2
3
namespace Ubiquity\utils\base;
4
/**
5
 * 
6
 * Ubiquity\utils\base$UConfigFile
7
 * This class is part of Ubiquity
8
 * @author jc
9
 * @version 1.0.0
10
 * @since 2.4.11
11
 *
12
 */
13
class UConfigFile {
14
	
15
	private string $filename;
16
	
17
	private array $data=[];
18
	
19
	public function __construct(string $name){
20
		$this->filename=\ROOT . "config/$name.config.php";
21
	}
22
	
23
	public function load(array $default=[]): array {
24
		return $this->data=self::load_($this->filename,$default);
25
	}
26
	
27
	public function save(): bool {
28
		return self::save_($this->filename, $this->data);
29
	}
30
	
31
	public function get(string $key,$default=null){
32
		return $this->data[$key]??$default;
33
	}
34
	
35
	public function set(string $key,$value):self{
36
		$this->data[$key]=$value;
37
		return $this;
38
	}
39
40
	/**
41
	 * @return array
42
	 */
43
	public function getData(): array {
44
		return $this->data;
45
	}
46
47
	/**
48
	 * @param array $data
49
	 */
50
	public function setData(array $data): self {
51
		$this->data = $data;
52
		return $this;
53
	}
54
55
56
	
57
	public static function load_(string $filename,array $default=[]):array {
58
		if(\file_exists($filename)){
59
			return include($filename);
60
		}
61
		return $default;
62
	}
63
	
64
	public static function save_(string $filename,array $data):bool{
65
		return false!==UFileSystem::save($filename, '<?php return '.UArray::asPhpArray_($data,1,true).';');
66
	}
67
}
68
69