1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom FlĂdr (https://github.com/mvccore/mvccore)
* @license https://mvccore.github.io/docs/mvccore/4.0.0/LICENCE.md
*/
namespace MvcCore;
/**
* Core tools:
* - translating cases (with folders support):
* - dashed-case to PascalCase
* - PascalCase to dashed-case
* - unserscore_case to PascalCase
* - PascalCase to unserscore_case
* - JSON safe encode/decode
*/
class Tool
{
/**
* Convert all string 'from' => 'to':
* - 'MyCutomValue' => 'my-custom-value'
* - 'MyCutom/Value/InsideFolder' => 'my-custom/value/inside-folder'
* @param string $pascalCase
* @return string
*/
public static function GetDashedFromPascalCase ($pascalCase = '') {
return strtolower(preg_replace("#([a-z])([A-Z])#", "$1-$2", lcfirst($pascalCase)));
}
/**
* Convert all string 'from' => 'to':
* - 'my-custom-value' => 'MyCutomValue'
* - 'my-custom/value/inside-folder'=> 'MyCutom/Value/InsideFolder'
* @param string $dashed
* @return string
*/
public static function GetPascalCaseFromDashed ($dashed = '') {
$a = explode('/', $dashed);
foreach ($a as & $b) $b = ucfirst(str_replace('-', '', ucwords($b, '-')));
return implode('/', $a);
}
/**
* Convert all string 'from' => 'to':
* - 'MyCutomValue' => 'my_custom_value'
* - 'MyCutom/Value/InsideFolder' => 'my_custom/value/inside_folder'
* @param string $pascalCase
* @return string
*/
public static function GetUnderscoredFromPascalCase ($pascalCase = '') {
return strtolower(preg_replace("#([a-z])([A-Z])#", "$1_$2", lcfirst($pascalCase)));
}
/**
* Convert all string 'from' => 'to':
* - 'my_custom_value' => 'MyCutomValue'
* - 'my_custom/value/inside_folder'=> 'MyCutom/Value/InsideFolder'
* @param string $underscored
* @return string
*/
public static function GetPascalCaseFromUnderscored ($underscored = '') {
$a = explode('/', $underscored);
foreach ($a as & $b) $b = ucfirst(str_replace('_', '', ucwords($b, '_')));
return implode('/', $a);
}
/**
* Safely encode json string from php value.
* @param mixed $data
* @throws \Exception
* @return string
*/
public static function EncodeJson ($data) {
$flags = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP |
(defined('JSON_UNESCAPED_SLASHES') ? JSON_UNESCAPED_SLASHES : 0) |
(defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : 0) |
(defined('JSON_PRESERVE_ZERO_FRACTION') ? JSON_PRESERVE_ZERO_FRACTION : 0);
$json = json_encode($data, $flags);
if ($errorCode = json_last_error()) {
throw new \Exception(json_last_error_msg(), $errorCode);
}
if (PHP_VERSION_ID < 70100) {
$json = strtr($json, array(
"\xe2\x80\xa8" => '\u2028',
"\xe2\x80\xa9" => '\u2029',
));
}
return $json;
}
/**
* Safely decode json string into php stdClass/array.
* Result has always keys:
* - 'success' - decoding boolean success
* - 'data' - decoded json data as stdClass/array
* - 'errorData'- array with possible decoding error message and error code
* @param string $jsonStr
* @return object
*/
public static function DecodeJson (& $jsonStr) {
$result = (object) array(
'success' => TRUE,
'data' => null,
'errorData' => array(),
);
$jsonData = @json_decode($jsonStr);
$errorCode = json_last_error();
if ($errorCode == JSON_ERROR_NONE) {
$result->data = $jsonData;
} else {
$result->success = FALSE;
$result->errorData = array(json_last_error_msg(), $errorCode);
}
return $result;
}
}