定制 dozer111/type_checker 二次开发

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

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

dozer111/type_checker

最新稳定版本:2.1

Composer 安装命令:

composer require dozer111/type_checker

包简介

Check your variable types using using less code

README 文档

README

composer require dozer111/type_checker

Was created, for better and faster checking $yourValue data types in imperativeStyle

Main idea , that there is no need to write and read after looong checks(which takes our time and energy), when we can change it to some simple structure, which would do code shorter and readPrettier

Lib has 2 main ways:

  • check
  • hardCheck/guard => check, and if false -> throw exception
TypeChecker::hardCheck($value,[__INTEGER__,__STRING__]);
TypeChecker::hardCheckInt($value,[__INTEGER__,__STRING__]);
TypeChecker::hardCheckString($value,[__INTEGER__,__STRING__]);
// or same, but with synonyms
TypeChecker::guard($value,[__INTEGER__,__STRING__]);
TypeChecker::guardInt($value,[__INTEGER__,__STRING__]);
TypeChecker::guardString($value,[__INTEGER__,__STRING__]);

Example1 => common usage

//before
$value = '<someYourValue>';
if(is_int($value) || is_string($value))
{
    doSmth();
}


// now
$valueHasCorrectType = TypeChecker::check($value,[__INTEGER__,__STRING__]);

//========================================================================================================================
//========================================================================================================================

// before
if(is_int($value) || is_string($value))
{
    throw new SomeYourException();
}

// now hardCheck or guard
TypeChecker::hardCheck($value,[__INTEGER__,__STRING__]);
TypeChecker::guard($value,[__INTEGER__,__STRING__]);

Example2 => value + nullable

TypeChecker has own type TYPE_NULL, and you can use this.
Also, you can use nullable argument, instead of adding this to check() method:

// add manually
TypeChecker::check($value,[__INTEGER__,__STRING__,__NULL__]);
// or use nullable mechanism
TypeChecker::check($value,[__INTEGER__,__STRING__],true);

This trick works almost for all methods:

TypeChecker::hardCheckInt($x,true); // null or int
TypeChecker::hardCheckInt($x); // int ONLY!

Example3 => checking Object

  • TypeChecker can check object in 2 ways
    • check, that value is just object type
    • check, that object really instanceOf your needs
// we can check objects in couple of ways:
// 1 => using TypeChecker::check
TypeChecker::check($value,[__OBJECT__]);
TypeChecker::check($value,[YorClassName::class]);

// 2 => TypeChecker::checkObject()/TypeChecker::hardCheckObject()
TypeChecker::checkObject($value); // will check for `object` type
TypeChecker::checkObject($value,YorClassName::class); // will check for `object` && YorClassName types
TypeChecker::checkObject($value,YorClassName::class,true); // will check for (`object` && YorClassName) or null types

Example4 => change default throw exception

just extends class, and rewrite throwHardCheckError()

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2020-05-07