Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Adds ability to specify delimiter for key/value pairs #45

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/Reader/JavaProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,37 @@
*/
class JavaProperties implements ReaderInterface
{
const DELIMITER_DEFAULT = ':';

/**
* Directory of the Java-style properties file
*
* @var string
*/
protected $directory;

/**
* Delimiter for key/value pairs.
*/
private $delimiter;

/**
* @param string $delimiter Delimiter to use for key/value pairs; defaults
* to self::DELIMITER_DEFAULT (':')
* @throws Exception\InvalidArgumentException for invalid $delimiter values.
*/
public function __construct($delimiter = self::DELIMITER_DEFAULT)
{
if (! is_string($delimiter) || '' === $delimiter) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid delimiter of type "%s"; must be a non-empty string',
is_object($delimiter) ? get_class($delimiter) : gettype($delimiter)
));
}

$this->delimiter = $delimiter;
}

/**
* fromFile(): defined by Reader interface.
*
Expand Down Expand Up @@ -99,6 +123,8 @@ protected function process(array $data)
*/
protected function parse($string)
{
$delimiter = $this->delimiter;
$delimLength = strlen($delimiter);
$result = [];
$lines = explode("\n", $string);
$key = "";
Expand All @@ -113,8 +139,8 @@ protected function parse($string)

// Add a new key-value pair or append value to a previous pair
if (! $isWaitingOtherLine) {
$key = substr($line, 0, strpos($line, ':'));
$value = substr($line, strpos($line, ':') + 1, strlen($line));
$key = substr($line, 0, strpos($line, $delimiter));
$value = substr($line, strpos($line, $delimiter) + $delimLength, strlen($line));
} else {
$value .= $line;
}
Expand Down
37 changes: 37 additions & 0 deletions test/Reader/JavaPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,41 @@ public function testInvalidIncludeInString()
$this->expectExceptionMessage($expectedErrorMessage);
$arrayJavaPropterties = $this->reader->fromString($JavaProperties);
}

public function testAllowsSpecifyingAlternateKeyValueDelimiter()
{
$reader = new JavaProperties('=');

$arrayJavaProperties = $reader->fromFile($this->getTestAssetPath('alternate-delimiter'));

$this->assertNotEmpty($arrayJavaProperties);
$this->assertEquals($arrayJavaProperties['single.line'], 'test');
$this->assertEquals($arrayJavaProperties['multiple'], 'line test');
}

public function invalidDelimiters()
{
return [
'null' => [null],
'true' => [true],
'false' => [false],
'zero' => [0],
'int' => [1],
'zero-float' => [0.0],
'float' => [1.1],
'empty-string' => [''],
'array' => [[':']],
'object' => [(object) ['delimiter' => ':']],
];
}

/**
* @dataProvider invalidDelimiters
* @param mixed $delimiter
*/
public function testInvalidDelimiterValuesResultInExceptions($delimiter)
{
$this->expectException(Exception\InvalidArgumentException::class);
new JavaProperties($delimiter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
single.line=test
multiple=line \
test