eclipxe/micro-catalog
Composer 安装命令:
composer require eclipxe/micro-catalog
包简介
Micro catalog template class, useful to create value objects based on a known small catalog
README 文档
README
Micro catalog PHP template class, useful to create value objects based on a known small catalog.
Several times I have the need for a catalog, by example, when receiving a result code and correlate to specific related values. This library helps me to resolve this common problem, and it may help you too.
Installation
Use composer, install using:
composer require eclipxe/micro-catalog
Usage
MicroCatalog is a template class, so you need to extend it to use it. Strictly speaking, you only
need to implement two abstract methods: getEntriesArray(): array and getEntryValueOnUndefined(): mixed.
The sugar lives on is<Type>(): bool and get<Property>(): bool, with the special case of isUndefined(): bool.
The is<Type>() methods allow to distinct a type by its index.
The get<Property>() methods allow to get a string key from the value array when it has content.
See the following examples:
ResultCodesCommon usage with value asscalar, allows undefined properties.ComplexArrayCommon usage with value asarray, get property, allows undefined properties.ComplexObjectCommon usage with value asobject, get property, disable undefined properties.
MicroCatalog example
<?php declare(strict_types=1); use Eclipxe\MicroCatalog\MicroCatalog; /** * ResultCodes is an example class to show the basic usage * * @method bool isOk() * @method bool isWarning() * @method bool isError() */ final class ResultCodes extends MicroCatalog { public static function getEntriesArray(): array { return [ 0 => 'Ok', 1 => 'Warning', 2 => 'Error', ]; } public function getEntryValueOnUndefined(): string { return '#N/A'; } public function getEntryId(): string { return strval($this->getEntryValue()); } } $warning = new ResultCodes(1); var_dump($warning->isWarning()); // bool(true) var_dump($warning->getEntryIndex()); // int(1) var_dump($warning->getEntryValue()); // string(7) "Warning" var_dump($warning->getEntryId()); // string(7) "Warning" (method was overriden) var_dump($warning->isUndefined()); // bool(false) $other = new ResultCodes(99); var_dump($other->isUndefined()); // bool(true) var_dump($other->getEntryIndex()); // int(99) var_dump($other->getEntryValue()); // string(4) "#N/A"
Undefined entry
When creating an instance of MicroCatalog, when the key isn't found in the list of known values
it will use the value from getValueOnUndefined().
If you don't want to allow unknown instances you can override and throw an exception on getValueOnUndefined().
Check if instance is of certain entry
Use the methods is<name>() to compare to specific value.
It will work by default when the indexes are strings, in example: isFoo()
will evaluate if the current index is foo (with the first character as lower case).
You can redefine whits behavior by overriding the getEntryId(): string function. So isFoo() will compare
foo with the returned value of getEntryId(): string.
You have to define these methods in your docblock to let your IDE or code analyzer detect what you are doing.
Get a magic property
If your known values array constains an array with string keys or a standard object with public variables then
getSomething() will return what is defined on something key/property.
Even when PHP supports method with different case, this class use exact case, so getSomething() is different
from getSomeThing(). The only transformation made to locate the key/property is to lower first char, then
getSomething() will look up for something into local value. Anyhow, you can override the method
getEntryValueWithKey(string $key): mixed to perform other transformation on key/property name.
Extending
I recommend you to declare your MicroCatalog classes as final to disable extension.
When creating a MicroCatalog extending from other, the parent MicroCatalog have priority on indices and values.
You cannot override indices or values of previous classes.
Exceptions
Exceptions thrown from this package implements the empty interface Eclipxe\MicroCatalog\Exceptions\MicroCatalogException.
Creational patterns
You can write other creational patterns, the implemented constructor uses the value as index entry. If you want to create an entry based on some value or other type of evaluation I don't recommend to you to override the constructor, instead you can use static method calls.
PHP Support
This library is compatible with at least the oldest PHP Supported Version with active support. Please, try to use PHP full potential.
We adhere to Semantic Versioning. We will not introduce any compatibility backwards change on major versions.
Internal classes (using @internal annotation) are not part of this agreement
as they must only exist inside this project. Do not use them in your project.
Contributing
Contributions are welcome! Please read CONTRIBUTING for details and don't forget to take a look in TODO and CHANGELOG files.
Copyright and License
The eclipxe/micro-catalog library is copyright © Carlos C Soto
and licensed for use under the MIT License (MIT). Please see LICENSE for more information.
eclipxe/micro-catalog 适用场景与选型建议
eclipxe/micro-catalog 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 140.68k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「catalog」 「generic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eclipxe/micro-catalog 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eclipxe/micro-catalog 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eclipxe/micro-catalog 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LUYA Generic content
Java9 Optional implementation for PHP
Generic API Client for PHP
Generic and reusable component for other wordpress packages and themes
Extension for Catalog Manager.
Advanced ratings plugin for Premium Contao Themes CustomCatalog module https://www.premium-contao-themes.com/
统计信息
- 总下载量: 140.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 17
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-01