-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathEnvironmentFile.php
More file actions
88 lines (68 loc) · 1.9 KB
/
Copy pathEnvironmentFile.php
File metadata and controls
88 lines (68 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace Frosh\Tools\Components\Environment;
class EnvironmentFile implements \Stringable
{
/**
* @param array<string, EnvironmentLine> $items
*/
public function __construct(private array $items) {}
public function __toString(): string
{
$content = '';
foreach ($this->items as $item) {
$content .= $item->getLine() . \PHP_EOL;
}
return $content;
}
public function has(string $key): bool
{
return $this->get($key) instanceof EnvironmentKeyValue;
}
public function get(string $key): ?EnvironmentKeyValue
{
foreach ($this->items as $item) {
if ($item instanceof EnvironmentKeyValue && $item->getKey() === $key) {
return $item;
}
}
return null;
}
public function set(string $key, string $value): void
{
$v = $this->get($key);
if ($v instanceof EnvironmentKeyValue) {
$v->setValue($value);
return;
}
$this->items[] = EnvironmentKeyValue::parse($key . '=' . $value);
}
public function delete(string $key): void
{
foreach ($this->items as $i => $item) {
if ($item instanceof EnvironmentKeyValue && $item->getKey() === $key) {
unset($this->items[$i]);
}
}
}
public function keys(): array
{
$keys = [];
foreach ($this->items as $item) {
if ($item instanceof EnvironmentKeyValue) {
$keys[] = $item->getKey();
}
}
return $keys;
}
public function values(): array
{
$values = [];
foreach ($this->items as $item) {
if ($item instanceof EnvironmentKeyValue) {
$values[$item->getKey()] = $item->getValue();
}
}
return $values;
}
}