mistralys/application-utils-result-handling
Composer 安装命令:
composer require mistralys/application-utils-result-handling
包简介
Classes used to store information on the results of application operations.
README 文档
README
Collection of classes used to store information on the results of application operations.
This is part of the AppUtils project.
Features
- Store detailed status messages.
- Store success as well as error or warning messages.
- Store a single result or a collection of results.
- Ideal to collect validation messages, for example.
- Recognize results easily with numeric codes.
- Extend the classes to add custom methods.
Requirements
- PHP 7.4 or higher
- Composer
Usage
Single possible result
If an operation can only have a single possible result state,
you can use the OperationResult class.
use function AppUtils\operationResult const ERROR_FILE_NOT_FOUND = 1; function doSomething() : OperationResult { // Create a new result object using the global function $result = operationResult(); if(!file_exists('waldo.txt')) { return $result->makeError( 'The waldo file could not be found :(', ERROR_FILE_NOT_FOUND ); } return $result; } $result = doSomething(); if(!$result->isValid()) { echo $result; }
NOTE: The
isValid()method returnstrueif no state was set, or the state is of the success type.
Collection of results
Introduction
If an operation can have multiple possible result states, like a validation operation, for example, you can use the result collection class.
Every call to makeError(), makeWarning() or makeSuccess()
will add a new result instance to the collection.
All result types are stored in the collection, so it is important to note that both failed and successful operations can be tracked.
Quick Start
use function AppUtils\operationCollection; use AppUtils\OperationResult_Collection; const VALIDATION_ERROR_NAME_TOO_SHORT = 1; const VALIDATION_WARNING_NOT_RECOMMENDED_LENGTH = 2; function validateSomething() : OperationResult_Collection { $collection = operationCollection(); $collection->makeError( 'The name must be at least 5 characters long', VALIDATION_ERROR_NAME_TOO_SHORT ); $collection->makeWarning( 'The name must be at most 50 characters long', VALIDATION_WARNING_NOT_RECOMMENDED_LENGTH ) return $collection; } $collection = validateSomething(); if(!$collection->isValid()) { $results = $collection->getResults(); // do something with the results }
NOTE: The
isValid()method returnstrueif none of the results in the collection are of the error or warning type.
Accessing the results
Accessing results is made to be as easy as possible, with many different ways to get the information you need.
use function AppUtils\operationCollection; $collection = operationCollection(); // Get all results in the order they were added $results = $collection->getResults(); // Get all errors $errors = $collection->getErrors(); // Get all warnings $warnings = $collection->getWarnings(); // Get all successes $successes = $collection->getSuccesses(); // Get all notices $notices = $collection->getNotices(); // Check if the collection contains any result of a specific type if($collection->isError()) {} if($collection->isWarning()) {} if($collection->isSuccess()) {} if($collection->isNotice()) {} // Check if the collection contains a specific result code if($collection->containsCode(1)) { // do something } // Get all unique result codes $codes = $collection->getCodes();
Counting results
All result types can be counted individually, and the total number of results can be counted as well.
use function AppUtils\operationCollection; $collection = operationCollection(); // Count the total number of results $all = $collection->countResults(); $errors = $collection->countErrors(); $warnings = $collection->countWarnings(); $successes = $collection->countSuccesses(); $notices = $collection->countNotices();
Extend the result classes
Both the OperationResult and collection classes are designed to be extended,
so you can add your own custom methods to them.
The most common use for this is to correctly document the result subject's type:
use AppUtils\OperationResult; /** * @method MyOperation getSubject() */ class MyOperationResult extends OperationResult { public function __construct(MyOperation $subject) { parent::__construct($subject); } }
mistralys/application-utils-result-handling 适用场景与选型建议
mistralys/application-utils-result-handling 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38.29k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mistralys/application-utils-result-handling 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mistralys/application-utils-result-handling 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 38.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-13