Skip to content

Commit 30b366f

Browse files
committed
fix: wrapping up UserData as object
1 parent 194df89 commit 30b366f

File tree

1 file changed

+137
-21
lines changed

1 file changed

+137
-21
lines changed

src/Helper/UserDataHelper.php

Lines changed: 137 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44

55
class UserDataHelper
66
{
7-
private array $sha256_email_address = [];
8-
private array $sha256_phone_number = [];
7+
private ?string $sha256_email_address = null;
8+
private ?string $sha256_phone_number = null;
9+
10+
private ?string $sha256_first_name = null;
11+
private ?string $sha256_last_name = null;
12+
private ?string $sha256_street = null;
13+
private ?string $city = null;
14+
private ?string $region = null;
15+
private ?string $postal_code = null;
16+
private ?string $country = null;
917

1018
/**
1119
* @param string $email Valid email format
12-
* @return int Cursor of array or -1 for error
20+
* @return int Cursor of array or -1 for invalid
1321
*/
14-
public function addEmail(string $email): int
22+
public function setEmail(string $email): bool
1523
{
1624
// Sanitize & Validate email
1725
$email = str_replace(" ", "", mb_strtolower($email));
18-
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return -1; // Invalid email format
26+
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return false; // Invalid email format
1927

2028
// Google Mail Sanitizer
2129
// https://support.google.com/mail/answer/7436150
@@ -28,38 +36,146 @@ public function addEmail(string $email): int
2836
// https://support.google.com/mail/thread/125577450/gmail-and-googlemail?hl=en
2937
$x[1] = "gmail.com";
3038
}
31-
$x[0] = explode("+", $x[0], 2)[0]; // https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html
39+
// https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html
40+
$x[0] = explode("+", $x[0], 2)[0];
3241
$x[0] = str_replace(".", "", $x[0]);
3342
$email = implode("@", $x);
3443
}
3544

3645
// Sha256 encode
37-
$email = hash("sha256", $email);
38-
39-
// Skip duplicated and append email
40-
$this->sha256_email_address = array_filter($this->sha256_email_address, fn ($v) => $v != $email);
41-
return array_push($this->sha256_email_address, $email) - 1;
46+
$this->sha256_email_address = hash("sha256", $email);
47+
return true;
4248
}
4349

4450
/**
45-
* This converst prefix and number into international approved numbers
4651
* @param int $number fully international number (without dashes or plus) eg. \
4752
* "+1-123-4567890" for USA or\
4853
* "+44-1234-5678900" for UK or\
4954
* "+45-12345678" for DK
50-
* @return int Cursor of array or -1 for error
55+
* @return boolean
5156
*/
52-
public function addPhone(int $number): int
57+
public function setPhone(int $number): bool
58+
{
59+
$sNumber = strval($number);
60+
if (strlen($sNumber) < 3 || strlen($sNumber) > 15) {
61+
return false;
62+
}
63+
64+
$this->sha256_phone_number = hash("sha256", "+{$sNumber}");
65+
return true;
66+
}
67+
68+
public function setFirstName(string $firstName): bool
69+
{
70+
if (empty($firstName)) return false;
71+
$this->sha256_first_name = hash("sha256", $this->strip($firstName, true));
72+
return true;
73+
}
74+
75+
public function setLastName(string $lastName): bool
76+
{
77+
if (empty($lastName)) return false;
78+
$this->sha256_last_name = hash("sha256", $this->strip($lastName, true));
79+
return true;
80+
}
81+
82+
public function setStreet(string $street): bool
83+
{
84+
if (empty($street)) return false;
85+
$this->sha256_street = hash("sha256", $this->strip($street));
86+
return true;
87+
}
88+
89+
public function setCity(string $city): bool
90+
{
91+
if (empty($city)) return false;
92+
$this->city = $this->strip($city, true);
93+
return true;
94+
}
95+
96+
public function setRegion(string $region): bool
97+
{
98+
if (empty($region)) return false;
99+
$this->region = $this->strip($region, true);
100+
return true;
101+
}
102+
103+
public function setPostalCode(string $postalCode): bool
104+
{
105+
if (empty($postalCode)) return false;
106+
$this->postal_code = $this->strip($postalCode);
107+
return true;
108+
}
109+
110+
public function setCountry(string $iso): bool
53111
{
54-
$sNumber = (string)$number;
55-
if (strlen($sNumber) < 1 || strlen($sNumber) > 15) {
56-
return -1;
112+
if (!CountryIsoHelper::valid($iso)) {
113+
return false;
57114
}
58115

59-
$sNumber = hash("sha256", "+{$sNumber}");
116+
$this->country = mb_strtoupper(trim($iso));
117+
return false;
118+
}
119+
120+
public function toArray(): array
121+
{
122+
$res = [];
123+
124+
if (!empty($this->sha256_email_address)) {
125+
$res["sha256_email_address"] = $this->sha256_email_address;
126+
}
127+
128+
if (!empty($this->sha256_phone_number)) {
129+
$res["sha256_phone_number"] = $this->sha256_phone_number;
130+
}
131+
132+
$addr = [];
133+
134+
if (!empty($this->sha256_first_name)) {
135+
$addr["sha256_first_name"] = $this->sha256_first_name;
136+
}
137+
138+
if (!empty($this->sha256_last_name)) {
139+
$addr["sha256_last_name"] = $this->sha256_last_name;
140+
}
141+
142+
if (!empty($this->sha256_street)) {
143+
$addr["sha256_street"] = $this->sha256_street;
144+
}
145+
146+
if (!empty($this->city)) {
147+
$addr["city"] = $this->city;
148+
}
149+
150+
if (!empty($this->region)) {
151+
$addr["region"] = $this->region;
152+
}
153+
154+
if (!empty($this->postal_code)) {
155+
$addr["postal_code"] = $this->postal_code;
156+
}
157+
158+
if (!empty($this->country)) {
159+
$addr["country"] = $this->country;
160+
}
161+
162+
if (!empty($this->sha256_phone_number)) {
163+
$res["sha256_phone_number"] = $this->sha256_phone_number;
164+
}
165+
166+
if (count($addr) > 0) {
167+
$res["address"] = $addr;
168+
}
169+
170+
return $res;
171+
}
172+
173+
private function strip(string $s, bool $removeDigits = false): string
174+
{
175+
$d = $removeDigits ? '0-9' : '';
60176

61-
// Skip duplicated and append phone number
62-
$this->sha256_phone_number = array_filter($this->sha256_phone_number, fn ($v) => $v != $sNumber);
63-
return array_push($this->sha256_phone_number, $sNumber) - 1;
177+
$s = preg_replace("[^a-zA-Z{$d}\-\_\.\,\s]", "", $s);
178+
$s = mb_strtolower($s);
179+
return trim($s);
64180
}
65181
}

0 commit comments

Comments
 (0)