|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the m1\vars library |
|
5
|
|
|
* |
|
6
|
|
|
* (c) m1 <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @package m1/vars |
|
12
|
|
|
* @version 1.1.0 |
|
13
|
|
|
* @author Miles Croxford <[email protected]> |
|
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
|
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
|
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace M1\Vars\Traits; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Vars transformer class for to*() functions |
|
23
|
|
|
* |
|
24
|
|
|
* @since 0.2.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
trait TransformerTrait |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Makes it so the content is available in getenv() |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function toEnv() |
|
32
|
|
|
{ |
|
33
|
1 |
|
$dots = $this->toDots(); |
|
34
|
|
|
|
|
35
|
1 |
|
if (is_array($dots)) { |
|
36
|
1 |
|
foreach ($dots as $dot_k => $dot_v) { |
|
37
|
1 |
|
putenv(sprintf('%s=%s', $dot_k, $dot_v)); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Converts the array into a flat dot notation array |
|
44
|
|
|
* |
|
45
|
|
|
* @param bool $flatten_array Flatten arrays into none existent keys |
|
46
|
|
|
* |
|
47
|
|
|
* @return array The dot notation array |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function toDots($flatten_array = true) |
|
50
|
|
|
{ |
|
51
|
2 |
|
return (!is_null($this->content)) ? $this->dotTransformer($this->content, $flatten_array) : $this->content; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Converts the array into a flat dot notation array |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $content The content array |
|
58
|
|
|
* @param bool $flatten_array Flatten arrays into none existent keys |
|
59
|
|
|
* @param string $prefix The prefix for the key |
|
60
|
|
|
* |
|
61
|
|
|
* @return array The dot notation array |
|
62
|
|
|
*/ |
|
63
|
2 |
|
private function dotTransformer($content, $flatten_array, $prefix = '') |
|
64
|
|
|
{ |
|
65
|
2 |
|
$parsed = array(); |
|
66
|
2 |
|
foreach ($content as $arr_k => $arr_v) { |
|
67
|
2 |
|
if (is_array($arr_v)) { |
|
68
|
1 |
|
if (!$flatten_array) { |
|
69
|
|
|
$parsed[$prefix.$arr_k] = $arr_v; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
$parsed = array_merge($parsed, $this->dotTransformer($arr_v, $flatten_array, $prefix.$arr_k.".")); |
|
73
|
|
|
} else { |
|
74
|
2 |
|
$parsed[$prefix.$arr_k] = $arr_v; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return $parsed; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: