forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhoneNumberService.php
143 lines (120 loc) · 4.22 KB
/
PhoneNumberService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* PhoneService
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Stephen Waite <[email protected]>
* @author Stephen Nielson <[email protected]>
* @copyright Copyright (c) 2023 Stephen Waite <[email protected]>
* @copyright Copyright (c) 2024 Care Management Solutions, Inc. <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Common\Database\QueryUtils;
use Particle\Validator\Validator;
class PhoneNumberService extends BaseService
{
public const COUNTRY_CODE = "+1";
public string $area_code;
public string $prefix;
public string $number;
public int $type;
private int $foreignId;
public function __construct()
{
$this->area_code = $area_code ?? '';
$this->prefix = $prefix ?? '';
$this->number = $number ?? '';
$this->type = $type ?? 2;
$this->foreignId = $foreignId ?? 0;
}
public function validate($phoneNumber)
{
$validator = new Validator();
$validator->optional('country_code')->lengthBetween(1, 5);
$validator->optional('area_code')->lengthBetween(1, 3);
$validator->optional('prefix')->lengthBetween(1, 3);
$validator->optional('number')->lengthBetween(1, 4);
$validator->optional('type')->lengthBetween(1, 11);
$validator->optional('foreign_id')->lengthBetween(1, 11);
return $validator->validate($phoneNumber);
}
public function insert($data, $foreignId)
{
$freshId = $this->getFreshId("id", "phone_numbers");
$this->foreignId = $foreignId;
$this->getPhoneParts($data['phone']);
$phoneNumbersSql = " INSERT INTO phone_numbers SET";
$phoneNumbersSql .= " id=?,";
$phoneNumbersSql .= " country_code=?,";
$phoneNumbersSql .= " area_code=?,";
$phoneNumbersSql .= " prefix=?,";
$phoneNumbersSql .= " number=?,";
$phoneNumbersSql .= " type=?,";
$phoneNumbersSql .= " foreign_id=?";
$phoneNumbersSqlResults = QueryUtils::sqlInsert(
$phoneNumbersSql,
array(
$freshId,
self::COUNTRY_CODE,
$this->area_code,
$this->prefix,
$this->number,
$this->type,
$this->foreignId
)
);
if (!$phoneNumbersSqlResults) {
return false;
}
return $freshId;
}
public function update($data, $foreignId)
{
$this->foreignId = $foreignId;
$this->getPhoneParts($data['phone']);
$phoneNumbersSql = " UPDATE phone_numbers SET";
$phoneNumbersSql .= " country_code=?,";
$phoneNumbersSql .= " area_code=?,";
$phoneNumbersSql .= " prefix=?,";
$phoneNumbersSql .= " number=? ";
$phoneNumbersSql .= " WHERE foreign_id=? AND type=?";
$phoneNumbersSqlResults = sqlStatement(
$phoneNumbersSql,
array(
self::COUNTRY_CODE,
$this->area_code ,
$this->prefix,
$this->number,
$this->foreignId,
$this->type
)
);
if (!$phoneNumbersSqlResults) {
return false;
}
$phoneNumbersIdSqlResults = sqlQuery("SELECT id FROM phone_numbers WHERE foreign_id=?", $this->foreignId);
if (empty($phoneNumbersIdSqlResults)) {
$this->insert($data, $foreignId);
}
return $phoneNumbersIdSqlResults["id"] ?? null;
}
public function getPhoneParts(string $phone_number)
{
$phone_parts = array();
preg_match(
"/(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/",
$phone_number,
$phone_parts
);
$this->area_code = $phone_parts[1] ?? '';
$this->prefix = $phone_parts[2] ?? '';
$this->number = $phone_parts[3] ?? '';
}
public function getOneByForeignId($foreignId)
{
$sql = "SELECT * FROM phone_numbers WHERE foreign_id=?";
return sqlQuery($sql, array($foreignId));
}
}