Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new documentation for IsInt validator #17

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
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 the
locale is used.
froschdesign marked this conversation as resolved.
Show resolved Hide resolved

## 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
provide by PHP's `Locale` class and the `getDefault()` method.
froschdesign marked this conversation as resolved.
Show resolved Hide resolved

(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
provide 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`.
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