move several helper methods to the new structure
This commit is contained in:
parent
baa58935a5
commit
ff71571e46
3 changed files with 101 additions and 22 deletions
|
@ -226,19 +226,7 @@ class RCrmActions
|
|||
*/
|
||||
public static function clearArr($arr)
|
||||
{
|
||||
if (is_array($arr) === false) {
|
||||
return $arr;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
foreach ($arr as $index => $node ) {
|
||||
$result[ $index ] = is_array($node) === true ? self::clearArr($node) : trim($node);
|
||||
if ($result[ $index ] == '' || $result[ $index ] === null || count($result[ $index ]) < 1) {
|
||||
unset($result[ $index ]);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
return \Intaro\RetailCrm\Component\Utils::clearArray($arr);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -246,13 +234,10 @@ class RCrmActions
|
|||
* @param array|bool|\SplFixedArray|string $str in SITE_CHARSET
|
||||
*
|
||||
* @return array|bool|\SplFixedArray|string $str in utf-8
|
||||
* @global $APPLICATION
|
||||
*/
|
||||
public static function toJSON($str)
|
||||
{
|
||||
global $APPLICATION;
|
||||
|
||||
return $APPLICATION->ConvertCharset($str, SITE_CHARSET, 'utf-8');
|
||||
return \Intaro\RetailCrm\Component\Utils::toUTF8($str);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -260,13 +245,10 @@ class RCrmActions
|
|||
* @param string|array|\SplFixedArray $str in utf-8
|
||||
*
|
||||
* @return array|bool|\SplFixedArray|string $str in SITE_CHARSET
|
||||
* @global $APPLICATION
|
||||
*/
|
||||
public static function fromJSON($str)
|
||||
{
|
||||
global $APPLICATION;
|
||||
|
||||
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
|
||||
return \Intaro\RetailCrm\Component\Utils::fromUTF8($str);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
96
intaro.retailcrm/lib/component/utils.php
Normal file
96
intaro.retailcrm/lib/component/utils.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* @category Integration
|
||||
* @package Intaro\RetailCrm\Component
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license MIT
|
||||
* @link http://retailcrm.ru
|
||||
* @see http://retailcrm.ru/docs
|
||||
*/
|
||||
namespace Intaro\RetailCrm\Component;
|
||||
|
||||
use Bitrix\Main\Text\Encoding;
|
||||
|
||||
/**
|
||||
* Class Utils
|
||||
*
|
||||
* @package Intaro\RetailCrm\Component
|
||||
*/
|
||||
class Utils
|
||||
{
|
||||
/**
|
||||
* Removes all empty fields from arrays, works for nested arrays
|
||||
*
|
||||
* @param array $arr
|
||||
* @return array
|
||||
*/
|
||||
public static function clearArray($arr): array
|
||||
{
|
||||
if (is_array($arr) === false) {
|
||||
return $arr;
|
||||
}
|
||||
|
||||
$result = array();
|
||||
|
||||
foreach ($arr as $index => $node) {
|
||||
$result[$index] = is_array($node) === true ? self::clearArray($node) : trim($node);
|
||||
|
||||
if ($result[$index] == '' || $result[$index] === null || count($result[$index]) < 1) {
|
||||
unset($result[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array|bool|\SplFixedArray|string $string in SITE_CHARSET
|
||||
*
|
||||
* @return array|bool|\SplFixedArray|string $str in utf-8
|
||||
*/
|
||||
public static function toUTF8($string)
|
||||
{
|
||||
if (!defined('SITE_CHARSET')) {
|
||||
throw new \RuntimeException('SITE_CHARSET must be defined.');
|
||||
}
|
||||
|
||||
return static::convertCharset($string, SITE_CHARSET, 'utf-8');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string|array|\SplFixedArray $string in utf-8
|
||||
*
|
||||
* @return array|bool|\SplFixedArray|string $str in SITE_CHARSET
|
||||
*/
|
||||
public static function fromUTF8($string)
|
||||
{
|
||||
if (!defined('SITE_CHARSET')) {
|
||||
throw new \RuntimeException('SITE_CHARSET must be defined.');
|
||||
}
|
||||
|
||||
return static::convertCharset($string, 'utf-8', SITE_CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @param string $inCharset
|
||||
* @param string $outCharset
|
||||
*
|
||||
* @return array|bool|\SplFixedArray|string
|
||||
*/
|
||||
protected static function convertCharset(string $string, string $inCharset, string $outCharset)
|
||||
{
|
||||
$error = '';
|
||||
$result = Encoding::convertEncoding($string, $inCharset, $outCharset, $error);
|
||||
|
||||
if (!$result && !empty($error)) {
|
||||
throw new \RuntimeException($error);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ namespace Intaro\RetailCrm\Model\Api;
|
|||
|
||||
use Intaro\RetailCrm\Component\Json\Mapping\PostDeserialize;
|
||||
use Intaro\RetailCrm\Component\Json\Mapping\PostSerialize;
|
||||
use Intaro\RetailCrm\Component\Utils;
|
||||
|
||||
/**
|
||||
* Class AbstractApiModel
|
||||
|
@ -52,6 +53,6 @@ class AbstractApiModel implements ApiModelInterface
|
|||
*/
|
||||
public function postSerialize(array $fields): array
|
||||
{
|
||||
return \RCrmActions::clearArr($fields);
|
||||
return Utils::clearArray($fields);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue