adrenalinkin/enum-mapper
Composer 安装命令:
composer require adrenalinkin/enum-mapper
包简介
Component provides easy way to emulate ENUM behaviour on the PHP layer instead database and convert DB value into human representation and vise versa
README 文档
README
Introduction
Component provides easy way to emulate ENUM behaviour on the PHP layer instead database by constant usage.
Convert database value into human representation and vise versa.
Installation
Open a command console, enter your project directory and execute the following command to download the latest stable version of this component:
composer require adrenalinkin/enum-mapper
This command requires you to have Composer install globally.
Usage examples
Mapper creation
For start create mapper-class and extend him from AbstractEnumMapper. For second and last step - determine list of the constants with specific prefixes:
- DB_ - contains database values.
- HUMAN_ - contains humanized values.
Let's say we need to store user's gender. In that case we need to create mapper-class:
<?php use Linkin\Component\EnumMapper\Mapper\AbstractEnumMapper; class GenderMapper extends AbstractEnumMapper { const DB_UNDEFINED = 0; const DB_MALE = 10; const DB_FEMALE = 20; const HUMAN_UNDEFINED = 'Undefined'; const HUMAN_MALE = 'Male'; const HUMAN_FEMALE = 'Female'; }
Usage
fromDbToHuman
Get humanized value by received database value:
<?php $mapper = new GenderMapper(); $dbGenderValue = GenderMapper::DB_MALE; // 10 $humanValue = $mapper->fromDbToHuman($dbGenderValue);
Variable $humanValue will be contain Male value.
Note: When you will try to get value of the unregistered value will be throws UndefinedMapValueException.
fromHumanToDb
Get database value by received humanized value:
<?php $mapper = new GenderMapper(); $humanGenderValue = GenderMapper::HUMAN_FEMALE; // Female $dbValue = $mapper->fromHumanToDb($humanGenderValue);
Variable $dbValue will be contain 20 value.
Note: When you will try to get value of the unregistered value will be throws UndefinedMapValueException.
getMap
Get full list of the available pairs of the database and humanized values:
<?php $mapper = new GenderMapper(); $map = $mapper->getMap(); // [0 => 'Undefined', 10 => 'Male', 20 => 'Female']
Constant usage
All the time you available to use constant as is:
<?php if (GenderMapper::DB_UNDEFINED === $maleFromForm) { throw new \Exception('Field "Gender" is required in this form'); }
getAllowedDbValues and getAllowedHumanValues
Get list of the all available value for the database values or for the humanized values:
<?php $mapper = new GenderMapper(); $allowedDb = $mapper->getAllowedDbValues(); // [0, 10, 20] $allowedHuman = $mapper->getAllowedHumanValues(); // ['Undefined', 'Male', 'Female'] // Exclude values from result $allowedDb = $mapper->getAllowedDbValues([GenderMapper::DB_UNDEFINED]); // [10, 20] $allowedHuman = $mapper->getAllowedHumanValues([GenderMapper::HUMAN_UNDEFINED]); // ['Male', 'Female']
getRandomDbValue и getRandomHumanValue
Get random database or humanized value:
<?php $mapper = new GenderMapper(); $randomDb = $mapper->getRandomDbValue(); // 0 || 10 || 20 $randomHuman = $mapper->getRandomHumanValue(); // Undefined || Male || Female // Exclude values from result $randomDb = $mapper->getRandomDbValue([GenderMapper::DB_UNDEFINED]); // 10 || 20 $randomHuman = $mapper->getRandomHumanValue([GenderMapper::HUMAN_UNDEFINED]); // Male || Female
License
adrenalinkin/enum-mapper 适用场景与选型建议
adrenalinkin/enum-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「php」 「convert」 「enum」 「component」 「humanize」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adrenalinkin/enum-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adrenalinkin/enum-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adrenalinkin/enum-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Provides a simple decimal/roman number converter
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-15