forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAddressService.php
More file actions
150 lines (133 loc) · 4.59 KB
/
AddressService.php
File metadata and controls
150 lines (133 loc) · 4.59 KB
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
144
145
146
147
148
149
150
<?php
/**
* AddressService
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Matthew Vita <matthewvita48@gmail.com>
* @author Brady Miller <brady.g.miller@gmail.com>
* @author Michael A. Smith <michael@opencoreemr.com>
* @copyright Copyright (c) 2018 Matthew Vita <matthewvita48@gmail.com>
* @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
* @copyright Copyright (c) 2026 OpenCoreEMR Inc <https://opencoreemr.com/>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Services\Address\AddressData;
use OpenEMR\Services\Address\AddressRecord;
use Particle\Validator\ValidationResult;
use Particle\Validator\Validator;
class AddressService extends BaseService
{
public function __construct()
{
}
/**
* Validate address data against length constraints.
*/
public function validate(AddressData $address): ValidationResult
{
$validator = new Validator();
$validator->optional('line1')->lengthBetween(2, 255);
$validator->optional('line2')->lengthBetween(2, 255);
$validator->optional('city')->lengthBetween(2, 255);
$validator->optional('state')->lengthBetween(2, 35);
$validator->optional('zip')->lengthBetween(2, 10);
$validator->optional('plus_four')->lengthBetween(2, 4);
$validator->optional('country')->lengthBetween(2, 255);
return $validator->validate($address->toArray());
}
/**
* Format an address record as a multi-line string.
*/
public function getAddressFromRecordAsString(AddressRecord $addressRecord): string
{
return $addressRecord->toString();
}
/**
* Insert a new address record.
*
* @return int|false The new address ID, or false on failure
*/
public function insert(AddressData $data, int $foreignId): int|false
{
/** @var int $freshId */
$freshId = $this->getFreshId("id", "addresses");
$addressesSql = " INSERT INTO addresses SET";
$addressesSql .= " id=?,";
$addressesSql .= " line1=?,";
$addressesSql .= " line2=?,";
$addressesSql .= " city=?,";
$addressesSql .= " state=?,";
$addressesSql .= " zip=?,";
$addressesSql .= " plus_four=?,";
$addressesSql .= " country=?,";
$addressesSql .= " foreign_id=?";
// throws an exception if the record doesn't insert
QueryUtils::sqlInsert(
$addressesSql,
[
$freshId,
$data->line1,
$data->line2,
$data->city,
$data->state,
$data->zip,
$data->plusFour,
$data->country,
$foreignId
]
);
return $freshId;
}
/**
* Update an existing address record.
*
* @return int|string|null The address ID, or null if not found
*/
public function update(AddressData $data, int $foreignId): int|string|null
{
$addressesSql = " UPDATE addresses SET";
$addressesSql .= " line1=?,";
$addressesSql .= " line2=?,";
$addressesSql .= " city=?,";
$addressesSql .= " state=?,";
$addressesSql .= " zip=?,";
$addressesSql .= " plus_four=?,";
$addressesSql .= " country=?";
$addressesSql .= " WHERE foreign_id=?";
QueryUtils::sqlStatementThrowException(
$addressesSql,
[
$data->line1,
$data->line2,
$data->city,
$data->state,
$data->zip,
$data->plusFour,
$data->country,
$foreignId
]
);
/** @var array{id: int|string}|false $addressIdSqlResults */
$addressIdSqlResults = QueryUtils::querySingleRow(
"SELECT id FROM addresses WHERE foreign_id=? LIMIT 1",
[$foreignId]
);
return $addressIdSqlResults["id"] ?? null;
}
/**
* Get an address by its foreign ID.
*/
public function getOneByForeignId(int $foreignId): ?AddressData
{
$sql = "SELECT * FROM addresses WHERE foreign_id=? LIMIT 1";
/** @var array<string, mixed>|false $result */
$result = QueryUtils::querySingleRow($sql, [$foreignId]);
if ($result === false) {
return null;
}
return AddressData::fromArray($result);
}
}