From ef1dd318d437bb33adddf226c546e85f4744ce8c Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Mon, 25 Sep 2023 01:35:47 +0300 Subject: Update stuff --- README.md | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 26685b1..32b3fb4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Hird -[![Latest Stable Version](http://poser.pugx.org/askonomm/hird/v)](https://packagist.org/packages/askonomm/hird) +[![Latest Stable Version](http://poser.pugx.org/asko/hird/v)](https://packagist.org/packages/asko/hird) > Hirds, also known as housecarls, was a gathering of hirdmen, who functioned as the king's personal guards during the viking age and the early middle ages. @@ -13,7 +13,7 @@ Hird is an extensible validation library for your data with sane defaults. You can install the package via composer: ``` -composer require askonomm/hird +composer require asko/hird ``` ## Usage @@ -23,8 +23,8 @@ Hird takes in an array of `$fields` and an array of `$rules`. The key of each item in the `$fields` array must correspond to the the key of each item in the `$rules` array, so that Hird 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. + +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 @@ -33,7 +33,7 @@ An example usage of Hird looks like this: ```php use Askonomm\Hird\Hird; -$fields = ['email' => 'asko@bien.ee']; +$fields = ['email' => 'asko@asko.dev']; $rules = ['email' => 'required|email|len:5']; $hird = new Hird($fields, $rules); @@ -44,9 +44,9 @@ if ($hird->fails()) { From the above example, you can see that there are two Hird methods being used such as `$hird->fails()` and `$hird->errors()`. The `$hird->fails()` method will run the validation and return a boolean depending on whether the validation failed or not, `true` if it did. The `$hird->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 `$hird->firstError()`. +You can also get the first error rather than all errors by using the method `$hird->firstError()`. -If you wish to run the validation without needing to call `$hird->fails()`, you can instead call `$hird->validate()`. +If you wish to run the validation without needing to call `$hird->fails()`, you can instead call `$hird->validate()`. ## Built-in validators @@ -102,15 +102,14 @@ $hird = new Hird($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 `$hird->removeValidator('rule-name')` and then add your own via `$hird->registerValidator('rule-name', $validator)`. +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 `$hird->removeValidator('rule-name')` and then add your own via `$hird->registerValidator('rule-name', ValidatorClass::class)`. 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\Hird\Validators\Validator; +use Asko\Hird\Validators\Validator; class EmailValidator implements Validator { @@ -122,7 +121,7 @@ class EmailValidator implements Validator * @param mixed $modifier * @return boolean */ - public static function validate(string $field, mixed $value, mixed $modifier = null): bool + public function validate(string $field, mixed $value, mixed $modifier = null): bool { return filter_var($value, FILTER_VALIDATE_EMAIL); } @@ -134,13 +133,13 @@ class EmailValidator implements Validator * @param mixed $modifier * @return string */ - public static function composeError(string $field, mixed $modifier = null): string + public 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}`. +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 `$hird->registerValidator('rule-name', (new YourValidatorClass))`. \ No newline at end of file +Once you've created the class for your validator, you can register it by calling `$hird->registerValidator('rule-name', (new YourValidatorClass))`. -- cgit v1.2.3