Completed
Push — master ( 3158a5...be27c2 )
by Pavel
02:16
created

PHPConstantsOutputAdapter::addSlashes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Paro\EnvironmentParameters\Adapter\Output;
4
5
6
class PHPConstantsOutputAdapter implements OutputAdapterInterface
7
{
8
    public static function getName()
9
    {
10
        return 'php-constants';
11
    }
12
13
    public function process($parameters, $fileName, $env)
14
    {
15
        $content = sprintf("<?php\n/** This file is auto-generated during the build process of '%s' environment at %s **/\n", $env, date(DATE_ATOM));
16
        foreach ($parameters as $key => $value) {
17
            $content .= sprintf("define('%s', %s);\n", $key, $this->serialize($value));
18
        };
19
        file_put_contents($fileName, $content, 99);
20
    }
21
22
	/**
23
	 * @param $value
24
	 * @return string
25
	 */
26
    protected function serialize($value) {
27
	    switch (gettype($value)) {
28
		    case 'boolean':
29
			    return $value ? 'true' : 'false';
30
		    case 'array':
31
		    case 'object':
32
			    return "'" . $this->addSlashes(serialize($value)) . "'";
33
		    case 'NULL':
34
			    return "null";
35
		    case 'integer':
36
		    case 'double':
37
			    return $value;
38
		    default:
39
			    return "'" . $this->addslashes($value) . "'";
40
	    }
41
	}
42
43
	private function addSlashes($value) {
44
		return str_replace("'", "\\'", $value);
45
	}
46
47
}