Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqby committed Nov 27, 2015
1 parent 49a09ee commit 0aa2ee7
Show file tree
Hide file tree
Showing 6 changed files with 693 additions and 6 deletions.
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
assets/*
!assets/.gitignore
protected/runtime/*
!protected/runtime/.gitignore
protected/data/*.db
themes/classic/views/
vendor/*
.idea/*
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,65 @@
# yii2-phone-formatter
Phone numbers formatter and behavior for Yii2 Framework

### Composer

The preferred way to install this extension is through [Composer](http://getcomposer.org/).

Either run

```
php composer.phar require uniqby/yii2-phone-formatter "dev-master"
```

or add

```
"uniqby/yii2-phone-formatter": "dev-master"
```

to the require section of your ```composer.json```

## Configuration

### Configure your application in common config:
```php
'components' => [
'formatter' => [
'class' => 'uniqby\phoneFormatter\i18n\Formatter',
]
]
```

Now you can use asPhoneE164 and asPhoneInt methods

```php
echo \Yii::$app->formatter->asPhoneE164(
'+375259862464',
'BY'
);

echo \Yii::$app->formatter->asPhoneInt(
'+375 25 986-24-64',
'BY'
);
```

### Behavior
You can add behavior to your models

```php
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'convertPhone' => [
'class' => PhoneFormatterBehavior::className(),
'attributes' => [
'number'
]
]
];
}
```
72 changes: 72 additions & 0 deletions behaviors/PhoneFormatterBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Created by PhpStorm.
* User: alexander
* Date: 26.11.15
* Time: 19:43
*
*
*/
namespace uniqby\phoneFormatter\behaviors;

use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;
use yii\db\Expression;

/**
* Class PhoneFormatterBehavior
* @package common\components\behaviors
*
* @property ActiveRecord $owner
*/
class PhoneFormatterBehavior extends Behavior
{
/**
* @var array the attribute that will receive number value
*/
public $attributes = ['number'];

public $countryAlpha2 = 'RU';

/**
* @var callable|Expression The expression that will be used for generating the timestamp.
* This can be either an anonymous function that returns the timestamp value,
* or an [[Expression]] object representing a DB expression (e.g. `new Expression('NOW()')`).
* If not set, it will use the value of `time()` to set the attributes.
*/
public $value;


/**
* @inheritdoc
*/
public function events()
{
return [
BaseActiveRecord::EVENT_BEFORE_INSERT => 'convertE164',
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'convertE164',
BaseActiveRecord::EVENT_AFTER_FIND => 'convertInt'
];
}

public function convertInt($event)
{
foreach ($this->attributes as $attribute) {
$this->owner->{$attribute} = \Yii::$app->formatter->asPhoneInt(
str_replace('++', '+', $this->owner->{$attribute}),
$this->countryAlpha2
);
}
}

protected function convertE164($event)
{
foreach ($this->attributes as $attribute) {
$this->owner->{$attribute} = \Yii::$app->formatter->asPhoneE164(
str_replace('++', '+', $this->owner->{$attribute}),
$this->countryAlpha2
);
}
}
}
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "uniqby/yii2-phone-formatter",
"description": "Phone numbers formatter and behavior for Yii2 Framework",
"minimum-stability": "dev",
"license": "MIT",
"authors": [
{
"name": "Alexander Sazanovich",
"email": "[email protected]"
}
],
"require": {
"yiisoft/yii2": "2.*",
"yiisoft/yii2-composer": "2.*",
"giggsey/libphonenumber-for-php": "~7.0"
},
"autoload": {
"psr-4": {
"uniqby\\phoneFormatter\\": ""
}
}
}
Loading

0 comments on commit 0aa2ee7

Please sign in to comment.