updated US CDC website link to current immunization VIS page (#7855)
[openemr.git] / src / Services / PhoneNumberService.php
blob832e1aed69c8271f87f24a78e922c482fd016ca4
1 <?php
3 /**
4 * PhoneService
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Stephen Waite <stephen.waite@cmsvt.com>
9 * @author Stephen Nielson <snielson@discoverandchange.com>
10 * @copyright Copyright (c) 2023 Stephen Waite <stephen.waite@cmsvt.com>
11 * @copyright Copyright (c) 2024 Care Management Solutions, Inc. <stephen.waite@cmsvt.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 namespace OpenEMR\Services;
17 use OpenEMR\Common\Database\QueryUtils;
18 use Particle\Validator\Validator;
20 class PhoneNumberService extends BaseService
22 public const COUNTRY_CODE = "+1";
23 public string $area_code;
24 public string $prefix;
25 public string $number;
26 public int $type;
27 private int $foreignId;
29 public function __construct()
31 $this->area_code = $area_code ?? '';
32 $this->prefix = $prefix ?? '';
33 $this->number = $number ?? '';
34 $this->type = $type ?? 2;
35 $this->foreignId = $foreignId ?? 0;
38 public function validate($phoneNumber)
40 $validator = new Validator();
42 $validator->optional('country_code')->lengthBetween(1, 5);
43 $validator->optional('area_code')->lengthBetween(1, 3);
44 $validator->optional('prefix')->lengthBetween(1, 3);
45 $validator->optional('number')->lengthBetween(1, 4);
46 $validator->optional('type')->lengthBetween(1, 11);
47 $validator->optional('foreign_id')->lengthBetween(1, 11);
49 return $validator->validate($phoneNumber);
52 public function insert($data, $foreignId)
54 $freshId = $this->getFreshId("id", "phone_numbers");
55 $this->foreignId = $foreignId;
57 $this->getPhoneParts($data['phone']);
59 $phoneNumbersSql = " INSERT INTO phone_numbers SET";
60 $phoneNumbersSql .= " id=?,";
61 $phoneNumbersSql .= " country_code=?,";
62 $phoneNumbersSql .= " area_code=?,";
63 $phoneNumbersSql .= " prefix=?,";
64 $phoneNumbersSql .= " number=?,";
65 $phoneNumbersSql .= " type=?,";
66 $phoneNumbersSql .= " foreign_id=?";
68 $phoneNumbersSqlResults = QueryUtils::sqlInsert(
69 $phoneNumbersSql,
70 array(
71 $freshId,
72 self::COUNTRY_CODE,
73 $this->area_code,
74 $this->prefix,
75 $this->number,
76 $this->type,
77 $this->foreignId
81 if (!$phoneNumbersSqlResults) {
82 return false;
85 return $freshId;
88 public function update($data, $foreignId)
90 $this->foreignId = $foreignId;
91 $this->getPhoneParts($data['phone']);
93 $phoneNumbersSql = " UPDATE phone_numbers SET";
94 $phoneNumbersSql .= " country_code=?,";
95 $phoneNumbersSql .= " area_code=?,";
96 $phoneNumbersSql .= " prefix=?,";
97 $phoneNumbersSql .= " number=? ";
98 $phoneNumbersSql .= " WHERE foreign_id=? AND type=?";
100 $phoneNumbersSqlResults = sqlStatement(
101 $phoneNumbersSql,
102 array(
103 self::COUNTRY_CODE,
104 $this->area_code ,
105 $this->prefix,
106 $this->number,
107 $this->foreignId,
108 $this->type
112 if (!$phoneNumbersSqlResults) {
113 return false;
116 $phoneNumbersIdSqlResults = sqlQuery("SELECT id FROM phone_numbers WHERE foreign_id=?", $this->foreignId);
118 if (empty($phoneNumbersIdSqlResults)) {
119 $this->insert($data, $foreignId);
121 return $phoneNumbersIdSqlResults["id"] ?? null;
124 public function getPhoneParts(string $phone_number)
126 $phone_parts = array();
127 preg_match(
128 "/(\d\d\d)\D*(\d\d\d)\D*(\d\d\d\d)/",
129 $phone_number,
130 $phone_parts
133 $this->area_code = $phone_parts[1] ?? '';
134 $this->prefix = $phone_parts[2] ?? '';
135 $this->number = $phone_parts[3] ?? '';
138 public function getOneByForeignId($foreignId)
140 $sql = "SELECT * FROM phone_numbers WHERE foreign_id=?";
141 return sqlQuery($sql, array($foreignId));