Skip to content
This repository has been archived by the owner on Jun 22, 2018. It is now read-only.

Commit

Permalink
update fileloader
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Oct 12, 2017
1 parent 5b634be commit 5acd4dc
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/SDK/FileLoader.php
Expand Up @@ -20,6 +20,7 @@ class FileLoader
'yaml' => FileLoader\YamlLoader::class,
'json' => FileLoader\JSONLoader::class,
'xml' => FileLoader\XmlLoader::class,
'ini' => FileLoader\IniLoader::class,
];

/**
Expand Down
52 changes: 52 additions & 0 deletions src/SDK/FileLoader/IniLoader.php
@@ -0,0 +1,52 @@
<?php

namespace Bavix\SDK\FileLoader;

class IniLoader implements DataInterface
{

use DataTrait;

/**
* @var IniWriter
*/
protected $writer;

/**
* @return IniWriter
*/
protected function writer()
{
if (!$this->writer)
{
$this->writer = new IniWriter();
}

return $this->writer;
}

/**
* @inheritdoc
*/
public function asArray()
{
if (!$this->data)
{
$this->data = \parse_ini_file($this->path, true);
}

return $this->data;
}

/**
* @inheritdoc
*/
public function save($data)
{
$data = $this->_fromArray($data);

return (int)$this->writer()
->toFile($this->path, $data);
}

}
118 changes: 118 additions & 0 deletions src/SDK/FileLoader/IniWriter.php
@@ -0,0 +1,118 @@
<?php

namespace Bavix\SDK\FileLoader;

use Bavix\Exceptions\Runtime;
use Bavix\Helpers\File;

class IniWriter
{

/**
* @param string $filename
* @param array $config
* @param string $header
*
* @return int
*/
public function toFile($filename, array $config, $header = null)
{
$ini = $this->toString($config, $header);
return File::put($filename, $ini);
}

/**
* @param array $config
* @param null $header
*
* @return string
*/
public function toString(array $config, $header = null)
{
$ini = !empty($header) ? $header . PHP_EOL : '';

uasort($config, function ($first, $second) {
if (is_array($first)) {
return 1;
}

if (is_array($second))
{
return -1;
}

return 0;
});

$names = array_keys($config);

foreach ($names as $name)
{
$section = $config[$name];

if (!is_array($section))
{
$ini .= $name . ' = ' . $this->encodeValue($section) . PHP_EOL;
continue;
}

if (empty($section))
{
continue;
}

if (!empty($ini))
{
$ini .= PHP_EOL;
}

$ini .= "[$name]" . PHP_EOL;

foreach ($section as $option => $value)
{
if (is_numeric($option))
{
$option = $name;
$value = (array)$value;
}

if (is_array($value))
{
foreach ($value as $key => $currentValue)
{
$ini .= $option . '[' . $key . '] = ' . $this->encodeValue($currentValue) . PHP_EOL;
}
}
else
{
$ini .= $option . ' = ' . $this->encodeValue($value) . PHP_EOL;
}
}

$ini .= "\n";
}

return $ini;
}

/**
* @param $value
*
* @return int|string
*/
protected function encodeValue($value)
{
if (is_bool($value))
{
return (int)$value;
}

if (is_string($value))
{
return '"' . $value . '"';
}

return $value;
}

}
4 changes: 2 additions & 2 deletions src/SDK/FileLoader/JSONLoader.php
Expand Up @@ -17,8 +17,8 @@ public function asArray()
{
if (!$this->data)
{
$yml = \file_get_contents($this->path);
$this->data = JSON::decode($yml);
$data = \file_get_contents($this->path);
$this->data = JSON::decode($data);
}

return $this->data;
Expand Down
2 changes: 0 additions & 2 deletions src/SDK/FileLoader/PHPLoader.php
Expand Up @@ -2,8 +2,6 @@

namespace Bavix\SDK\FileLoader;

use Bavix\Slice\Slice;

class PHPLoader implements DataInterface
{

Expand Down

0 comments on commit 5acd4dc

Please sign in to comment.