Skip to content

Commit

Permalink
* Small changes: Variable renaming and code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilyas Ronef committed Jun 26, 2018
1 parent eb138b5 commit 60a1f48
Showing 1 changed file with 71 additions and 32 deletions.
103 changes: 71 additions & 32 deletions modx.ddtools.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ddTools {

/**
* orderedParamsToNamed
* @version 1.1.3b (2017-02-07)
* @version 1.1.4 (2018-06-26)
*
* @desc Convert list of ordered parameters to named. Method is public, but be advised that this is beta-version!
*
Expand All @@ -130,7 +130,10 @@ public static function orderedParamsToNamed($params){
$message = [];

//Перебираем массив соответствия
foreach ($params->compliance as $index => $name){
foreach (
$params->compliance as
$index => $name
){
//Если параметр задан
if (isset($params->paramsList[$index])){
//Сохраним его
Expand All @@ -149,10 +152,16 @@ public static function orderedParamsToNamed($params){
//General info with code example
$message = '<p>Deprecated ordered parameters.</p><p>Ordered list of parameters is no longer allowed, use the “<a href="https://en.wikipedia.org/wiki/Named_parameter" target="_blank">pass-by-name</a>” style.</p>
<pre><code>//Old style
'.$caller.'($'.implode(', $', $params->compliance).');
'.$caller.'($'.implode(
', $',
$params->compliance
).');
//Pass-by-name
'.$caller.'([
'.implode(','.PHP_EOL."\t", $message).'
'.implode(
','.PHP_EOL."\t",
$message
).'
]);
</code></pre>';

Expand Down Expand Up @@ -206,7 +215,7 @@ public static function explodeAssoc(

/**
* unfoldArray
* @version 1.0.3 (2018-06-17)
* @version 1.0.4 (2018-06-26)
*
* @desc Converts a multidimensional array into an one-dimensional one joining the keys with '.'. It can be helpful while using placeholders like [+size.width+].
* @example [
Expand All @@ -227,24 +236,27 @@ public static function explodeAssoc(
* 'c': ''
* ].
*
* @param $arr {array} — An array to convert. @required
* @param $array {array} — An array to convert. @required
* @param $keyPrefix {string} — Prefix of the keys of an array (it's an internal varible, can be used if required). Default: ''.
*
* @return {array} — Unfolded array.
*/
public static function unfoldArray(
$arr,
$array,
$keyPrefix = ''
){
$output = [];
$result = [];

//Перебираем массив
foreach ($arr as $key => $val){
foreach (
$array as
$key => $val
){
//Если значение является массивом
if (is_array($val)){
//Запускаем рекурсию дальше
$output = array_merge(
$output,
$result = array_merge(
$result,
self::unfoldArray(
$val,
$keyPrefix.$key.'.'
Expand All @@ -253,11 +265,11 @@ public static function unfoldArray(
//Если значение — не массив
}else{
//Запоминаем (в соответствии с ключом родителя)
$output[$keyPrefix.$key] = $val;
$result[$keyPrefix.$key] = $val;
}
}

return $output;
return $result;
}

/**
Expand Down Expand Up @@ -759,7 +771,7 @@ public static function logEvent($params){

/**
* parseText
* @version 1.5 (2018-06-26)
* @version 1.5.1 (2018-06-26)
*
* @desc Similar to $modx->parseChunk, but takes a text.
*
Expand Down Expand Up @@ -806,7 +818,10 @@ public static function parseText($params = []){
//Unfold for arrays support (e. g. “some[a]=one&some[b]=two” => “[+some.a+]”, “[+some.b+]”; “some[]=one&some[]=two” => “[+some.0+]”, “[some.1]”)
$params->data = self::unfoldArray($params->data);

foreach ($params->data as $key => $value){
foreach (
$params->data as
$key => $value
){
$result = str_replace(
$params->placeholderPrefix.$key.$params->placeholderSuffix,
$value,
Expand Down Expand Up @@ -882,7 +897,10 @@ public static function explodeFieldsArr($fields = []){
//Save fields
$result[0] = $docData->fieldsData;
//And TVs
foreach ($docData->tvsData as $tvName => $tvValue){
foreach (
$docData->tvsData as
$tvName => $tvValue
){
$result[1][$tvName] = ['val' => $tvValue];

if (isset($docData->tvsAdditionalData[$tvName])){
Expand All @@ -895,7 +913,7 @@ public static function explodeFieldsArr($fields = []){

/**
* prepareDocData
* @version 2.0 (2018-06-17)
* @version 2.0.1 (2018-06-26)
*
* @desc Prepare document data from single array of fields and TVs: separate them and get TV IDs if needed.
*
Expand Down Expand Up @@ -927,7 +945,10 @@ public static function prepareDocData($params){
];

//Перебираем поля, раскидываем на поля документа и TV
foreach ($params->data as $data_itemFieldName => $data_itemFieldValue){
foreach (
$params->data as
$data_itemFieldName => $data_itemFieldValue
){
//Если это не поле документа
if (!in_array(
$data_itemFieldName,
Expand Down Expand Up @@ -979,7 +1000,7 @@ public static function prepareDocData($params){

/**
* createDocument
* @version 1.2 (2018-06-17)
* @version 1.2.1 (2018-06-26)
*
* @desc Create a new document.
*
Expand Down Expand Up @@ -1027,7 +1048,10 @@ public static function createDocument(
//Если есть хоть одна существующая TV
if (count($docData->tvsAdditionalData) > 0){
//Перебираем массив TV с ID
foreach ($docData->tvsAdditionalData as $tvName => $tvData){
foreach (
$docData->tvsAdditionalData as
$tvName => $tvData
){
if (
//Если это дата
$tvData['type'] == 'date' &&
Expand Down Expand Up @@ -1110,7 +1134,7 @@ public static function createDocument(

/**
* updateDocument
* @version 1.3 (2018-06-17)
* @version 1.3.1 (2018-06-26)
*
* @desc Update a document.
*
Expand Down Expand Up @@ -1190,7 +1214,10 @@ public static function updateDocument(
//Обновляем TV всех найденых документов
while ($doc = self::$modx->db->getRow($docIdsToUpdate_dbRes)){
//Перебираем массив существующих TV
foreach ($docData->tvsAdditionalData as $tvName => $tvData){
foreach (
$docData->tvsAdditionalData as
$tvName => $tvData
){
if (
//Если это дата
$tvData['type'] == 'date' &&
Expand Down Expand Up @@ -1429,7 +1456,7 @@ public static function getDocument(

/**
* getTemplateVars
* @version 1.3.6 (2018-06-17)
* @version 1.3.7 (2018-06-26)
*
* @desc Returns the TV and fields array of a document.
*
Expand Down Expand Up @@ -1537,7 +1564,10 @@ public static function getTemplateVars(
// get default/built-in template variables
ksort($docRow);

foreach ($docRow as $key => $value){
foreach (
$docRow as
$key => $value
){
if (
$idnames == '*' ||
in_array(
Expand Down Expand Up @@ -2026,7 +2056,7 @@ public static function getDocumentIdByUrl($url){

/**
* verifyRenamedParams
* @version 1.1.6 (2018-06-17)
* @version 1.1.7 (2018-06-26)
*
* @desc The method checks an array for deprecated parameters and writes warning messages into the MODX event log. It returns an associative array, in which the correct parameter names are the keys and the parameter values are the values. You can use the “exctract” function to turn the array into variables of the current symbol table.
*
Expand Down Expand Up @@ -2061,7 +2091,10 @@ public static function verifyRenamedParams(
$params_names = array_keys($params);

//Перебираем таблицу соответствия
foreach ($compliance as $newName => $oldNames){
foreach (
$compliance as
$newName => $oldNames
){
//Если параметр с новым именем не задан
if (!isset($params[$newName])){
//Если старое имя только одно, всё равно приведём к массиву для удобства
Expand Down Expand Up @@ -2099,7 +2132,7 @@ public static function verifyRenamedParams(

/**
* sendMail
* @version 3.0.1 (2018-06-17)
* @version 3.0.2 (2018-06-26)
*
* @desc Method for sending e-mails.
*
Expand Down Expand Up @@ -2146,7 +2179,10 @@ public static function sendMail($params){
if(!$_FILES[$value]['tmp_name'][0]){break;}

//Перебираем пост
foreach($_FILES[$value]['name'] as $key => $name){
foreach(
$_FILES[$value]['name'] as
$key => $name
){
//Если нет ошибок
if ($_FILES[$value]['error'][$key] == 0){
//Добавляем в массив файлы
Expand Down Expand Up @@ -2212,7 +2248,7 @@ public static function sendMail($params){

/**
* getResponse
* @version 1.0.4 (2017-05-25)
* @version 1.0.5 (2018-06-26)
*
* @desc Returns a proper instance of the “Response” class recommended to be used as response to an HTTP request.
*
Expand All @@ -2223,17 +2259,20 @@ public static function sendMail($params){
public static function getResponse($version = '0.2'){
$responseClass = \DDTools\Response::includeResponseByVersion($version);

$output = new $responseClass;
$result = new $responseClass;

return $output;
return $result;
}
}

if(isset($modx)){
ddTools::$modx = $modx;

//Решение спорное, но делать Синглтон очень не хотелось
foreach (ddTools::$tables as $key => $val){
foreach (
ddTools::$tables as
$key => $val
){
ddTools::$tables[$key] = $modx->getFullTableName($key);
}

Expand Down

0 comments on commit 60a1f48

Please sign in to comment.