Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output code style (Fixes #28) #64

Merged
merged 6 commits into from
Jun 26, 2020
Merged
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
93 changes: 53 additions & 40 deletions src/app/Services/LangFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function setFile($file)
}

/**
* get the content of a language file as an array sorted ascending.
* @return array|false
* Get the content of a language file as an array sorted ascending.
* @return array|false
*/
public function getFileContent()
{
Expand All @@ -46,25 +46,26 @@ public function getFileContent()
}

/**
* rewrite the file with the modified texts.
* @param array $postArray the data received from the form
* Rewrite the file with the modified texts.
* @param array $postArray the data received from the form
* @return int
*/
public function setFileContent($postArray)
{
$postArray = $this->prepareContent($postArray);

$return = (int) file_put_contents(
$this->getFilePath(),
print_r("<?php \n\n return ".$this->var_export54($postArray).';', true)
);
$this->getFilePath(),
print_r("<?php\n\nreturn ".$this->var_export54($postArray).";\n", true)
);

return $return;
}

/**
* get the language files that can be edited, to ignore a file add it in the config/admin file to language_ignore key.
* @return array
* Get the language files that can be edited,
* to ignore a file add it in the config/admin file to language_ignore key.
* @return array
*/
public function getlangFiles()
{
Expand Down Expand Up @@ -92,9 +93,9 @@ public function getlangFiles()
}

/**
* check if all the fields were completed.
* @param array $postArray the array containing the data
* @return array
* Check if all the fields were completed.
* @param array $postArray the array containing the data
* @return array
*/
public function testFields($postArray)
{
Expand All @@ -120,12 +121,12 @@ public function testFields($postArray)
}

/**
* display the form that permits the editing.
* @param array $fileArray the array with all the texts
* @param array $parents all the ancestor keys of the current key
* @param string $parent the parent key of the current key
* @param int $level the current level
* @return void
* Display the form that permits the editing.
* @param array $fileArray the array with all the texts
* @param array $parents all the ancestor keys of the current key
* @param string $parent the parent key of the current key
* @param int $level the current level
* @return void
*/
public function displayInputs($fileArray, $parents = [], $parent = '', $level = 0)
{
Expand All @@ -135,16 +136,28 @@ public function displayInputs($fileArray, $parents = [], $parent = '', $level =
}
foreach ($fileArray as $key => $item) {
if (is_array($item)) {
echo view()->make('langfilemanager::language_headers', ['header' => $key, 'parents' => $parents, 'level' => $level, 'item' => $item, 'langfile' => $this, 'lang_file_name' => $this->file])->render();
echo view()->make('langfilemanager::language_headers', [
'header' => $key,
'parents' => $parents,
'level' => $level,
'item' => $item,
'langfile' => $this,
'lang_file_name' => $this->file,
])->render();
} else {
echo view()->make('langfilemanager::language_inputs', ['key' => $key, 'item' => $item, 'parents' => $parents, 'lang_file_name' => $this->file])->render();
echo view()->make('langfilemanager::language_inputs', [
'key' => $key,
'item' => $item,
'parents' => $parents,
'lang_file_name' => $this->file,
])->render();
}
}
}

/**
* create the array that will be saved in the file.
* @param array $postArray the array to be transformed
* Create the array that will be saved in the file.
* @param array $postArray The array to be transformed
* @return array
*/
private function prepareContent($postArray)
Expand All @@ -159,11 +172,12 @@ private function prepareContent($postArray)
if (is_array($item)) {
if (isset($item['before'])) {
$items_arr = array_map(
function ($item1, $item2) {
return $item1.$item2;
},
str_replace('|', '&#124;', $item['before']), str_replace('|', '&#124;', $item['after'])
);
function ($item1, $item2) {
return $item1.$item2;
},
str_replace('|', '&#124;', $item['before']),
str_replace('|', '&#124;', $item['after'])
);
$value = $this->sanitize(implode('|', $items_arr));
} else {
$value = $this->sanitize(implode('|', str_replace('|', '&#124;', $item['after'])));
Expand All @@ -179,21 +193,21 @@ function ($item1, $item2) {
}

/**
* add filters to the values inserted by the user.
* @param string $str the string to be sanitized
* @return string
* Add filters to the values inserted by the user.
* @param string $str the string to be sanitized
* @return string
*/
private function sanitize($str)
{
return trim($str);
}

/**
* set a value in a multidimensional array when knowing the keys.
* @param array $data the array that will be modified
* @param array $keys the keys (path)
* @param string $value the value to be added
* @return array
* Set a value in a multidimensional array when knowing the keys.
* @param array $data the array that will be modified
* @param array $keys the keys (path)
* @param string $value the value to be added
* @return array
*/
private function setArrayValue(&$data, $keys, $value)
{
Expand All @@ -218,19 +232,18 @@ private function var_export54($var, $indent = '')
{
switch (gettype($var)) {
case 'string':
return '"'.addcslashes($var, "\\\$\"\r\n\t\v\f").'"';
return "'".addcslashes($var, "\\\$'\r\n\t\v\f")."'";
case 'array':
$indexed = array_keys($var) === range(0, count($var) - 1);
$r = [];
foreach ($var as $key => $value) {
$r[] = "$indent "
.($indexed ? '' : $this->var_export54($key).' => ')
.(is_numeric($key) ? '' : $this->var_export54($key).' => ')
.$this->var_export54($value, "$indent ");
}

return "[\n".implode(",\n", $r)."\n".$indent.']';
return "[\n".implode(",\n", $r).",\n{$indent}]";
case 'boolean':
return $var ? 'TRUE' : 'FALSE';
return $var ? 'true' : 'false';
default:
return var_export($var, true);
}
Expand Down