-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
693 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] | ||
] | ||
]; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\": "" | ||
} | ||
} | ||
} |
Oops, something went wrong.