life-code/primeira-mao

View on GitHub
src/Support/helpers.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

/**
 * PagSeguro SDK
 * 
 * @type        library
 * @version     0.0.7
 * @package     life-code/pagseguro-sdk
 * @copyright   Copyright (c) 2018 Vinicius Pugliesi (http://www.viniciuspugliesi.com)
 * @author      Vinicius Pugliesi <vinicius_pugliesi@outlook.com>
 * @license     MIT
 */
if (! function_exists('env')) {
    /**
     * Gets the value of an environment variable.
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    function env($key, $default = null)
    {
        $value = getenv($key);
        
        if ($value === false) {
            return $default;
        }
        
        switch (strtolower($value)) {
            case 'true':
            case '(true)':
                return true;
            case 'false':
            case '(false)':
                return false;
            case 'empty':
            case '(empty)':
                return '';
            case 'null':
            case '(null)':
                return null;
        }
        
        if (strlen($value) > 1 && substr($value, 0, 1) === '"' && substr($value, -1) === '"') {
            return substr($value, 1, -1);
        }
        
        return $value;
    }
}

if (! function_exists('toArrayRecursive')) {
    /**
     * Convert object in array
     *
     * @param mixed $data
     * @return array
     */
    function toArrayRecursive($data) : array
    {
        return array_map(function ($value) {
            return is_object($value) ? toArrayRecursive($value) : $value;
        }, (array) $data);
    }
}