1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-config for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-config/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-config/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\Config\Writer;
10
11use Laminas\Config\Exception;
12
13class JavaProperties extends AbstractWriter
14{
15    const DELIMITER_DEFAULT = ':';
16
17    /**
18     * Delimiter for key/value pairs.
19     */
20    private $delimiter;
21
22    /**
23     * @param string $delimiter Delimiter to use for key/value pairs; defaults
24     *     to self::DELIMITER_DEFAULT (':')
25     * @throws Exception\InvalidArgumentException for invalid $delimiter values.
26     */
27    public function __construct($delimiter = self::DELIMITER_DEFAULT)
28    {
29        if (! is_string($delimiter) || '' === $delimiter) {
30            throw new Exception\InvalidArgumentException(sprintf(
31                'Invalid delimiter of type "%s"; must be a non-empty string',
32                is_object($delimiter) ? get_class($delimiter) : gettype($delimiter)
33            ));
34        }
35
36        $this->delimiter = $delimiter;
37    }
38
39    /**
40     * processConfig(): defined by AbstractWriter.
41     *
42     * @param  array $config
43     * @return string
44     * @throws Exception\UnprocessableConfigException for non-scalar values in
45     *     the $config array.
46     */
47    public function processConfig(array $config)
48    {
49        $string = '';
50
51        foreach ($config as $key => $value) {
52            if (! is_scalar($value)) {
53                throw new Exception\UnprocessableConfigException(sprintf(
54                    '%s configuration writer can only process scalar values; received "%s" for key "%s"',
55                    __CLASS__,
56                    is_object($value) ? get_class($value) : gettype($value),
57                    $key
58                ));
59            }
60
61            $value = (null === $value) ? '' : $value;
62
63            $string .= sprintf(
64                "%s%s%s\n",
65                $key,
66                $this->delimiter,
67                $value
68            );
69        }
70
71        return $string;
72    }
73}
74