Passed
Push — master ( 8a8111...583a39 )
by Jean-Christophe
19:52
created

UConfigFile::save_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 2
	public function __construct(string $name){
20 2
		$this->filename=\ROOT . "config/$name.config.php";
21
	}
22
	
23 1
	public function load(array $default=[]): array {
24 1
		return $this->data=self::load_($this->filename,$default);
25
	}
26
	
27 1
	public function save(): bool {
28 1
		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 1
	public function setData(array $data): self {
51 1
		$this->data = $data;
52 1
		return $this;
53
	}
54
55
56
	
57 1
	public static function load_(string $filename,array $default=[]):array {
58 1
		if(\file_exists($filename)){
59 1
			return include($filename);
60
		}
61
		return $default;
62
	}
63
	
64 1
	public static function save_(string $filename,array $data):bool{
65 1
		return false!==UFileSystem::save($filename, '<?php return '.UArray::asPhpArray_($data,1,true).';');
66
	}
67
}
68
69