定制 ems/xtype 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ems/xtype

最新稳定版本:v0.3.1

Composer 安装命令:

composer require ems/xtype

包简介

Semantic types for PHP

README 文档

README

Semantic atomic types for PHP

Usage

XType classifies datatypes. They are mainly used to scaffold forms and get centralized string formatting.

Instead of saying:

echo number_format($myObject->price,2,'.');

with xtype you can write:

class MyObject{

    public $price;
    
    public function getType(){
        return NamedFieldType::create()
           ->set('price', UnitType::create()->setUnit('$')->setDecimals(2));
    }
}

// Then in you're view:

$myObject = new MyObject();
echo $myObject->getType('price')->valueToString($myObject->price);

Where this seems to be very clumsy it offers its powers in big applications or big data centric applications. Imagine you have a class customer:

   class Customer{
   
       public $id;
       public $doubts;
       public $lastLogin;
       public $taxes;
       
       public function getType(){
           $type = NamedField::create();
           $type->set('id', NumberType::create()->setNativeType('int'))
           $type->set('doubts', CurrencyType::create()->setCurrency(''));
           $type->set('lastLogin', TemporalType::create();
           $type->set('taxes', PercentageType::create());
           return $type;
       }
   }
   
   $c = new Customer();
   
   foreach($c->getType() as $key=>$type){
       echo "\n$key=>".$type->valueToString($c->{$key});
   }

And in every part of youre application you can just forget any string formatting. But string formatting isn't the only usage of xtype. (i know about SOLID...)

Its on the other hand extremly useful to describe types. So you can build a database scheme of xtypes.

Another possibility is to scaffold forms with xtype.

Factory

To simply create an XType you can use XType\TemplateFactory::create($native) which creates a AbstractType by a native php variable.

Caution

This lib is in beta quality. The classes don't do much so it's not that dangerous to use em. So no docs or at this time

统计信息

  • 总下载量: 6.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-05-30