Skip to content

Commit

Permalink
Simplified impelemtation
Browse files Browse the repository at this point in the history
  • Loading branch information
klapuch committed Nov 20, 2016
1 parent 6ec1a93 commit 85bc7e7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
3 changes: 1 addition & 2 deletions Core/Fake.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function write(array $values) {
public function remove($key, string $section = null) {
if($section === null)
unset($this->ini[$key]);
else
unset($this->ini[$section][$key]);
unset($this->ini[$section][$key]);
}
}
27 changes: 14 additions & 13 deletions Core/Typed.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,32 @@ public function remove($key, string $section = null) {
$ini = $this->read();
if($section === null)
unset($ini[$key]);
else
unset($ini[$section][$key]);
unset($ini[$section][$key]);
file_put_contents($this->path, $this->toIni($ini));
}

/**
* Converts the array to ini format
* Convert the array to ini format
* @param array $values
* @return string
*/
private function toIni(array $values): string {
$ini = '';
foreach($values as $key => $value) {
if($this->isArray($value)) {
$ini .= sprintf('[%s]', $key) . self::CRLF;
$ini .= $this->toIni($value);
} else
$ini .= sprintf('%s=%s', $key, $value) . self::CRLF;
}
return $ini;
return implode(
array_map(
function(string $key, $value): string {
return $this->isArray($value)
? sprintf('[%s]', $key) . self::CRLF . $this->toIni($value)
: sprintf('%s=%s', $key, $value) . self::CRLF;
},
array_keys($values),
$values
)
);
}

/**
* Is the given value an array?
* @param $value
* @param mixed $value
* @return bool
*/
private function isArray($value): bool {
Expand Down
7 changes: 1 addition & 6 deletions Core/Untyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public function remove($key, string $section = null) {
* @return array
*/
private function toUnitedType(array $ini): array {
return array_map(
function($value) {
return (string)$value;
},
$ini
);
return array_map('strval', $ini);
}
}
11 changes: 6 additions & 5 deletions Core/Valid.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(string $path, Ini $origin) {
}

public function read(): array {
if(!is_readable($this->path) || !$this->isIni()) {
if(!is_readable($this->path) || !$this->isIni($this->path)) {
throw new \InvalidArgumentException(
sprintf(
'File "%s" must be readable ini file',
Expand All @@ -27,7 +27,7 @@ public function read(): array {
}

public function write(array $values) {
if(!is_writable($this->path) || !$this->isIni()) {
if(!is_writable($this->path) || !$this->isIni($this->path)) {
throw new \InvalidArgumentException(
sprintf(
'File "%s" must be writable ini file',
Expand All @@ -43,10 +43,11 @@ public function remove($key, string $section = null) {
}

/**
* Is the $this->path valid ini file?
* Is the path valid ini file?
* @param string $path
* @return bool
*/
private function isIni(): bool {
return strtolower(pathinfo($this->path, PATHINFO_EXTENSION)) === 'ini';
private function isIni(string $path): bool {
return strtolower(pathinfo($path, PATHINFO_EXTENSION)) === 'ini';
}
}
14 changes: 14 additions & 0 deletions Tests/Core/Unit/Typed.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ final class Typed extends Tester\TestCase {
Assert::same("foo=666\r\n[SECTION]\r\n", file_get_contents($ini));
}

public function testRemovingSameNamedSectionAsKey() {
$ini = $this->preparedIni();
file_put_contents($ini, "foo=666\r\n[bar]\r\nbar=123\r\n");
(new Ini\Typed($ini))->remove('bar', 'bar');
Assert::same("foo=666\r\n[bar]\r\n", file_get_contents($ini));
}

public function testRemovingNullSection() {
$ini = $this->preparedIni();
file_put_contents($ini, "foo=666\r\n[null]\r\nbar=123\r\n");
(new Ini\Typed($ini))->remove('bar', null);
Assert::same("foo=666\r\n[null]\r\nbar=123\r\n", file_get_contents($ini));
}

private function preparedIni() {
return Tester\FileMock::create('', 'ini');
}
Expand Down

0 comments on commit 85bc7e7

Please sign in to comment.