Passed
Push — master ( 2f56fd...c7e98d )
by Anton
05:11 queued 01:55
created

_Object::remove()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
3
namespace Utils\Schema {
4
5
	use Explorer, JSON;
6
7
	class _Object {
8
9
		# File name interface
10
11
		protected static $file_name = '';
12
13
		# Properties list
14
15
		protected $properties = [];
16
17
		# Add array property
18
19
		protected function addArray(string $name) {
20
21
			if ('' !== $name) return $this->properties[$name] = new _Array;
22
		}
23
24
		# Add boolean property
25
26
		protected function addBoolean(string $name) {
27
28
			if ('' !== $name) return $this->properties[$name] = new _Boolean;
29
		}
30
31
		# Add integer property
32
33
		protected function addInteger(string $name) {
34
35
			if ('' !== $name) return $this->properties[$name] = new _Integer;
36
		}
37
38
		# Add object property
39
40
		protected function addObject(string $name) {
41
42
			if ('' !== $name) return $this->properties[$name] = new _Object;
43
		}
44
45
		# Add string property
46
47
		protected function addString(string $name) {
48
49
			if ('' !== $name) return $this->properties[$name] = new _String;
50
		}
51
52
		# Validate data
53
54
		public function validate($data) {
55
56
			if (!is_array($data)) return null;
57
58
			$result = [];
59
60
			foreach ($this->properties as $name => $property) {
61
62
				if (!isset($data[$name]) || (null === ($value = $property->validate($data[$name])))) return null;
63
64
				$result[$name] = $value;
65
			}
66
67
			# ------------------------
68
69
			return $result;
70
		}
71
72
		# Load JSON file
73
74 View Code Duplication
		public function load() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
76
			if ('' === static::$file_name) return null;
77
78
			$file_name = (DIR_SYSTEM_DATA . static::$file_name);
79
80
			if (null === ($data = JSON::load($file_name))) return null;
81
82
			if (null === ($data = $this->validate($data))) return null;
83
84
			# ------------------------
85
86
			return $data;
87
		}
88
89
		# Save JSON file
90
91 View Code Duplication
		public function save(array $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
93
			if ('' === static::$file_name) return false;
94
95
			$file_name = (DIR_SYSTEM_DATA . static::$file_name);
96
97
			if (null === ($data = $this->validate($data))) return false;
98
99
			if (false === JSON::save($file_name, $data)) return false;
100
101
			# ------------------------
102
103
			return true;
104
		}
105
106
		# Remove JSON file
107
108
		public function remove() {
109
110
			if ('' === static::$file_name) return false;
111
112
			$file_name = (DIR_SYSTEM_DATA . static::$file_name);
113
114
			return (!Explorer::isFile($file_name) || Explorer::removeFile($file_name));
115
		}
116
	}
117
}
118