Skip to content

Commit

Permalink
Merge branch 'docs/17' into develop
Browse files Browse the repository at this point in the history
Forward port #17
  • Loading branch information
michalbundyra committed Mar 15, 2020
2 parents 2c2863f + e049ac1 commit bddc216
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 83 deletions.
81 changes: 0 additions & 81 deletions docs/book/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,87 +181,6 @@ instead.
Users pulling their `Float` validator instance from the validator plugin manager
receive an `IsFloat` instance instead starting in 2.4.0.

## IsInt

`Laminas\I18n\Validator\IsInt` validates if a given value is an integer, using the
locale provided.

### Supported Options

The following options are supported for `Laminas\I18n\Validator\IsInt`:

- `locale`: Sets the locale to use when validating localized integers.
- `strict`: Sets whether or not the value's data type should be checked.

### Basic integer validation

When no locale is provided to the validator, it uses the system locale:

```php
$validator = new Laminas\I18n\Validator\IsInt();

$validator->isValid(1234); // returns true
$validator->isValid(1234.5); // returns false
$validator->isValid('1,234'); // returns true
```

(The above example assumes that the environment locale is set to `en`.)

### Strict validation

By default, the value's data type is not enforced.

```php
$validator = new Laminas\I18n\Validator\IsInt();

$validator->isValid(1234); // returns true
$validator->isValid('1234'); // returns true
```

```php
$validator = new Laminas\I18n\Validator\IsInt();
$validator->setStrict(true);

$validator->isValid(1234); // returns true
$validator->isValid('1234'); // returns false
```

### Localized integer validation

Integer values are often written differently based on country or region. For
example, using English, you may write "1234" or "1,234"; both are integer
values, but the grouping is optional. In German, you'd write "1.234", and in
French, "1 234".

`Laminas\I18n\Validator\IsInt` will use a provided locale when evaluating the
validity of an integer value. In such cases, it doesn't simply strip the
validator, but instead validates that the correct separator as defined by the
locale is used.

```php
$validator = new Laminas\I18n\Validator\IsInt(['locale' => 'de']);

$validator->isValid(1234); // returns true
$validator->isValid("1,234"); // returns false
$validator->isValid("1.234"); // returns true
```

By using a locale, your input is validated based on the locale provided. Using a
notation not specific to the locale results in a `false` evaulation.

The default validation locale can also be set after instantiation using
`setLocale()`, and retrieved using `getLocale()`.

### Migration from 2.0-2.3 to 2.4+

Version 2.4 adds support for PHP 7. In PHP 7, `int` is a reserved keyword, which
required renaming the `Int` validator. If you were using the `Int` validator
directly previously, you will now receive an `E_USER_DEPRECATED` notice on
instantiation. Please update your code to refer to the `IsInt` class instead.

Users pulling their `Int` validator instance from the validator plugin manager
receive an `IsInt` instance instead starting in 2.4.0.

## PostCode

`Laminas\I18n\Validator\PostCode` allows you to determine if a given value is a
Expand Down
111 changes: 111 additions & 0 deletions docs/book/validators/is-int.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# IsInt

`Laminas\I18n\Validator\IsInt` validates if a given value **is an integer**, using
the locale provided.

Integer values are often written differently based on country or region. For
example, using English, you may write `1234` or `1,234`; both are integer
values, but the grouping is optional. In German, you'd write `1.234`, and in
French, `1 234`.

`Laminas\I18n\Validator\IsInt` will use a provided locale when evaluating the
validity of an integer value. In such cases, it doesn't simply strip the
separator, but instead validates that the correct separator as defined by used
locale.

## Basic Usage

```php
$validator = new Laminas\I18n\Validator\IsInt();

$validator->isValid(1234); // returns true
$validator->isValid(1234.5); // returns false
$validator->isValid('1,234'); // returns true
```

By default, if no locale is provided, `IsInt` will use the system locale
provided by PHP's `Locale` class and the `getDefault()` method.

(The above example assumes that the environment locale is set to `en`.)

Using a notation not specific to the locale results in a `false` evaulation.

## Using Locale

```php fct_label="Constructor Usage"
$validator = new Laminas\I18n\Validator\IsInt(['locale' => 'en_US']);

$validator->isValid(1234); // true
```

```php fct_label="Setter Usage"
$validator = new Laminas\I18n\Validator\IsInt();
$validator->setLocale('en_US');

$validator->isValid(1234); // true
```

```php fct_label="Locale Class Usage"
Locale::setDefault('en_US');

$validator = new Laminas\I18n\Validator\IsInt();

$validator->isValid(1234); // true
```

### Get Current Value

To get the current value of this option, use the `getLocale()` method.

```php
$validator = new Laminas\I18n\Validator\IsInt(['locale' => 'en_US']);

echo $validator->getLocale(); // 'en_US'
```

### Default Value

By default, if no locale is provided, `IsInt` will use the system locale
provided by PHP's `Locale::getDefault()`.

## Strict Validation

By default, the value's data type is not enforced.

```php fct_label="Default (Without Strict)"
$validator = new Laminas\I18n\Validator\IsInt();

$validator->isValid(1234); // true
$validator->isValid('1234'); // true
```

To enforced a strict validation set the `strict` option to `true`.

```php fct_label="Constructor Usage"
$validator = new Laminas\I18n\Validator\IsInt(['strict' => true]);

$validator->isValid(1234); // true
$validator->isValid('1234'); // false
```

```php fct_label="Setter Usage"
$validator = new Laminas\I18n\Validator\IsInt();
$validator->setStrict(true)

$validator->isValid(1234); // true
$validator->isValid('1234'); // false
```

### Get Current Value

To get the current value of this option, use the `getStrict()` method.

```php
$validator = new Laminas\I18n\Validator\IsInt(['strict' => true]);

echo $validator->getStrict(); // true
```

### Default Value

The default value of this option is `false`.
4 changes: 2 additions & 2 deletions docs/book/validators/phone-number.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var_dump($validator->isValid('+4930123456')); // true
```

By default, if no country code is provided, `PhoneNumber` will use the system
locale provide by PHP's `Locale::getDefault()` and `Locale::getRegion()` to
locale provided by PHP's `Locale::getDefault()` and `Locale::getRegion()` to
extract the country code.

(The above example assumes that the environment locale is set to `de_DE`.)
Expand Down Expand Up @@ -55,7 +55,7 @@ echo $validator->getCountry(); // 'US'
### Default Value

By default, if no country is provided, `PhoneNumber` will use the system locale
provide by PHP's `Locale::getDefault()` and `Locale::getRegion()` to extract
provided by PHP's `Locale::getDefault()` and `Locale::getRegion()` to extract
the region code.

## Using Allowed Phone Number Patterns
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ nav:
- Validators:
- 'Standard Validators': validators.md
- DateTime: validators/date-time.md
- IsInt: validators/is-int.md
- PhoneNumber: validators/phone-number.md
site_name: laminas-i18n
site_description: 'Provide translations for your application, and filter and validate internationalized values.'
Expand Down

0 comments on commit bddc216

Please sign in to comment.