1
1
<?php
2
+ declare (strict_types=1 );
2
3
/**
3
4
* MIT License
4
5
*
28
29
use doganoo \PHPUtil \Exception \FileNotFoundException ;
29
30
use doganoo \PHPUtil \Exception \InvalidPropertyStructureException ;
30
31
use doganoo \PHPUtil \Exception \NoPathException ;
32
+ use function array_keys ;
33
+ use function count ;
34
+ use function is_file ;
35
+ use function trim ;
31
36
32
37
/**
33
- * Class SysProperties
34
- *
35
- * @package Core\Objects
38
+ * Class Properties
39
+ * @package doganoo\PHPUtil\System
40
+ * @author Dogan Ucar <dogan@dogan-ucar.de>
36
41
*/
37
- class SysProperties {
42
+ class Properties {
38
43
39
- /**
40
- * @var null
41
- */
42
- private static $ path = null ;
44
+ /** @var string */
45
+ private $ path ;
43
46
44
- /**
45
- * @var null|array $properties
46
- */
47
- private $ properties = null ;
47
+ /** @var array */
48
+ private $ properties ;
48
49
49
50
/**
51
+ * Properties constructor.
50
52
* @param string $path
51
53
*/
52
- public static function setPropertiesPath (string $ path ) {
53
- self :: $ path = $ path ;
54
+ public function __construct (string $ path ) {
55
+ $ this -> path = $ path ;
54
56
}
55
57
56
58
/**
57
- * @param string $index
58
- * @return string
59
- * @throws FileNotFoundException
60
- * @throws NoPathException
61
- * @throws InvalidPropertyStructureException
59
+ * reads the property value assigned to $key
60
+ * @param string $key
61
+ * @return string|null
62
62
*/
63
- public function read (string $ index ): string {
64
- $ index = \trim ($ index );
65
- $ properties = $ this ->getProperties ();
66
- if (isset ($ properties [$ index ])) {
67
- return $ properties [$ index ];
68
- } else {
69
- throw new \InvalidArgumentException ();
70
- }
63
+ public function read (string $ key ): ?string {
64
+ $ key = trim ($ key );
65
+ $ properties = $ this ->loadProperties ();
66
+ return $ this ->properties [$ key ] ?? null ;
71
67
}
72
68
73
69
/**
70
+ * loads the ini file located at a given path
71
+ *
74
72
* @return array
75
73
* @throws FileNotFoundException
76
- * @throws NoPathException
77
74
* @throws InvalidPropertyStructureException
75
+ * @throws NoPathException
78
76
*/
79
- private function getProperties (): array {
77
+ private function loadProperties (): array {
80
78
if (null !== $ this ->properties ) {
81
79
return $ this ->properties ;
82
80
}
83
- if (self :: $ path === null ) {
81
+ if (null === $ this -> path ) {
84
82
throw new NoPathException ();
85
83
}
86
- if (!\ is_file (self :: $ path )) {
84
+ if (!is_file ($ this -> path )) {
87
85
throw new FileNotFoundException ();
88
86
}
89
- $ ini = parse_ini_file (self :: $ path );
87
+ $ ini = parse_ini_file ($ this -> path );
90
88
if (false === $ ini ) {
91
89
throw new InvalidPropertyStructureException ();
92
90
}
@@ -95,26 +93,23 @@ private function getProperties(): array {
95
93
}
96
94
97
95
/**
96
+ * returns the number of properties
97
+ *
98
98
* @return int
99
- * @throws FileNotFoundException
100
- * @throws InvalidPropertyStructureException
101
- * @throws NoPathException
102
99
*/
103
100
public function size (): int {
104
- $ properties = $ this ->getProperties ();
105
- return \ count ($ properties );
101
+ $ properties = $ this ->loadProperties ();
102
+ return count ($ properties );
106
103
}
107
104
108
105
/**
106
+ * returns the key set of the properties
107
+ *
109
108
* @return array
110
- * @throws FileNotFoundException
111
- * @throws InvalidPropertyStructureException
112
- * @throws NoPathException
113
109
*/
114
110
public function keySet (): array {
115
- $ array = $ this ->getProperties ();
116
- $ array = \array_keys ($ array );
117
- return $ array ;
111
+ $ array = $ this ->loadProperties ();
112
+ return array_keys ($ array );
118
113
}
119
114
120
- }
115
+ }
0 commit comments