PHP code example of hehex / hehep-hformat
1. Go to this page and download the library: Download hehex/hehep-hformat library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hehex / hehep-hformat example snippets
$formatConf = [
// 定义格式化集合器
'formatCollectors'=>[
'hehe\core\hformat\formators\CommonFormator'
],
];
// 数据源定义
class UserData
{
public static function showStatus():array
{
return [
['id'=>1,'name'=>'正常'],
['id'=>2,'name'=>'禁用'],
['id'=>3,'name'=>'注销'],
];
}
public function totalAdminNewsNum(array $ids)
{
return [
['id'=>1,'hit_num'=>10,'buy_num'=>20],
['id'=>2,'hit_num'=>10,'buy_num'=>25],
['id'=>3,'hit_num'=>11,'buy_num'=>21],
];
}
public function getRoles(array $ids)
{
return [
['id'=>1,'roleName'=>'超级管理员'],
['id'=>2,'roleName'=>'管理员'],
['id'=>3,'roleName'=>'普通用户'],
];
}
}
use hehe\core\hformat\FormatManager;
use UserData;
// 数据定义,一般从数据库获取
$users = [
['id'=>1,'name'=>'hehe1','status'=>1,'ctime'=>'2018-01-01 12:00:00','roleId'=>1,'headPortrait'=>'a/b/c1.jpg'],
['id'=>2,'name'=>'hehe2','status'=>2,'ctime'=>'2018-01-01 12:00:00','roleId'=>2,'headPortrait'=>'a/b/c2.jpg'],
];
// 创建字典数据来源对象
$userData = new UserData();
// 创建格式化管理器
$hformat = new FormatManager();
// 格式化"$users"数据
$data = $hformat->doFormat($users,[
// 状态数值转状态文本
['status',[['dict','data'=> [[UserData::class,'showStatus']] ]], 'alias'=>':_text' ],
// 日期转换
['ctime',[['date','params'=>['Y年m月d日 H:i'] ]] ],
// 头像短地址转长地址(http)
['headPortrait',[['trim'],['res']], 'alias'=>':_url' ],
// 统计访问量,统计购买量
['hit_num',[['dict','name'=>'hit_num','data'=>[[$userData, 'totalAdminNewsNum']]]],'dataid'=>'id','alias'=>'hit_num'],
['buy_num',[['dict','name'=>'buy_num','data'=>[[$userData, 'totalAdminNewsNum']]]],'dataid'=>'id','alias'=>'buy_num'],
// 角色ID值转角色名称
['roleId',[['dict','name'=>'roleName','data'=>[[$userData, 'getRoles']]]], 'alias'=>'roleName_text']
]);
// 输出数据
$data = [
['id'=>1,'name'=>'hehe1','status'=>1,'ctime'=>'2018年01月01日 12:00','roleId'=>1,'headPortrait'=>'a/b/c1.jpg',
'status_text'=>'正常','roleName_text'=>'超级管理员','headPortrait_url'=>'http://www.hehex.com/a/b/c1.jpg','hit_num'=>10,'buy_num'=>20],
['id'=>2,'name'=>'hehe2','status'=>1,'ctime'=>'2018年01月01日 12:00','roleId'=>2,'headPortrait'=>'a/b/c1.jpg',
'status_text'=>'禁用','roleName_text'=>'管理员','headPortrait_url'=>'http://www.hehex.com/a/b/c2.jpg','hit_num'=>10,'buy_num'=>21],
];
class UserFormat
{
public static function defaultFormat()
{
// 字典数据来源
$user = new UserData();
return [
// 状态值转换
['status',[ ['dict','data'=>[[UserData::class,'showStatus']] ]],'isdef'=>true,'alias'=>':Text'],
// 头像图片http 转换
['headPortrait',[ ['res'] ],'isdef'=>true,'alias'=>'headPortraitUrl'],
// 统计访问量,统计购买量
['hit_num',[['dict','name'=>'hit_num','data'=>[[$user, 'totalAdminNewsNum']]]],'dataid'=>'id'],
['buy_num',[['dict','name'=>'buy_num','data'=>[[$user, 'totalAdminNewsNum']]]],'dataid'=>'id'],
// 角色数值转角色名称
['roleId',[ ['dict','name'=>'roleName','data'=>[[$user, 'getRoles']]] ],'isdef'=>true,'alias'=>'roleName_text']
];
}
}
$users = [
['id'=>1,'name'=>'hehe1','status'=>1,'ctime'=>'2018-01-01 12:00:00','roleId'=>1,'headPortrait'=>'/a/b/c1.jpg'],
['id'=>2,'name'=>'hehe2','status'=>2,'ctime'=>'2018-01-01 12:00:00','roleId'=>2,'headPortrait'=>'/a/b/c2.jpg'],
];
$hformat = new FormatManager();
$data = $hformat->doCustomFormat($users,[UserFormat::defaultFormat(),['hit_num']]);
// 输出数据
$data = [
['id'=>1,'name'=>'hehe1','status'=>1,'ctime'=>'2018年01月01日 12:00','roleId'=>1,'headPortrait'=>'a/b/c1.jpg',
'status_text'=>'正常','roleName_text'=>'超级管理员','headPortrait_url'=>'http://www.hehex.com/a/b/c1.jpg','hit_num'=>10],
['id'=>2,'name'=>'hehe2','status'=>1,'ctime'=>'2018年01月01日 12:00','roleId'=>2,'headPortrait'=>'a/b/c1.jpg',
'status_text'=>'禁用','roleName_text'=>'管理员','headPortrait_url'=>'http://www.hehex.com/a/b/c2.jpg','hit_num'=>10],
];
use hehe\core\hformat\FormatManager;
use hehe\core\hformat\Format;
// 创建格式化管理器
$hformat = new FormatManager();
// 注册格式器
FormatManager::addFormator('date',DateFormator::class);
Format::addFormator('date',DateFormator::class);
Format::addFormators(['date'=>DateFormator::class]);
// 注册预定义格式化集合器
FormatManager::addFormatCollector(CommonFormator::class);
Format::addFormatCollector(CommonFormator::class);
Format::addFormatCollectors([CommonFormator::class]);
// 执行格式化规则
$hformat->doFormat([['ctime'=>'2018-01-01 12:00:00']],[
['ctime',[ ['date','params'=>['Y年m月d日 H时i分'] ] ] ],
]);
// 执行预定义格式化规则
$hformat->doCustomFormat([['ctime'=>'2018-01-01 12:00:00']],[[
['ctime',[ ['date','params'=>['Y年m月d日 H时i分'] ] ] ],
],['ctime']]);
namespace hehe\core\hformat\formators;
use hehe\core\hformat\base\Formator;
// 日期格式器
class DateFormator extends Formator
{
protected $format = 'Y-m-d';
// 格式化数据入口
public function getValue(...$params)
{
return $this->formatDate(...$params);
}
protected function formatDate(string $value,string $format = '')
{
if ($format === '') {
$format = $this->format;
}
return date($format,strtotime($value));
}
}
use hehe\core\hformat\FormatManager;
FormatManager::addFormator('date',DateFormator::class);
FormatManager::addFormators(['date'=>DateFormator::class]);
class CommonFormator
{
// 方法名后缀为"Formator"作为格式器,默认别名为:trim
public static function trimFormator($value)
{
return trim($value);
}
// 方法名后缀为"Formator"作为格式器,默认别名为:toArr
public static function toArrFormator($value)
{
if (!empty($value) && is_string($value)) {
$value = explode(',',$value);
}
return $value;
}
// 方法名后缀为"Formator"作为格式器,默认别名为:jsonDecode
public static function jsonDecodeFormator($value)
{
return json_decode($value,true);
}
// 方法名后缀为"Formator"作为格式器,默认别名为:jsonEncode
public static function jsonEncodeFormator($value)
{
return json_encode($value,true);
}
}
use hehe\core\hformat\FormatManager;
// 注册类格式器
FormatManager::addFormatCollector(CommonFormator::class);
FormatManager::addFormatCollectors([CommonFormator::class]);
class UserData
{
public static function showStatus():array
{
return [
['id'=>1,'name'=>'正常'],
['id'=>2,'name'=>'禁用'],
['id'=>3,'name'=>'注销'],
];
}
public function totalAdminNewsNum(array $ids)
{
return [
['id'=>1,'hit_num'=>10,'buy_num'=>20],
['id'=>2,'hit_num'=>10,'buy_num'=>25],
['id'=>3,'hit_num'=>11,'buy_num'=>21],
];
}
/**
* @param array $ids 数据id集合
* @param bool $effective 是否读取有效角色
* @return array[]
*/
public function getRoles(array $ids,bool $effective = false)
{
return [
['id'=>1,'roleName'=>'超级管理员'],
['id'=>2,'roleName'=>'管理员'],
['id'=>3,'roleName'=>'普通用户'],
];
}
}
use hehe\core\hformat\FormatManager;
use UserData;
// 数据定义,一般从数据库获取
$users = [
['id'=>1,'name'=>'hehe1','status'=>1,'roleId'=>1],
['id'=>2,'name'=>'hehe2','status'=>2,'roleId'=>2],
];
// 创建字典数据来源对象
$userData = new UserData();
// 创建格式化管理器
$hformat = new FormatManager();
// 格式化"$users"数据
$data = $hformat->doFormat($users,[
// 状态数值转状态文本
['status',[['dict','data'=> [[UserData::class,'showStatus']] ]], 'alias'=>':_text' ],
// 统计访问量,统计购买量
['hit_num',[['dict','name'=>'hit_num','data'=>[[$userData, 'totalAdminNewsNum']]]],'dataid'=>'id','alias'=>'hit_num'],
['buy_num',[['dict','name'=>'buy_num','data'=>[[$userData, 'totalAdminNewsNum']]]],'dataid'=>'id','alias'=>'buy_num'],
// 角色ID值转角色名称
['roleId',[['dict','name'=>'roleName','data'=>[[$userData, 'getRoles'],true]]], 'alias'=>'roleName_text']
]);
// 输出数据
$data = [
['id'=>1,'name'=>'hehe1','status'=>1,'roleId'=>1,
'status_text'=>'正常','roleName_text'=>'超级管理员','hit_num'=>10,'buy_num'=>20],
['id'=>2,'name'=>'hehe2','status'=>1,
'status_text'=>'禁用','roleName_text'=>'管理员','hit_num'=>10,'buy_num'=>21],
];
use hehe\core\hformat\FormatManager;
$hformat = new FormatManager();
$str_json = $hformat->jsonEncode(["id"=>1,'name'=>'hehe']);
$arr = $hformat->jsonDecode($str_json);
$value = $hformat->trim(' hehex ');
$date = $hformat->date('2000-01-01','Y年m月d日');
// 字典数据
$data = [
['id'=>1,'name'=>'hehe1'],
['id'=>2,'name'=>'hehe2'],
['id'=>3,'name'=>'hehe3'],
];
$name = $hformat->createFormator('dict')->setData($data)->getValue(1);
// $name = 'hehe1'
$name = $this->hformat->dict()->setData($data)->getValue(2);
// $name = 'hehe2'
use hehe\core\hformat\annotation\AnnFormator;
use \hehe\core\hformat\base\Formator;
/**
* @AnnFormator("uri")
*/
class UrlFormator extends Formator
{
public function getValue(...$params)
{
return $this->formatUri(...$params);
}
protected function formatUri(string $url)
{
// 生成URL地址
return $url;
}
}
use hehe\core\hformat\annotation\AnnFormator;
/**
* @AnnFormator()
*/
class CommonFormator
{
// 方法名后缀为"Formator"作为格式器,默认别名为:toArr
public static function toArrFormator($value)
{
if (!empty($value) && is_string($value)) {
$value = explode(',',$value);
}
return $value;
}
}
use hehe\core\hformat\annotation\AnnFormator;
class CommonFormator
{
/**
* @AnnFormator("toArr")
*/
public static function toArrFormator($value)
{
if (!empty($value) && is_string($value)) {
$value = explode(',',$value);
}
return $value;
}
/**
* @AnnFormator()
*/
public static function uriFormator(string $url)
{
// URL生成
return $url;
}
}