Skip to content

Commit 029a516

Browse files
committed
first commit
0 parents  commit 029a516

File tree

7 files changed

+320
-0
lines changed

7 files changed

+320
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.phpunit.result.cache
2+
/build/
3+
/composer.lock
4+
/phpcs.xml
5+
/phpunit.xml
6+
/vendor/
7+
phpunit.xml.bak
8+
bin/

LICENCE

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c)2020 Cherif BOUCHELAGHEM
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6+
associated documentation files (the "Software"), to deal in the Software without restriction,
7+
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9+
subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all copies or substantial
12+
portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15+
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# cherif/php-algerian-mobile-phone-number-doctrine
2+
3+
The cherif/php-algerian-mobile-phone-number-doctrine package allows to use (cherif/algerian-mobile-phone-number)[https://github.com/cherifGsoul/php-algerian-mobile-phone-number] as a (Doctrine field type)[https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/cookbook/custom-mapping-types.html].
4+
5+
## Installtion
6+
The recommended way of installation is by using (Packagist)[https://packagist.org/packages/cherif/php-algerian-mobile-phone-number-doctrine] and (Composer)[http://getcomposer.org/].
7+
8+
The following command should be executed in order to add the package as a requirement to `composer.json` of a project:
9+
10+
```shell
11+
$ composer require cherif/php-algerian-mobile-phone-number-doctrine
12+
```
13+
14+
## Examples:
15+
To configure Doctrine to use cherif/php-algerian-mobile-phone-number-doctrine as a field type, you'll need to set up the following in your bootstrap:
16+
17+
```php
18+
\Doctrine\DBAL\Types\Type::addType('algerian_mobile_phone_number', 'Cherif\AlgerianMobilePhoneNumber\Doctrine\AlgerianMobilePhoneNumberType');
19+
```
20+
21+
In Symfony:
22+
23+
```yaml
24+
# config/packages/doctrine.yaml
25+
doctrine:
26+
dbal:
27+
types:
28+
algerian_mobile_phone_number: Cherif\AlgerianMobilePhoneNumber\Doctrine\AlgerianMobilePhoneNumberType
29+
```
30+
## Usage:
31+
32+
Then, in your entities, you may annotate properties by setting the @Column type to `algerian_mobile_phone_number`:
33+
34+
```php
35+
use Doctrine\ORM\Mapping as ORM;
36+
use Cherif\AlgerianMobilePhoneNumber\AlgerianMobilePhoneNumber;
37+
38+
/**
39+
* @ORM\Entity
40+
* @ORM\Table(name="person")
41+
*/
42+
class Person
43+
{
44+
/**
45+
* @var Cherif\AlgerianMobilePhoneNumber\AlgerianMobilePhoneNumber
46+
*
47+
* @ORM\Id
48+
* @ORM\Column(type="algerian_mobile_phone_number", unique=true)
49+
*/
50+
protected $mobilePhoneNumber;
51+
52+
public function getMobilePhoneNumber(): AlgerianMobilePhoneNumber
53+
{
54+
return $this->mobilePhoneNumber;
55+
}
56+
}
57+
```
58+
59+
To use XML Mapping instead of PHP annotations.
60+
61+
```xml
62+
...
63+
64+
<field name="mobilePhoneNumber" column="mobile_phone_number" type="algerian_mobile_phone_number" unique="true" />
65+
66+
...
67+
```
68+
69+
## Contribution
70+
Contributions are welcome to make this library better.
71+
72+
- Clone the repo:
73+
74+
```shell
75+
$ git clone git@github.com:cherifGsoul/php-algerian-mobile-phone-number-doctrine.git
76+
```
77+
78+
and enter to the cloned repository directory.
79+
80+
- Install dependencies:
81+
82+
```shell
83+
$ composer install
84+
```
85+
86+
### Testing:
87+
Run composer script for testing:
88+
89+
```shell
90+
$ ./bin/phpunit
91+
```
92+
93+
## License
94+
95+
[MIT License](LICENSE).
96+
97+
98+

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "cherif/php-algerian-mobile-phone-number-doctrine",
3+
"description": "Use cherif/algerian-mobile-phone-number with doctrine",
4+
"authors": [
5+
{
6+
"name": "Cherif BOUCHELAGHEM",
7+
"email": "cherif.bouchelaghem@gmail.com"
8+
}
9+
],
10+
"require": {
11+
"php": ">=7.2.0",
12+
"cherif/algerian-mobile-phone-number": "^0.5.0",
13+
"doctrine/dbal": "^2.10"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Cherif\\AlgerianMobilePhoneNumber\\Doctrine\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Cherif\\AlgerianMobilePhoneNumber\\Doctrine\\": "tests/"
23+
}
24+
},
25+
"config": {
26+
"bin-dir": "bin",
27+
"sort-packages": true
28+
},
29+
"scripts": {
30+
},
31+
"require-dev": {
32+
"phpspec/prophecy-phpunit": "^2.0",
33+
"phpunit/phpunit": "^9.1"
34+
}
35+
}

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" colors="true" backupGlobals="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="cherif/php-algerian-mobile-phone-number-doctrine unit tests">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

src/AlgerianMobilePhoneNumberType.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Cherif\AlgerianMobilePhoneNumber\Doctrine;
4+
5+
use Cherif\AlgerianMobilePhoneNumber\AlgerianMobilePhoneNumber;
6+
use Cherif\AlgerianMobilePhoneNumber\InvalidAlgerianMobilePhoneNumberException;
7+
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Types\ConversionException;
9+
use Doctrine\DBAL\Types\Type;
10+
11+
class AlgerianMobilePhoneNumberType extends Type
12+
{
13+
const NAME = 'algerian_mobile_phone_number';
14+
15+
public function getSQLDeclaration(array $column, AbstractPlatform $platform)
16+
{
17+
return $platform->getVarcharTypeDeclarationSQL($column);
18+
}
19+
20+
public function convertToPHPValue($value, AbstractPlatform $platform)
21+
{
22+
if ($value === null || $value === '') {
23+
return null;
24+
}
25+
26+
if ($value instanceof AlgerianMobilePhoneNumber) {
27+
return $value;
28+
}
29+
30+
try {
31+
$mobileNumber = AlgerianMobilePhoneNumber::fromString($value);
32+
} catch (InvalidAlgerianMobilePhoneNumberException $e) {
33+
throw ConversionException::conversionFailed($value, self::NAME);
34+
35+
}
36+
37+
return $mobileNumber;
38+
}
39+
40+
public function convertToDatabaseValue($value, AbstractPlatform $platform)
41+
{
42+
if ($value === null || $value === '') {
43+
return null;
44+
}
45+
46+
if (is_string($value)) {
47+
try {
48+
$value = AlgerianMobilePhoneNumber::fromString($value);
49+
} catch (InvalidAlgerianMobilePhoneNumberException $e) {
50+
throw ConversionException::conversionFailed($value, static::NAME);
51+
}
52+
}
53+
54+
return (string) $value;
55+
}
56+
57+
public function getName()
58+
{
59+
return self::NAME;
60+
}
61+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Cherif\AlgerianMobilePhoneNumber\Doctrine;
4+
5+
use Cherif\AlgerianMobilePhoneNumber\AlgerianMobilePhoneNumber;
6+
use Doctrine\DBAL\Platforms\AbstractPlatform;
7+
use Doctrine\DBAL\Types\ConversionException;
8+
use Doctrine\DBAL\Types\Type;
9+
use PHPUnit\Framework\TestCase;
10+
use Prophecy\Argument;
11+
use Prophecy\PhpUnit\ProphecyTrait;
12+
13+
class AlgerianMobilePhoneNumberTypeTest extends TestCase
14+
{
15+
use ProphecyTrait;
16+
17+
private $platform;
18+
private $type;
19+
20+
public static function setUpBeforeClass(): void
21+
{
22+
if (class_exists(Type::class)) {
23+
Type::addType('algerian_mobile_phone_number', AlgerianMobilePhoneNumberType::class);
24+
}
25+
}
26+
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->platform = $this->prophesize(AbstractPlatform::class);
32+
$this->type = Type::getType('algerian_mobile_phone_number');
33+
}
34+
35+
36+
public function testGetSQLDeclaration()
37+
{
38+
$this->platform->getVarcharTypeDeclarationSQL(Argument::cetera())->willReturn('phone_number');
39+
$this->assertEquals('phone_number', $this->type->getSQLDeclaration([], $this->platform->reveal()));
40+
}
41+
42+
public function testConvertToPHPValue()
43+
{
44+
$mobileNumber = $this->type->convertToPHPValue('00213-6-99-00-00-00', $this->platform->reveal());
45+
$normalized = '00213699000000';
46+
$this->assertInstanceOf(AlgerianMobilePhoneNumber::class, $mobileNumber);
47+
$this->assertEquals($normalized, $mobileNumber->asString());
48+
}
49+
50+
public function testConvertToPHPValueWithNullValue()
51+
{
52+
$mobileNumber = $this->type->convertToPHPValue(null, $this->platform->reveal());
53+
$this->assertNull($mobileNumber);
54+
}
55+
56+
public function testConvertToPHPValueWithValueObjectInstance()
57+
{
58+
$value = AlgerianMobilePhoneNumber::fromString('00213699000000');
59+
$converted = $this->type->convertToPHPValue($value, $this->platform->reveal());
60+
$this->assertSame($value, $converted);
61+
}
62+
63+
public function testConvertToDatabaseValue()
64+
{
65+
$mobileNumber = AlgerianMobilePhoneNumber::fromString('00213699000000');
66+
$actual = $this->type->convertToDatabaseValue($mobileNumber, $this->platform->reveal());
67+
$this->assertEquals($mobileNumber->asString(), $actual);
68+
}
69+
70+
public function testConvertToDatabaseValueWithNullValue()
71+
{
72+
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform->reveal()));
73+
}
74+
75+
public function testInvalidConversionToDatabaseValue()
76+
{
77+
$this->expectException(ConversionException::class);
78+
$this->type->convertToDatabaseValue('foobarbaz', $this->platform->reveal());
79+
}
80+
81+
82+
public function testGetName()
83+
{
84+
$type = Type::getType('algerian_mobile_phone_number');
85+
$this->assertEquals('algerian_mobile_phone_number', $type->getName());
86+
}
87+
}

0 commit comments

Comments
 (0)