Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Aug 5, 2021
0 parents commit b57b64a
Show file tree
Hide file tree
Showing 15 changed files with 576 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.gitattributes export-ignore
.gitignore export-ignore
.github/ export-ignore
phpunit.xml export-ignore
phpunit-bootstrap.php export-ignore
psalm.xml export-ignore
tests/ export-ignore
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: BenMorel
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: CI

on:
push:
pull_request:

jobs:
psalm:
name: Psalm
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.0"

- name: Install composer dependencies
uses: "ramsey/composer-install@v1"

- name: Run Psalm
run: vendor/bin/psalm --show-info=false --find-unused-psalm-suppress --no-progress

phpunit:
name: PHPUnit
runs-on: ubuntu-latest

strategy:
matrix:
php-version:
- "7.1"
- "7.2"
- "7.3"
- "7.4"
- "8.0"
deps:
- "highest"
include:
- php-version: "7.1"
deps: "lowest"

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: xdebug

- name: Install composer dependencies
uses: "ramsey/composer-install@v1"
with:
dependency-versions: ${{ matrix.deps }}

- name: Run PHPUnit
run: vendor/bin/phpunit
if: ${{ matrix.php-version != '8.0' }}

- name: Run PHPUnit with coverage
run: |
mkdir -p mkdir -p build/logs
vendor/bin/phpunit --coverage-clover build/logs/clover.xml
if: ${{ matrix.php-version == '8.0' }}

- name: Upload coverage report to Coveralls
run: vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: ${{ matrix.php-version == '8.0' }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
/composer.lock
/.phpunit.result.cache
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2021-present Benjamin Morel

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
brick/phonenumber-doctrine
========================

<img src="https://raw.githubusercontent.com/brick/brick/master/logo.png" alt="" align="left" height="64">

Doctrine type mappings for [brick/phonenumber](https://github.com/brick/phonenumber).

[![Build Status](https://github.com/brick/phonenumber-doctrine/workflows/CI/badge.svg)](https://github.com/brick/phonenumber-doctrine/actions)
[![Coverage Status](https://coveralls.io/repos/github/brick/phonenumber-doctrine/badge.svg?branch=master)](https://coveralls.io/github/brick/phonenumber-doctrine?branch=master)
[![Latest Stable Version](https://poser.pugx.org/brick/phonenumber-doctrine/v/stable)](https://packagist.org/packages/brick/phonenumber-doctrine)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT)

Introduction
------------

This library provides a type mapping to use `PhoneNumber` objects as Doctrine entity properties.

Installation
------------

This library is installable via [Composer](https://getcomposer.org/):

```bash
composer require brick/phonenumber-doctrine
```

Requirements
------------

This library requires PHP 7.1 or later.

Project status & release process
--------------------------------

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.

**When a breaking change is introduced, a new `0.x` version cycle is always started.**

It is therefore safe to lock your project to a given release cycle, such as `0.1.*`.

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.

Package contents
----------------

- [PhoneNumberType](https://github.com/brick/phonenumber-doctrine/blob/master/src/Types/PhoneNumberType.php)
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "brick/phonenumber-doctrine",
"description": "Doctrine type mappings for brick/phonenumber",
"type": "library",
"keywords": [
"Brick",
"PhoneNumber",
"Doctrine"
],
"license": "MIT",
"require": {
"brick/phonenumber": "~0.2.0 || ~0.3.0",
"doctrine/dbal": "^2.7.0",
"doctrine/orm": "^2.6.0",
"php": "^7.1 || ^8.0"
},
"require-dev": {
"ext-pdo": "*",
"ext-pdo_sqlite": "*",
"phpunit/phpunit": "^7.5.15 || ^8.0 || ^9.0",
"php-coveralls/php-coveralls": "^2.4",
"vimeo/psalm": "^4.9"
},
"autoload": {
"psr-4": {
"Brick\\PhoneNumber\\Doctrine\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Brick\\PhoneNumber\\Doctrine\\Tests\\": "tests/"
}
}
}
10 changes: 10 additions & 0 deletions phpunit-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Brick\PhoneNumber\Doctrine\Types\PhoneNumberType;
use Doctrine\DBAL\Types\Type;

require __DIR__ . '/vendor/autoload.php';

Type::addType('PhoneNumber', PhoneNumberType::class);
13 changes: 13 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<testsuites>
<testsuite name="PhoneNumber Doctrine tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
59 changes: 59 additions & 0 deletions src/Types/PhoneNumberType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Brick\PhoneNumber\Doctrine\Types;

use Brick\PhoneNumber\PhoneNumber;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;

final class PhoneNumberType extends Type
{
public function getName()
{
return 'PhoneNumber';
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}

if ($value instanceof PhoneNumber) {
return (string) $value;
}

throw ConversionException::conversionFailedInvalidType($value, $this->getName(), [PhoneNumber::class, 'null']);
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}

if (is_string($value)) {
return PhoneNumber::parse($value);
}

throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['string', 'null']);
}

public function getSQLDeclaration(array $column, AbstractPlatform $platform)
{
// E.164 defines the maximum length as 15 digits, to which we add 1 char for the leading + sign.
if (!isset($column['length'])) {
$column['length'] = 16;
}

return $platform->getVarcharTypeDeclarationSQL($column);
}

public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
}
55 changes: 55 additions & 0 deletions tests/AbstractFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Brick\PhoneNumber\Doctrine\Tests;

use Brick\PhoneNumber\Doctrine\Tests\Entity\User;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\TestCase;

abstract class AbstractFunctionalTest extends TestCase
{
final protected static function createConnection(): Connection
{
return DriverManager::getConnection(['url' => 'sqlite:///:memory:']);
}

final protected static function createEntityManager(Connection $connection): EntityManager
{
return EntityManager::create($connection, self::createConfiguration());
}

private static function createConfiguration(): Configuration
{
$config = new Configuration();

$driverImpl = $config->newDefaultAnnotationDriver([__DIR__ . '/tests/Entity'], false);
$config->setMetadataDriverImpl($driverImpl);

$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('Brick\PhoneNumber\Doctrine\Tests\Proxy');

return $config;
}

final protected static function truncateEntityTable(EntityManager $em): void
{
$em->createQueryBuilder()
->delete(User::class, 's')
->getQuery()
->execute();
}

final protected function getFirstEntity(EntityManager $em): ?User
{
return $em->createQueryBuilder()
->select('s')
->from(User::class, 's')
->getQuery()
->getOneOrNullResult();
}
}
30 changes: 30 additions & 0 deletions tests/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Brick\PhoneNumber\Doctrine\Tests\Entity;

use Brick\PhoneNumber\PhoneNumber;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int
*/
public $id;

/**
* @ORM\Column(type="PhoneNumber", nullable=true)
*
* @var PhoneNumber|null
*/
public $phoneNumber = null;
}
Loading

0 comments on commit b57b64a

Please sign in to comment.