Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from jcsanyi/ini-scanner-mode
Browse files Browse the repository at this point in the history
Ini scanner mode
  • Loading branch information
weierophinney authored Feb 11, 2021
2 parents eff2f8d + 9ea16e6 commit f91cd6f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
15 changes: 14 additions & 1 deletion docs/book/reader.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ function. Please review this documentation to be aware of its specific behaviors
> ### Process Sections
>
> By default, the INI reader executes `parse_ini_file()` with the optional parameter `$process_sections` being `true`. The result is a multidimensional array, with the section names and settings included.
> By default, the INI reader executes `parse_ini_file()` with the optional parameter `$process_sections` being `true`.
> The result is a multidimensional array, with the section names and settings included.
>
> To merge sections, set the parameter via `setProcessSections()` to `false` as follows.
>
Expand All @@ -54,6 +55,18 @@ function. Please review this documentation to be aware of its specific behaviors
> $reader->setProcessSections(false);
> ```
> ### Typed Mode
>
> By default, the INI reader executes `parse_ini_file()` with the optional parameter `$scanner_mode` set to `INI_SCANNER_NORMAL`.
> This results in all config values being returned as strings.
>
> To automatically return integer, boolean, and null values as the appropriate types, switch to typed mode with `setTypedMode()`, and `parse_ini_file()` will be called with `INI_SCANNER_TYPED` instead.
>
> ```php
> $reader = new Laminas\Config\Reader\Ini();
> $reader->setTypedMode(true);
> ```
The following example illustrates basic usage of `Laminas\Config\Reader\Ini` for
loading configuration data from an INI file. In this example, configuration data
for both a production system and for a staging system exists.
Expand Down
47 changes: 45 additions & 2 deletions src/Reader/Ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class Ini implements ReaderInterface
*/
protected $processSections = true;

/**
* Flag which determines whether boolean, null, and integer values should be
* returned as their proper types.
*
* @see https://www.php.net/parse_ini_file
* @var bool
*/
protected $typedMode = false;

/**
* Set nest separator.
*
Expand Down Expand Up @@ -104,6 +113,40 @@ public function getProcessSections()
return $this->processSections;
}

/**
* Set whether boolean, null, and integer values should be returned as their proper types.
* When set to false, all values will be returned as strings.
*
* @see https://www.php.net/parse_ini_file
*/
public function setTypedMode(bool $typedMode): self
{
$this->typedMode = $typedMode;
return $this;
}

/**
* Get whether boolean, null, and integer values should be returned as their proper types.
* When set to false, all values will be returned as strings.
*
* @see https://www.php.net/parse_ini_file
*/
public function getTypedMode(): bool
{
return $this->typedMode;
}

/**
* Get the scanner-mode constant value to be used with the built-in parse_ini_file function.
* Either INI_SCANNER_NORMAL or INI_SCANNER_TYPED depending on $typedMode.
*
* @see https://www.php.net/parse_ini_file
*/
public function getScannerMode(): int
{
return $this->getTypedMode() ? INI_SCANNER_TYPED : INI_SCANNER_NORMAL;
}

/**
* fromFile(): defined by Reader interface.
*
Expand Down Expand Up @@ -132,7 +175,7 @@ function ($error, $message = '') use ($filename) {
},
E_WARNING
);
$ini = parse_ini_file($filename, $this->getProcessSections());
$ini = parse_ini_file($filename, $this->getProcessSections(), $this->getScannerMode());
restore_error_handler();

return $this->process($ini);
Expand Down Expand Up @@ -161,7 +204,7 @@ function ($error, $message = '') {
},
E_WARNING
);
$ini = parse_ini_string($string, $this->getProcessSections());
$ini = parse_ini_string($string, $this->getProcessSections(), $this->getScannerMode());
restore_error_handler();

return $this->process($ini);
Expand Down
70 changes: 70 additions & 0 deletions test/Reader/IniTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,74 @@ public function testFromStringIgnoresNestingInSectionNamesWhenSectionsNotProcess
self::assertEquals('foo', $arrayIni['production_key']);
self::assertEquals('bar', $arrayIni['staging_key']);
}

public function testFromFileWithoutTypes()
{
$arrayIni = $this->reader->fromFile($this->getTestAssetPath('types'));

self::assertSame('Bob Smith', $arrayIni['production']['name']);
self::assertSame('55', $arrayIni['production']['age']);
self::assertSame('55', $arrayIni['production']['age_str']);
self::assertSame('1', $arrayIni['production']['is_married']);
self::assertSame('', $arrayIni['production']['is_employed']);
self::assertSame('', $arrayIni['production']['employer']);
}

public function testFromFileWithTypes()
{
$reader = $this->reader;
$reader->setTypedMode(true);
$arrayIni = $reader->fromFile($this->getTestAssetPath('types'));

self::assertSame('Bob Smith', $arrayIni['production']['name']);
self::assertSame(55, $arrayIni['production']['age']);
self::assertSame('55', $arrayIni['production']['age_str']);
self::assertSame(true, $arrayIni['production']['is_married']);
self::assertSame(false, $arrayIni['production']['is_employed']);
self::assertSame(null, $arrayIni['production']['employer']);
}

public function testFromStringWithoutTypes()
{
$ini = <<<ECS
[production]
name="Bob Smith"
age=55
age_str="55"
is_married=yes
is_employed=FALSE
employer=null
ECS;
$arrayIni = $this->reader->fromString($ini);

self::assertSame('Bob Smith', $arrayIni['production']['name']);
self::assertSame('55', $arrayIni['production']['age']);
self::assertSame('55', $arrayIni['production']['age_str']);
self::assertSame('1', $arrayIni['production']['is_married']);
self::assertSame('', $arrayIni['production']['is_employed']);
self::assertSame('', $arrayIni['production']['employer']);
}

public function testFromStringWithTypes()
{
$ini = <<<ECS
[production]
name="Bob Smith"
age=55
age_str="55"
is_married=yes
is_employed=FALSE
employer=null
ECS;
$reader = $this->reader;
$reader->setTypedMode(true);
$arrayIni = $reader->fromString($ini);

self::assertSame('Bob Smith', $arrayIni['production']['name']);
self::assertSame(55, $arrayIni['production']['age']);
self::assertSame('55', $arrayIni['production']['age_str']);
self::assertSame(true, $arrayIni['production']['is_married']);
self::assertSame(false, $arrayIni['production']['is_employed']);
self::assertSame(null, $arrayIni['production']['employer']);
}
}
7 changes: 7 additions & 0 deletions test/Reader/TestAssets/Ini/types.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[production]
name="Bob Smith"
age=55
age_str="55"
is_married=yes
is_employed=FALSE
employer=null

0 comments on commit f91cd6f

Please sign in to comment.