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
$config = [
// 自定义格式器集合
'formatCollectors'=>[
// 加载组件默认格式器集合
'hehe\core\hformat\formators\CommonFormator',
// 加载框架默认格式器集合
'hehe\extend\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\Formation;
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 Formation();
// 格式化"$users"数据
$data = $hformat->doFormat($users,[
// 状态数值转状态文本
['status',[['dict','data'=> [UserData::class,'showStatus'],'args'=>[] ]], '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'],'args'=>[] ]],'dataid'=>'id','alias'=>'hit_num'],
['buy_num',[['dict','name'=>'buy_num','data'=>[$userData, 'totalAdminNewsNum'],'args'=>[] ]],'dataid'=>'id','alias'=>'buy_num'],
// 角色ID值转角色名称
['roleId',[['dict','name'=>'roleName','data'=>[$userData, 'getRoles'],'args'=>[] ]], '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],
];
use hehe\core\hformat\Formation;
$hformat = new Formation();
$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'
namespace hehe\core\hformat\formators;
use hehe\core\hformat\base\Formator;
// 日期格式器
class DateFormator extends Formator
{
// 安装格式器
public static function install()
{
return [
'date'=>static::class
// 'date'=>['class'=>static::class,'属性1'=>'','属性2'=>'',]
];
}
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));
}
}
class CommonFormator
{
// 安装格式器
public static function install()
{
return [
'ltrim'=>static::class . '@@ltrim',
//'ltrim'=>[static::class,'ltrim']
];
}
public static function ltrim($value)
{
return ltrim($value);
}
// 方法名后缀为"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\Formation;
// 注册格式器
Formation::addFormator(DateFormator::class,'date');
Formation::addFormatCollectors(DateFormator::class);
Formation::install(DateFormator::class);
// 注册格式化集合器
Formation::addFormatCollectors(CommonFormator::class);
Formation::install(CommonFormator::class);
class UserFormat
{
public static function defaultFormat()
{
// 字典数据来源
$user = new UserData();
return [
// 状态值转换
['status',[ ['dict','data'=>[UserData::class,'showStatus'] ]],'isdef'=>true,'alias'=>':Text'],
['ctime',[['date','params'=>['Y年m月d日 H:i']]],'isdef'=>true ],
// 头像图片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']
];
}
}
use hehe\core\hformat\Formation;
$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 Formation();
$data = $hformat->doFormat($users,UserFormat::defaultFormat(),['hit_num']);
// $data = $hformat->doFormat($users,UserFormat::defaultFormat(),['hit_num','ctime'=>['alias'=>':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],
['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],
];
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\Formation;
use UserData;
// 数据定义,一般从数据库获取
$users = [
['id'=>1,'name'=>'hehe1','status'=>1,'roleId'=>1],
['id'=>2,'name'=>'hehe2','status'=>2,'roleId'=>2],
];
// 创建字典数据来源对象
$userData = new UserData();
// 创建格式化管理器
$hformat = new Formation();
// 格式化"$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'],'args'=>[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\Formation;
Formation::install(DateFormator::class);
use hehe\core\hformat\Formation;
Formation::install(CommonFormator::class);
use hehe\core\hformat\Formation;
Formation::install(CommonFormator::class,true);
Formation::install(DateFormator::class,true);
use hehe\core\hformat\Formation;
Formation::install(CommonFormator::class,true,true);
Formation::install(DateFormator::class,true,true);
use hehe\core\hformat\Formation;
Formation::install(DateFormator::class,true,false,'Formator');
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;
}
}