Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 87 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.