summaryrefslogtreecommitdiff
path: root/README.md
blob: 19868bcb292dde2558aa4de5b8ae7f2790419b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Bouncer

[![Latest Stable Version](http://poser.pugx.org/askonomm/bouncer/v)](https://packagist.org/packages/askonomm/bouncer))

An extensible validation library for your data with sane defaults.

![Bouncer](https://user-images.githubusercontent.com/84135165/155233016-1dd9990f-ed60-44cc-b44c-ea90a11fc350.jpg)

## Installation

You can install the package via composer:

```
composer require askonomm/bouncer
```

## Usage

The Bouncer takes in an array of fields, an array of rules and optionally an array of validators. If no validators are provided, default validators will be used instead, which are:

- `\Askonomm\Bouncer\Validators\LenValidator`
- `\Askonomm\Bouncer\Validators\EmailValidator`
- `\Askonomm\Bouncer\Validators\RequiredValidator`

The key of each item in the `$fields` array must correspond to the the key of each item in the `$rules` array, so that Bouncer would know how to connect the two to each other.

The `$rules` must have a value that is a string where the rules are separated by a `|` character, and each rule must match the key of the implemented validator, such as `len`, `email` or one that you have implemented yourself. Additionally, each rule can take in a modifier, where the name of the rule and the modifier is separated by a `:` character.
 
For example, say we have a validator called `len` which takes a modifier that lets that validator validate the length of a string, in such a case we'd write that rule as `len:8`, which would indicate using a `len` validator and passing a modifier with the value `8` to it. 

### Example usage

An example usage of Bouncer looks like this:

```php
use Askonomm\Bouncer\Bouncer;

$fields = ['email' => 'asko@bien.ee'];
$rules = ['email' => 'required|email|len:5'];
$bouncer = new Bouncer($fields, $rules);

if ($bouncer->fails()) {
    return $bouncer->errors();
}
```

From the above example, you can see that there are two Bouncer methods being used such as `$bouncer->fails()` and `$bouncer->errors()`. The `$bouncer->fails()` method will return a boolean depending on whether the validation failed or not, `true` if it did. The `$bouncer->errors()` method will return an array of all the errors that occured, as defined by the validators.

You can also get the first error rather than all errors by using the method `$bouncer->firstError()`. 

## Built-in validators

There are a number of built-in validators avaiable for use by default. If you want to remove a built-in validator, you can remove one using the `$bouncer->removeValidator('rule-name')` method.

### `email`

The `email` validator validates an e-mail address, and it is registered as the `email` rule.

```php
use Askonomm\Bouncer\Bouncer;

$fields = ['email' => 'asko@bien.ee'];
$rules = ['email' => 'email'];
$bouncer = new Bouncer($fields, $rules);
```

### `len`

The `len` validator validates the length of a string, and it is registered as the `len` rule. The `len` validator also accepts, and requires, a modifier. A modifier can be passed to a rule by appending a color character `:` to it, and passing the modifier after it, like `len:8`.

```php
use Askonomm\Bouncer\Bouncer;

$fields = ['password' => 'SuperSecretPassword'];
$rules = ['password' => 'len:10'];
$bouncer = new Bouncer($fields, $rules);
```

### `required`

The `required` validator validates the presence of value, and it is registered as the `required` rule. It will pass validation if the value is set and the value is not an empty string.

```php
use Askonomm\Bouncer\Bouncer;

$fields = ['password' => 'SuperSecretPassword'];
$rules = ['password' => 'required'];
$bouncer = new Bouncer($fields, $rules);
```

## Creating validators

You can also create your own validators, or replace existing ones if you're not happy with them. 

**Note:** To replace an existing one, first remove the built-in validator via `$bouncer->removeValidator('rule-name')` and then add your own via `$bouncer->registerValidator('rule-name', $validator)`. 


A validator is a class that implements the `Validator` interface. A full example of a correct validator would look something like this:

```php
use Askonomm\Bouncer\Validators\Validator;

class EmailValidator implements Validator
{
    /**
     * Returns a boolean `true` when given `$value` is a valid e-mail
     * address. Returns `false` otherwise.
     *
     * @param mixed $value
     * @param mixed $modifier
     * @return boolean
     */
    public static function validate(mixed $value, mixed $modifier = null): bool
    {
        return filter_var($value, FILTER_VALIDATE_EMAIL);
    }

    /**
     * Composes the error message in case the validation fails.
     *
     * @param string $field
     * @param mixed $modifier
     * @return string
     */
    public static function composeError(string $field, mixed $modifier = null): string
    {
        return "${field} is not a valid e-mail address.";
    }
}
```

You can see that there are two methods, one for validating the `$value` and the other for composing an error message if the validation fails. Both functions take in a `$modifier` argument, which will only have value if the validator is using modifiers. For example, the `len` validator is using modifiers to determine how long of a string should be required, by passing the rule in as `len:{number-of-characters}`. 

Once you've created the class for your validator, you can register it by calling `$bouncer->registerValidator('rule-name', (new YourValidatorClass))`.