forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressService.php
144 lines (125 loc) · 4.29 KB
/
AddressService.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
144
<?php
/**
* AddressService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Matthew Vita <[email protected]>
* @author Brady Miller <[email protected]>
* @copyright Copyright (c) 2018 Matthew Vita <[email protected]>
* @copyright Copyright (c) 2018 Brady Miller <[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 AddressService extends BaseService
{
public function __construct()
{
}
public function validate($insuranceCompany)
{
$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($insuranceCompany);
}
public function getAddressFromRecordAsString(array $addressRecord)
{
// works for patients and users
$address = [];
if (!empty($addressRecord['street'])) {
$address[] = $addressRecord['street'];
$address[] = "\n";
}
if (!empty($addressRecord['city'])) {
$address[] = $addressRecord['city'];
$address[] = ", ";
}
if (!empty($addressRecord['state'])) {
$address[] = $addressRecord['state'];
$address[] = " ";
}
if (!empty($addressRecord['postal_code'])) {
$address[] = $addressRecord['postal_code'];
$address[] = " ";
}
if (!empty($addressRecord['country_code'])) {
$address[] = $addressRecord['country_code'];
}
return implode("", $address);
}
public function insert($data, $foreignId)
{
$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=?";
$addressesSqlResults = QueryUtils::sqlInsert(
$addressesSql,
array(
$freshId,
$data["line1"],
$data["line2"],
$data["city"],
$data["state"],
$data["zip"],
$data["plus_four"] ?? null,
$data["country"],
$foreignId
)
);
if (!$addressesSqlResults) {
return false;
}
return $freshId;
}
public function update($data, $foreignId)
{
$addressesSql = " UPDATE addresses SET";
$addressesSql .= " line1=?,";
$addressesSql .= " line2=?,";
$addressesSql .= " city=?,";
$addressesSql .= " state=?,";
$addressesSql .= " zip=?,";
$addressesSql .= " plus_four=?,";
$addressesSql .= " country=?";
$addressesSql .= " WHERE foreign_id=?";
$addressesSqlResults = sqlStatement(
$addressesSql,
array(
$data["line1"],
$data["line2"],
$data["city"],
$data["state"],
$data["zip"],
$data["plus_four"] ?? null,
$data["country"],
$foreignId
)
);
if (!$addressesSqlResults) {
return false;
}
$addressIdSqlResults = sqlQuery("SELECT id FROM addresses WHERE foreign_id=?", $foreignId);
return $addressIdSqlResults["id"];
}
public function getOneByForeignId($foreignId)
{
$sql = "SELECT * FROM addresses WHERE foreign_id=?";
return sqlQuery($sql, array($foreignId));
}
}