From c6c2cd3e9139d0aefdc9e8f519bf503f66ee1691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimondas=20Rimkevi=C4=8Dius=20=28aka=20MekDrop=29?= Date: Wed, 9 Jul 2025 01:27:26 +0300 Subject: [PATCH] Improve README.md Resolves #56 --- README.md | 134 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index f703a32..c529871 100644 --- a/README.md +++ b/README.md @@ -1,72 +1,112 @@ [![License](https://img.shields.io/github/license/imponeer/object-errors.svg?maxAge=2592000)](LICENSE) -[![Packagist](https://img.shields.io/packagist/v/imponeer/object-errors.svg)](https://packagist.org/packages/imponeer/object-errors) [![PHP](https://img.shields.io/packagist/php-v/imponeer/object-errors.svg)](http://php.net) +[![Packagist](https://img.shields.io/packagist/v/imponeer/object-errors.svg)](https://packagist.org/packages/imponeer/object-errors) [![PHP](https://img.shields.io/packagist/php-v/imponeer/object-errors.svg)](http://php.net) [![Packagist](https://img.shields.io/packagist/dm/imponeer/object-errors.svg)](https://packagist.org/packages/imponeer/object-errors) # Object Errors -Library that can be used for collecting errors on objects. +A PHP library for collecting and managing errors associated with objects. Useful for tracking validation or processing errors in a structured way. ## Installation -To install and use this package, we recommend to use [Composer](https://getcomposer.org): +Install via [Composer](https://getcomposer.org): ```bash composer require imponeer/object-errors ``` -Otherwise you need to include manualy files from `src/` directory. +Alternatively, you can manually include the files from the `src/` directory. -## Example +## Usage + +This library allows you to attach an error collection to your objects and manage errors easily. + +### Using as a property +Below is a simple usage example by directly creating an `ErrorsCollection` instance: ```php +use Imponeer\ObjectErrors\ErrorsCollection; + +class MyObject { + /** + * @var ErrorsCollection|null + */ + public $errors = null; + + public function __construct() { + $this->errors = new ErrorsCollection(); + } + + public function doSomething() { + // Example logic + if ($failed) { + $this->errors->add("Some error"); + } + } + + public function render() { + if ($this->errors->isEmpty()) { + return 'Everything fine'; + } else { + return $this->errors->getHtml(); + } + } +} +``` -use Imponeer/ObjectErrors/ErrorsCollection; - -class Object { - - /** - * Errors variable - * - * @var null|ErrorsCollection - */ - public $errors = null; - - /** - * Constructor (binds new instance of ErrorsCollection to $errors var) - */ - public function __constructor() { - $this->errors = new ErrorsCollection(); - } - - /** - * This method do something - */ - public function doSomething() { - // here we should do something - if ($failed) { - $this->errors->add("Some error"); - } - } - - /** - * Renders object content - * - * @return string - */ - public function render() { - if ($this->errors->isEmpty()) { - return 'Everything fine'; - } else { - return $this->errors->getHTML(); - } - } +### Using as a trait +You can also use the provided `ErrorsTrait` to quickly add error handling to your classes: + +```php +use Imponeer\ObjectErrors\ErrorsTrait; +class MyObject { + use ErrorsTrait; + + public function doSomething() { + if ($failed) { + $this->setErrors("Some error"); + } + } + + public function render() { + if ($this->hasError()) { + return $this->getHtmlErrors(); + } + return 'Everything fine'; + } } +``` + +## Development + +Below are useful commands for development. Each command should be run from the project root directory. +Run tests using PHPUnit: +```bash +composer test +``` + +Check code style using PHP_CodeSniffer: +```bash +composer phpcs +``` + +Automatically fix code style issues: +```bash +composer phpcbf +``` + +Run static analysis using PHPStan: +```bash +composer phpstan ``` +## API Documentation + +For detailed API documentation, please visit the [Object Errors Wiki](https://github.com/imponeer/object-errors/wiki). + ## How to contribute? -If you want to add some functionality or fix bugs, you can fork, change and create pull request. If you not sure how this works, try [interactive GitHub tutorial](https://skills.github.com). +Contributions are welcome! If you want to add new features or fix bugs, please fork the repository, make your changes, and submit a pull request. -If you found any bug or have some questions, use [issues tab](https://github.com/imponeer/object-errors/issues) and write there your questions. +If you find any bugs or have questions, please use the [issues tab](https://github.com/imponeer/object-errors/issues) to report them or ask questions.