Overview

Namespaces

  • MvcCore
    • Ext
      • Auth
        • Virtual
      • Debug
        • Tracy
      • Form
        • Core
        • Validators
      • Request
      • Router
        • Lang
      • View
        • Helpers
  • None

Classes

  • MvcCore
  • MvcCore\Config
  • MvcCore\Controller
  • MvcCore\Debug
  • MvcCore\Ext\Auth
  • MvcCore\Ext\Auth\Controller
  • MvcCore\Ext\Auth\SignInForm
  • MvcCore\Ext\Auth\SignOutForm
  • MvcCore\Ext\Auth\User
  • MvcCore\Ext\Auth\Virtual\Controller
  • MvcCore\Ext\Auth\Virtual\Form
  • MvcCore\Ext\Auth\Virtual\User
  • MvcCore\Ext\Debug\Tracy
  • MvcCore\Ext\Debug\Tracy\AuthPanel
  • MvcCore\Ext\Debug\Tracy\IncludePanel
  • MvcCore\Ext\Debug\Tracy\MvcCorePanel
  • MvcCore\Ext\Debug\Tracy\RoutingPanel
  • MvcCore\Ext\Debug\Tracy\SessionPanel
  • MvcCore\Ext\Form
  • MvcCore\Ext\Form\Button
  • MvcCore\Ext\Form\Checkbox
  • MvcCore\Ext\Form\CheckboxGroup
  • MvcCore\Ext\Form\Core\Base
  • MvcCore\Ext\Form\Core\Configuration
  • MvcCore\Ext\Form\Core\Field
  • MvcCore\Ext\Form\Core\FieldGroup
  • MvcCore\Ext\Form\Core\Helpers
  • MvcCore\Ext\Form\Core\Validator
  • MvcCore\Ext\Form\Core\View
  • MvcCore\Ext\Form\CountrySelect
  • MvcCore\Ext\Form\Date
  • MvcCore\Ext\Form\DateTime
  • MvcCore\Ext\Form\Email
  • MvcCore\Ext\Form\Hidden
  • MvcCore\Ext\Form\NoType
  • MvcCore\Ext\Form\Number
  • MvcCore\Ext\Form\Password
  • MvcCore\Ext\Form\RadioGroup
  • MvcCore\Ext\Form\Range
  • MvcCore\Ext\Form\ResetButton
  • MvcCore\Ext\Form\ResetInput
  • MvcCore\Ext\Form\Select
  • MvcCore\Ext\Form\SubmitButton
  • MvcCore\Ext\Form\SubmitInput
  • MvcCore\Ext\Form\Text
  • MvcCore\Ext\Form\Textarea
  • MvcCore\Ext\Form\Time
  • MvcCore\Ext\Form\Validators\CompanyId
  • MvcCore\Ext\Form\Validators\CompanyVatId
  • MvcCore\Ext\Form\Validators\Date
  • MvcCore\Ext\Form\Validators\Email
  • MvcCore\Ext\Form\Validators\FloatVal
  • MvcCore\Ext\Form\Validators\Integer
  • MvcCore\Ext\Form\Validators\Maxlength
  • MvcCore\Ext\Form\Validators\MaxSelectedOptions
  • MvcCore\Ext\Form\Validators\MinSelectedOptions
  • MvcCore\Ext\Form\Validators\NumberField
  • MvcCore\Ext\Form\Validators\Pattern
  • MvcCore\Ext\Form\Validators\Phone
  • MvcCore\Ext\Form\Validators\RangeField
  • MvcCore\Ext\Form\Validators\SafeString
  • MvcCore\Ext\Form\Validators\Time
  • MvcCore\Ext\Form\Validators\Url
  • MvcCore\Ext\Form\Validators\ValueInOptions
  • MvcCore\Ext\Form\Validators\ZipCode
  • MvcCore\Ext\Request\ApacheDpi
  • MvcCore\Ext\Request\Cli
  • MvcCore\Ext\Router\Lang
  • MvcCore\Ext\Router\Lang\Route
  • MvcCore\Ext\Router\Media
  • MvcCore\Ext\Router\MediaSiteKey
  • MvcCore\Ext\View\Helpers\Assets
  • MvcCore\Ext\View\Helpers\Css
  • MvcCore\Ext\View\Helpers\Js
  • MvcCore\Ext\View\Helpers\LineBreaks
  • MvcCore\Model
  • MvcCore\Request
  • MvcCore\Response
  • MvcCore\Route
  • MvcCore\Router
  • MvcCore\Session
  • MvcCore\Tool
  • MvcCore\View

Exceptions

  • MvcCore\Ext\Form\Core\Exception
  • Overview
  • Namespace
  • Class
  • Tree
  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;
    }
}
MvcCore API documentation generated by ApiGen