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
1 changed file
with
76 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
Phone number Doctrine type | ||
=========================== | ||
|
||
[![Build Status](https://travis-ci.org/nepada/phone-number-doctrine.svg?branch=master)](https://travis-ci.org/nepada/phone-number-doctrine) | ||
[![Coverage Status](https://coveralls.io/repos/github/nepada/phone-number-doctrine/badge.svg?branch=master)](https://coveralls.io/github/nepada/phone-number-doctrine?branch=master) | ||
[![Downloads this Month](https://img.shields.io/packagist/dm/nepada/phone-number-doctrine.svg)](https://packagist.org/packages/nepada/phone-number-doctrine) | ||
[![Latest stable](https://img.shields.io/packagist/v/nepada/phone-number-doctrine.svg)](https://packagist.org/packages/nepada/phone-number-doctrine) | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
Via Composer: | ||
|
||
```sh | ||
$ composer require nepada/phone-number-doctrine | ||
``` | ||
|
||
Register the type in your bootstrap: | ||
``` php | ||
\Doctrine\DBAL\Types\Type::addType( | ||
\Nepada\PhonNumberDoctrine\PhonNumberType::NAME, | ||
\Nepada\PhonNumberDoctrine\PhonNumberType::class | ||
); | ||
``` | ||
|
||
In Nette with [kdyby/doctrine](https://github.com/Kdyby/Doctrine) integration, you can register the types in your configuration: | ||
```yaml | ||
doctrine: | ||
types: | ||
phone_number: Nepada\PhoneNumberDoctrine\PhoneNumberType | ||
|
||
``` | ||
|
||
|
||
Usage | ||
----- | ||
|
||
`PhoneNumberType` maps database value to phone number value object (see [brick/phonenumber](https://github.com/brick/phonenumber) for further details) and back. The phone number is stored using E164 format, i.e. a '+' sign followed by a series of digits comprising the country code and national number. | ||
|
||
Example usage in the entity: | ||
``` php | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Brick\PhoneNumber\PhoneNumber; | ||
|
||
/** | ||
* @ORM\Entity | ||
* @ORM\Table(name="contacts") | ||
*/ | ||
class Contact | ||
{ | ||
|
||
/** | ||
* @ORM\Column(type="phone_number", nullable=false) | ||
* @var PhoneNumber | ||
*/ | ||
private $phoneNumber; | ||
|
||
public function getPhoneNumber(): PhoneNumber | ||
{ | ||
return $this->phoneNumber; | ||
} | ||
|
||
} | ||
``` | ||
|
||
Example usage in query builder: | ||
```php | ||
$result = $repository->createQueryBuilder('foo') | ||
->select('foo') | ||
->where('foo.phoneNumber = :phoneNumber') | ||
// the parameter value is automatically normalized to +420123456789 | ||
->setParameter('phoneNumber', '+420 123 456 789', PhoneNumberType::NAME) | ||
->getQuery() | ||
->getResult() | ||
``` |