Skip to content

Instantly share code, notes, and snippets.

@crossmaya
Last active May 8, 2021 01:59
Show Gist options
  • Save crossmaya/5319554 to your computer and use it in GitHub Desktop.
Save crossmaya/5319554 to your computer and use it in GitHub Desktop.
php basic helps
<?php declare(strict_types=1);
use GuzzleHttp\Client;
use Swoft\Stdlib\Helper\ArrayHelper;
use GuzzleHttp\Exception\RequestException;
/**
* 数据格式化
*
* @access public
* @param mixed $args 待处理数据
* @return mixed
*/
if ( ! function_exists('formatData')) {
function formatData($args)
{
$data = [];
foreach ($args as $key => $val) {
$key = lineToHump((string)$key);
if (is_array($val) || is_object($val)) {
$val = formatData($val);
}
$data[$key] = $val;
}
return $data;
}
}
/**
* 驼峰转下划线
*
* @access public
* @param string $str 字符串
* @return string
*/
if ( ! function_exists('lineToHump')) {
function humpToLine(string $str)
{
return preg_replace_callback('/([A-Z])/', function ($match) {
return '_'.lcfirst($match[0]);
}, $str);
}
}
/**
* 下划线转驼峰
*
* @access public
* @param string $str 字符串
* @return string
*/
if ( ! function_exists('lineToHump')) {
function lineToHump(string $str)
{
return preg_replace_callback('/(_[a-z])/', function ($match) {
return ucfirst(trim($match[0], '_'));
}, $str);
}
}
/**
* 随机字符串
*
* @access public
* @param integer $len 字符长度
* @param boolean $int 纯数字
* @return string
*/
if ( ! function_exists('random')) {
function random($len = 10, $int = false)
{
$str = '';
$len = (is_numeric($len)) ? $len : 10;
$seed = base_convert(md5(microtime(true).uniqid((string)mt_rand(), true)), 16, ( ! empty($int)) ? 10 : 35);
$seed = ( ! empty($int)) ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
if ( ! empty($int)) {
$str = '';
} else {
$str = chr(mt_rand(1, 26) + mt_rand(0, 1) * 32 + 64);
--$len;
}
$max = strlen($seed) - 1;
for ($i = 0; $i < $len; ++$i) {
$str .= $seed{mt_rand(0, $max)};
}
return $str;
}
}
/**
* 发送请求
*
* @access public
* @param string $url URL
* @param array $args 提交参数
* @param array $headers HEAD信息
* @param string $method 提交方法
* @return array
* @throws
*/
if ( ! function_exists('send')) {
function send(string $url, array $args = [], array $headers = [], string $method = 'POST')
{
$status = ['code' => 0, 'data' => [], 'message' => ''];
try {
$options = ( ! empty($args)) ? ['form_params' => $args] : [];
if ( ! empty($headers)) {
$options['headers'] = $headers;
}
$response = (new Client())->request($method, $url, $options);
$content = $response->getBody()->getContents();
if (empty($content)) {
throw new \Exception('没有返回相关数据!');
}
$status = ['code' => 200, 'data' => $content, 'message' => ''];
} catch (RequestException $e) {
$status['message'] = $e->getMessage();
}
return $status;
}
}
/**
* 格式化JSON
*
* @access public
* @param mixed $data 待处理数据
* @return mixed
*/
if ( ! function_exists('withJson')) {
function withJson(array $data)
{
if (isset($data['data'])) {
if (empty($data['data']) && is_array($data['data'])) {
$data['data'] = new \stdClass();
}
}
return $data;
}
}
<?php
/** common libs **/
//应用版本比较
function updateAppVersion($appVersion1, $appVersion2)
{
$releaseVersion = explode(".",$appVersion1);
$deviceVersion = explode(".",$appVersion2);
$len = count($deviceVersion);
if(count($releaseVersion)>count($deviceVersion))
{
$len = count($releaseVersion);
}
for($i = 0;$i<$len;$i++){
echo "[i=".$i."][r=".$releaseVersion[$i]."][d=".$deviceVersion[$i]."]";
if(!isset($releaseVersion[$i])){
return false;
}
else if(!isset($deviceVersion[$i])){
return true;
}
else if($releaseVersion[$i]>$deviceVersion[$i]){
return true;
}
else if($releaseVersion[$i]<$deviceVersion[$i]){
return false;
}
}
return false;
}
/** Array helper **/
function array_except($array, $key)
{
return array_diff_key($array, array_flip((array) $key));
}
if (! function_exists('objectToArray') )
{
function objectToArray($d)
{
if( is_object($d) )
{
$d = get_object_vars($d);
}
if( is_array($d) )
{
return array_map(__FUNCTION__, $d);
} else
{
return $d;
}
}
}
/* String Helper */
function start_with($haystack, $needle)
{
return strpos($haystack, $needle) === 0;
}
function ends_with($haystack, $needle)
{
return $needle == substr($haystack, strlen($haystack) - strlen($needle));
}
/* Common Helper */
function with($object)
{
return $object;
}
function value($value)
{
return $object instanceof Closure ? $value() : $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment