{"id":24196236,"url":"https://github.com/mistralys/application-utils-result-handling","last_synced_at":"2026-02-08T18:04:03.269Z","repository":{"id":272318083,"uuid":"916185557","full_name":"Mistralys/application-utils-result-handling","owner":"Mistralys","description":"Classes used to store information on the results of application operations.","archived":false,"fork":false,"pushed_at":"2025-01-13T18:15:11.000Z","size":34,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-03T07:46:05.631Z","etag":null,"topics":["php","result-collection","resultset"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mistralys.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-13T16:05:37.000Z","updated_at":"2025-01-13T18:14:01.000Z","dependencies_parsed_at":"2025-01-13T17:28:27.759Z","dependency_job_id":"464befe4-160b-4640-a8ae-91d0a3978d98","html_url":"https://github.com/Mistralys/application-utils-result-handling","commit_stats":null,"previous_names":["mistralys/application-utils-result-handling"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fapplication-utils-result-handling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fapplication-utils-result-handling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fapplication-utils-result-handling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fapplication-utils-result-handling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mistralys","download_url":"https://codeload.github.com/Mistralys/application-utils-result-handling/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241629736,"owners_count":19993708,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","result-collection","resultset"],"created_at":"2025-01-13T19:23:59.449Z","updated_at":"2026-02-08T18:04:03.220Z","avatar_url":"https://github.com/Mistralys.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AppUtils - Result handling\n\nCollection of classes used to store information on the results of application operations.\n\nThis is part of the [AppUtils project](https://github.com/Mistralys/application-utils).\n\n# Features\n\n- Store detailed status messages.\n- Store success as well as error or warning messages.\n- Store a single result or a collection of results.\n- Ideal to collect validation messages, for example.\n- Recognize results easily with numeric codes.\n- Extend the classes to add custom methods.\n\n# Requirements\n\n- PHP 7.4 or higher\n- [Composer](https://getcomposer.org/)\n\n# Usage\n\n## Single possible result\n\nIf an operation can only have a single possible result state,\nyou can use the `OperationResult` class.\n\n```php\nuse function AppUtils\\operationResult\n\nconst ERROR_FILE_NOT_FOUND = 1;\n\nfunction doSomething() : OperationResult \n{\n    // Create a new result object using the global function\n    $result = operationResult();\n    \n    if(!file_exists('waldo.txt')) {\n        return $result-\u003emakeError(\n            'The waldo file could not be found :(',\n            ERROR_FILE_NOT_FOUND\n        );\n    }\n    \n    return $result;\n}\n\n$result = doSomething();\n\nif(!$result-\u003eisValid()) {\n    echo $result;\n}\n```\n\n\u003e NOTE: The `isValid()` method returns `true` if \n\u003e no state was set, or the state is of the success type. \n\n## Collection of results\n\n### Introduction\n\nIf an operation can have multiple possible result states,\nlike a validation operation, for example, you can use the\nresult collection class.\n\nEvery call to `makeError()`, `makeWarning()` or `makeSuccess()`\nwill add a new result instance to the collection.\n\n\u003e All result types are stored in the collection, so it is\n\u003e important to note that both failed and successful operations\n\u003e can be tracked.\n\n### Quick Start \n\n```php\nuse function AppUtils\\operationCollection;\nuse AppUtils\\OperationResult_Collection;\n\nconst VALIDATION_ERROR_NAME_TOO_SHORT = 1;\nconst VALIDATION_WARNING_NOT_RECOMMENDED_LENGTH = 2;\n\nfunction validateSomething() : OperationResult_Collection \n{\n    $collection = operationCollection();\n    \n    $collection-\u003emakeError(\n        'The name must be at least 5 characters long',\n        VALIDATION_ERROR_NAME_TOO_SHORT\n    );\n    \n    $collection-\u003emakeWarning(\n        'The name must be at most 50 characters long',\n        VALIDATION_WARNING_NOT_RECOMMENDED_LENGTH\n    )\n    \n    return $collection;\n}\n\n$collection = validateSomething();\n\nif(!$collection-\u003eisValid()) {\n     $results = $collection-\u003egetResults();\n     // do something with the results\n}\n```\n\n\u003e NOTE: The `isValid()` method returns `true` if none of the\n\u003e results in the collection are of the error or warning type.\n\n### Accessing the results\n\nAccessing results is made to be as easy as possible, with many\ndifferent ways to get the information you need.\n\n```php\nuse function AppUtils\\operationCollection;\n\n$collection = operationCollection();\n\n// Get all results in the order they were added\n$results = $collection-\u003egetResults(); \n\n// Get all errors\n$errors = $collection-\u003egetErrors();\n\n// Get all warnings\n$warnings = $collection-\u003egetWarnings();\n\n// Get all successes\n$successes = $collection-\u003egetSuccesses();\n\n// Get all notices\n$notices = $collection-\u003egetNotices();\n\n// Check if the collection contains any result of a specific type\nif($collection-\u003eisError()) {}\nif($collection-\u003eisWarning()) {}\nif($collection-\u003eisSuccess()) {}\nif($collection-\u003eisNotice()) {}\n\n// Check if the collection contains a specific result code\nif($collection-\u003econtainsCode(1)) {\n    // do something\n}\n\n// Get all unique result codes\n$codes = $collection-\u003egetCodes();\n```\n\n### Counting results\n\nAll result types can be counted individually, and the total\nnumber of results can be counted as well.\n\n```php\nuse function AppUtils\\operationCollection;\n\n$collection = operationCollection();\n\n// Count the total number of results\n$all = $collection-\u003ecountResults();\n$errors = $collection-\u003ecountErrors();\n$warnings = $collection-\u003ecountWarnings();   \n$successes = $collection-\u003ecountSuccesses();\n$notices = $collection-\u003ecountNotices();\n    \n\n```\n\n## Extend the result classes\n\nBoth the `OperationResult` and collection classes are designed to be extended, \nso you can add your own custom methods to them. \n\nThe most common use for this is to correctly document the result subject's type:\n\n```php\nuse AppUtils\\OperationResult;\n\n/**\n * @method MyOperation getSubject() \n */\nclass MyOperationResult extends OperationResult\n{\n    public function __construct(MyOperation $subject)\n    {\n        parent::__construct($subject);\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistralys%2Fapplication-utils-result-handling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmistralys%2Fapplication-utils-result-handling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistralys%2Fapplication-utils-result-handling/lists"}