Skip to content
This repository was archived by the owner on May 14, 2022. It is now read-only.

Commit 8f5f94f

Browse files
committed
Better phone-number-parsing
Fixes #3
1 parent 3d5d0a4 commit 8f5f94f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/Zyberspace/Telegram/Cli/AbstractClientCommands.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ public function msg($peer, $msg)
6363
/**
6464
* Adds a user to the contact list
6565
*
66-
* @param int|string $phoneNumber The phone-number of the new contact, needs to be a telegram-user.
67-
* Can start with or without '+'.
66+
* @param string $phoneNumber The phone-number of the new contact, needs to be a telegram-user.
67+
* Every char that is not a number gets deleted, so you don't need to care about spaces,
68+
* '+' and so on.
6869
* @param string $firstName The first name of the new contact
6970
* @param string $lastName The last name of the new contact
7071
*
@@ -75,10 +76,10 @@ public function msg($peer, $msg)
7576
*/
7677
public function addContact($phoneNumber, $firstName, $lastName)
7778
{
78-
if (is_string($phoneNumber) && $phoneNumber[0] === '+') {
79-
$phoneNumber = substr($phoneNumber, 1);
79+
$phoneNumber = preg_replace('%[^0-9]%', '', (string) $phoneNumber);
80+
if (empty($phoneNumber)) {
81+
return false;
8082
}
81-
$phoneNumber = (int) $phoneNumber;
8283

8384
return $this->exec('add_contact ' . $phoneNumber . ' ' . $this->escapeStringArgument($firstName)
8485
. ' ' . $this->escapeStringArgument($lastName));

0 commit comments

Comments
 (0)