Skip to content

Commit b57b64a

Browse files
committed
First version
0 parents  commit b57b64a

15 files changed

+576
-0
lines changed

.gitattributes

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.gitattributes export-ignore
2+
.gitignore export-ignore
3+
.github/ export-ignore
4+
phpunit.xml export-ignore
5+
phpunit-bootstrap.php export-ignore
6+
psalm.xml export-ignore
7+
tests/ export-ignore

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: BenMorel

.github/workflows/ci.yml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
psalm:
9+
name: Psalm
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: "8.0"
20+
21+
- name: Install composer dependencies
22+
uses: "ramsey/composer-install@v1"
23+
24+
- name: Run Psalm
25+
run: vendor/bin/psalm --show-info=false --find-unused-psalm-suppress --no-progress
26+
27+
phpunit:
28+
name: PHPUnit
29+
runs-on: ubuntu-latest
30+
31+
strategy:
32+
matrix:
33+
php-version:
34+
- "7.1"
35+
- "7.2"
36+
- "7.3"
37+
- "7.4"
38+
- "8.0"
39+
deps:
40+
- "highest"
41+
include:
42+
- php-version: "7.1"
43+
deps: "lowest"
44+
45+
steps:
46+
- name: Checkout
47+
uses: actions/checkout@v2
48+
49+
- name: Setup PHP
50+
uses: shivammathur/setup-php@v2
51+
with:
52+
php-version: ${{ matrix.php-version }}
53+
coverage: xdebug
54+
55+
- name: Install composer dependencies
56+
uses: "ramsey/composer-install@v1"
57+
with:
58+
dependency-versions: ${{ matrix.deps }}
59+
60+
- name: Run PHPUnit
61+
run: vendor/bin/phpunit
62+
if: ${{ matrix.php-version != '8.0' }}
63+
64+
- name: Run PHPUnit with coverage
65+
run: |
66+
mkdir -p mkdir -p build/logs
67+
vendor/bin/phpunit --coverage-clover build/logs/clover.xml
68+
if: ${{ matrix.php-version == '8.0' }}
69+
70+
- name: Upload coverage report to Coveralls
71+
run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
72+
env:
73+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74+
if: ${{ matrix.php-version == '8.0' }}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/composer.lock
3+
/.phpunit.result.cache

LICENSE

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

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
brick/phonenumber-doctrine
2+
========================
3+
4+
<img src="https://raw.githubusercontent.com/brick/brick/master/logo.png" alt="" align="left" height="64">
5+
6+
Doctrine type mappings for [brick/phonenumber](https://github.com/brick/phonenumber).
7+
8+
[![Build Status](https://github.com/brick/phonenumber-doctrine/workflows/CI/badge.svg)](https://github.com/brick/phonenumber-doctrine/actions)
9+
[![Coverage Status](https://coveralls.io/repos/github/brick/phonenumber-doctrine/badge.svg?branch=master)](https://coveralls.io/github/brick/phonenumber-doctrine?branch=master)
10+
[![Latest Stable Version](https://poser.pugx.org/brick/phonenumber-doctrine/v/stable)](https://packagist.org/packages/brick/phonenumber-doctrine)
11+
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT)
12+
13+
Introduction
14+
------------
15+
16+
This library provides a type mapping to use `PhoneNumber` objects as Doctrine entity properties.
17+
18+
Installation
19+
------------
20+
21+
This library is installable via [Composer](https://getcomposer.org/):
22+
23+
```bash
24+
composer require brick/phonenumber-doctrine
25+
```
26+
27+
Requirements
28+
------------
29+
30+
This library requires PHP 7.1 or later.
31+
32+
Project status & release process
33+
--------------------------------
34+
35+
The current releases are numbered `0.x.y`. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), `y` is incremented.
36+
37+
**When a breaking change is introduced, a new `0.x` version cycle is always started.**
38+
39+
It is therefore safe to lock your project to a given release cycle, such as `0.1.*`.
40+
41+
If you need to upgrade to a newer release cycle, check the [release history](https://github.com/brick/phonenumber-doctrine/releases) for a list of changes introduced by each further `0.x.0` version.
42+
43+
Package contents
44+
----------------
45+
46+
- [PhoneNumberType](https://github.com/brick/phonenumber-doctrine/blob/master/src/Types/PhoneNumberType.php)

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "brick/phonenumber-doctrine",
3+
"description": "Doctrine type mappings for brick/phonenumber",
4+
"type": "library",
5+
"keywords": [
6+
"Brick",
7+
"PhoneNumber",
8+
"Doctrine"
9+
],
10+
"license": "MIT",
11+
"require": {
12+
"brick/phonenumber": "~0.2.0 || ~0.3.0",
13+
"doctrine/dbal": "^2.7.0",
14+
"doctrine/orm": "^2.6.0",
15+
"php": "^7.1 || ^8.0"
16+
},
17+
"require-dev": {
18+
"ext-pdo": "*",
19+
"ext-pdo_sqlite": "*",
20+
"phpunit/phpunit": "^7.5.15 || ^8.0 || ^9.0",
21+
"php-coveralls/php-coveralls": "^2.4",
22+
"vimeo/psalm": "^4.9"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Brick\\PhoneNumber\\Doctrine\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"Brick\\PhoneNumber\\Doctrine\\Tests\\": "tests/"
32+
}
33+
}
34+
}

phpunit-bootstrap.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Brick\PhoneNumber\Doctrine\Types\PhoneNumberType;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
require __DIR__ . '/vendor/autoload.php';
9+
10+
Type::addType('PhoneNumber', PhoneNumberType::class);

phpunit.xml

+13
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" colors="true" bootstrap="phpunit-bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<testsuites>
4+
<testsuite name="PhoneNumber Doctrine tests">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<coverage processUncoveredFiles="true">
9+
<include>
10+
<directory suffix=".php">src</directory>
11+
</include>
12+
</coverage>
13+
</phpunit>

psalm.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="1"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
</projectFiles>
15+
</psalm>

src/Types/PhoneNumberType.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brick\PhoneNumber\Doctrine\Types;
6+
7+
use Brick\PhoneNumber\PhoneNumber;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
9+
use Doctrine\DBAL\Types\ConversionException;
10+
use Doctrine\DBAL\Types\Type;
11+
12+
final class PhoneNumberType extends Type
13+
{
14+
public function getName()
15+
{
16+
return 'PhoneNumber';
17+
}
18+
19+
public function convertToDatabaseValue($value, AbstractPlatform $platform)
20+
{
21+
if ($value === null) {
22+
return null;
23+
}
24+
25+
if ($value instanceof PhoneNumber) {
26+
return (string) $value;
27+
}
28+
29+
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), [PhoneNumber::class, 'null']);
30+
}
31+
32+
public function convertToPHPValue($value, AbstractPlatform $platform)
33+
{
34+
if ($value === null) {
35+
return null;
36+
}
37+
38+
if (is_string($value)) {
39+
return PhoneNumber::parse($value);
40+
}
41+
42+
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['string', 'null']);
43+
}
44+
45+
public function getSQLDeclaration(array $column, AbstractPlatform $platform)
46+
{
47+
// E.164 defines the maximum length as 15 digits, to which we add 1 char for the leading + sign.
48+
if (!isset($column['length'])) {
49+
$column['length'] = 16;
50+
}
51+
52+
return $platform->getVarcharTypeDeclarationSQL($column);
53+
}
54+
55+
public function requiresSQLCommentHint(AbstractPlatform $platform)
56+
{
57+
return true;
58+
}
59+
}

tests/AbstractFunctionalTest.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brick\PhoneNumber\Doctrine\Tests;
6+
7+
use Brick\PhoneNumber\Doctrine\Tests\Entity\User;
8+
use Doctrine\DBAL\Connection;
9+
use Doctrine\DBAL\DriverManager;
10+
use Doctrine\ORM\Configuration;
11+
use Doctrine\ORM\EntityManager;
12+
use PHPUnit\Framework\TestCase;
13+
14+
abstract class AbstractFunctionalTest extends TestCase
15+
{
16+
final protected static function createConnection(): Connection
17+
{
18+
return DriverManager::getConnection(['url' => 'sqlite:///:memory:']);
19+
}
20+
21+
final protected static function createEntityManager(Connection $connection): EntityManager
22+
{
23+
return EntityManager::create($connection, self::createConfiguration());
24+
}
25+
26+
private static function createConfiguration(): Configuration
27+
{
28+
$config = new Configuration();
29+
30+
$driverImpl = $config->newDefaultAnnotationDriver([__DIR__ . '/tests/Entity'], false);
31+
$config->setMetadataDriverImpl($driverImpl);
32+
33+
$config->setProxyDir(sys_get_temp_dir());
34+
$config->setProxyNamespace('Brick\PhoneNumber\Doctrine\Tests\Proxy');
35+
36+
return $config;
37+
}
38+
39+
final protected static function truncateEntityTable(EntityManager $em): void
40+
{
41+
$em->createQueryBuilder()
42+
->delete(User::class, 's')
43+
->getQuery()
44+
->execute();
45+
}
46+
47+
final protected function getFirstEntity(EntityManager $em): ?User
48+
{
49+
return $em->createQueryBuilder()
50+
->select('s')
51+
->from(User::class, 's')
52+
->getQuery()
53+
->getOneOrNullResult();
54+
}
55+
}

tests/Entity/User.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brick\PhoneNumber\Doctrine\Tests\Entity;
6+
7+
use Brick\PhoneNumber\PhoneNumber;
8+
use Doctrine\ORM\Mapping as ORM;
9+
10+
/**
11+
* @ORM\Entity
12+
*/
13+
class User
14+
{
15+
/**
16+
* @ORM\Id
17+
* @ORM\Column(type="integer")
18+
* @ORM\GeneratedValue
19+
*
20+
* @var int
21+
*/
22+
public $id;
23+
24+
/**
25+
* @ORM\Column(type="PhoneNumber", nullable=true)
26+
*
27+
* @var PhoneNumber|null
28+
*/
29+
public $phoneNumber = null;
30+
}

0 commit comments

Comments
 (0)