Skip to content

Commit

Permalink
add dutch language #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Martinus van Middelaar committed Sep 30, 2018
1 parent d62b4ac commit 1adcea0
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log: `yii2-bsnvalidator`
===========================

## Version 1.2.0

- Added dutch translation

## Version 1.1.0

- Added client side validation - #1
Expand Down
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,40 @@ to the ```require``` section of your `composer.json` file.

## Usage

### BsnValidator
```php
// add this in your model
use mahkali\validators\BsnValidator;

// use the validator in your model rules
public function rules() {
public function rules()
{
return [
['bsn', BsnValidator::class],
];
}
```

## Translations

You could set the message attribute. Or - add your language to the package:

Edit:

`@vendor/mahkali/yii2-bsnvalidator/src/messages/config.php`

Add your language.

Run:

```bash
./yii message @vendor/mahkali/yii2-bsnvalidator/src/messages/config.php
```

Edit your message file in

`@vendor/mahkali/yii2-bsnvalidator/src/messages/xx/bsnvalidator.php`

And make a PR :) Thank you very much.

## License

Expand Down
53 changes: 30 additions & 23 deletions src/BsnValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,20 @@ class BsnValidator extends Validator
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = \Yii::t('app', '{attribute} must be a valid BSN number.');
}
}

protected function validateValue($value)
{
if (!$this->checkBsn($value)) {
return [$this->message, []];
}
return null;
}

protected function checkBsn($input)
{
if (strlen($input) != 9 && strlen($input) != 8) {
return false;
}
$numbers = str_split(str_pad($input, 9, '0', STR_PAD_LEFT));
$total = 0;
foreach ($numbers as $i => $number) {
$value = $number * (9 - $i) * ((9 - $i) === 1 ? -1 : 1);
$total += $value;
if ($this->message === null) {
$i18n = \Yii::$app->i18n;
$i18n->translations['bsnvalidator'] = [
'class' => '\yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@vendor/mahkali/yii2-bsnvalidator/src/messages',
];

$this->message = \Yii::t('bsnvalidator', '{attribute} must be a valid BSN number.');
}
return ($total % 11) === 0;
}


public function clientValidateAttribute($model, $attribute, $view)
{
BsnValidatorAsset::register($view);
Expand All @@ -74,6 +60,27 @@ public function getClientOptions($model, $attribute)
return $options;
}

protected function validateValue($value)
{
if (!$this->checkBsn($value)) {
return [$this->message, []];
}
return null;
}

protected function checkBsn($input)
{
if (strlen($input) != 9 && strlen($input) != 8) {
return false;
}
$numbers = str_split(str_pad($input, 9, '0', STR_PAD_LEFT));
$total = 0;
foreach ($numbers as $i => $number) {
$value = $number * (9 - $i) * ((9 - $i) === 1 ? -1 : 1);
$total += $value;
}
return ($total % 11) === 0;
}


}
45 changes: 45 additions & 0 deletions src/messages/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Configuration file for 'yii message/extract' command.
*
* This file is automatically generated by 'yii message/config' command.
* It contains parameters for source code messages extraction.
* You may modify this file to suit your needs.
*
* You can use 'yii message/config-template' command to create
* template configuration file with detailed description for each parameter.
*/
return [
'color' => null,
'interactive' => true,
'help' => null,
'sourcePath' => '@vendor/mahkali/yii2-bsnvalidator/src',
'messagePath' => '@vendor/mahkali/yii2-bsnvalidator/src/messages',
'languages' => ['en','nl'],
'translator' => 'Yii::t',
'sort' => false,
'overwrite' => true,
'removeUnused' => false,
'markUnused' => true,
'except' => [
'.svn',
'.git',
'.gitignore',
'.gitkeep',
'.hgignore',
'.hgkeep',
'/messages',
'/BaseYii.php',
],
'only' => [
'*.php',
],
'format' => 'php',
'db' => 'db',
'sourceMessageTable' => '{{%source_message}}',
'messageTable' => '{{%message}}',
'catalog' => 'messages',
'ignoreCategories' => [],
'phpFileHeader' => '',
'phpDocBlock' => null,
];
21 changes: 21 additions & 0 deletions src/messages/en/bsnvalidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message/extract' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'{attribute} must be a valid BSN number.' => '',
];
21 changes: 21 additions & 0 deletions src/messages/nl/bsnvalidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message/extract' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'{attribute} must be a valid BSN number.' => '{attribute} moet een geldig BSN nummer zijn.',
];

0 comments on commit 1adcea0

Please sign in to comment.