Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a doctrine orm query function for phone number #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
sudo: false

dist: trusty

language: php

php:
Expand Down
120 changes: 120 additions & 0 deletions Doctrine/ORM/DQL/Functions/Phone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Misd\PhoneNumberBundle\Doctrine\ORM\DQL\Functions;

use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;

/**
* Class Phone
* @package Misd\PhoneNumberBundle\Doctrine\ORM\DQL\Functions
*/
class Phone extends FunctionNode
{
/**
* @var PhoneNumber
*/
public $phoneNumber;

/**
* @param \Doctrine\ORM\Query\SqlWalker $sqlWalker
* @return string
*/
public function getSql(SqlWalker $sqlWalker)
{
$phoneUtil = PhoneNumberUtil::getInstance();

return "'{$phoneUtil->format($this->phoneNumber, PhoneNumberFormat::E164)}'";
}

/**
* @param \Doctrine\ORM\Query\Parser $parser
*/
public function parse(Parser $parser)
{
$phoneNumber = new PhoneNumber();
$lexer = $parser->getLexer();
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_INPUT_PARAMETER);
$phoneNumber->setCountryCode($lexer->lookahead['value']);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_INPUT_PARAMETER);
$phoneNumber->setNationalNumber($lexer->lookahead['value']);
$parser->match(Lexer::T_IDENTIFIER);

if (
$lexer->lookahead['type'] == Lexer::T_LEADING &&
$lexer->lookahead['value'] == 'Leading'
) {
$phoneNumber->setItalianLeadingZero(true);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$parser->match(Lexer::T_INPUT_PARAMETER);
$parser->match(Lexer::T_TRUE);
}

if (
$lexer->lookahead['type'] == Lexer::T_IDENTIFIER &&
$lexer->lookahead['value'] == 'Number'
) {
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OF);
$parser->match(Lexer::T_LEADING);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_INPUT_PARAMETER);
$phoneNumber->setNumberOfLeadingZeros($lexer->lookahead['value']);
$parser->match(Lexer::T_INTEGER);
}

if (
$lexer->lookahead['type'] == Lexer::T_IDENTIFIER &&
$lexer->lookahead['value'] == 'Extension'
) {
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_INPUT_PARAMETER);
$phoneNumber->setExtension($lexer->lookahead['value']);
$parser->match(Lexer::T_INTEGER);
}

if (
$lexer->lookahead['type'] == Lexer::T_IDENTIFIER &&
$lexer->lookahead['value'] == 'Country'
) {
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_INPUT_PARAMETER);
$phoneNumber->setCountryCodeSource($lexer->lookahead['value']);
$parser->match(Lexer::T_INTEGER);
}

if (
$lexer->lookahead['type'] == Lexer::T_IDENTIFIER &&
$lexer->lookahead['value'] == 'Preferred'
) {
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_INPUT_PARAMETER);
$phoneNumber->setPreferredDomesticCarrierCode($lexer->lookahead['value']);
$parser->match(Lexer::T_STRING);
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
$this->phoneNumber = $phoneNumber;
}
}