-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSalesforce18Id.php
More file actions
170 lines (140 loc) · 4.56 KB
/
Salesforce18Id.php
File metadata and controls
170 lines (140 loc) · 4.56 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace MatchBot\Domain;
use JsonSerializable;
use MatchBot\Application\Assertion;
use MatchBot\Application\AssertionFailedException;
/**
* 18 digit ID as used at salesforce. The casing will be normalised on construction to be acceptable to salesforce.
*
* @psalm-template-covariant T of SalesforceProxy
*/
class Salesforce18Id implements JsonSerializable
{
public readonly string $value;
private const string CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345';
/**
* @param string $value
* @psalm-param class-string<T> $_entityClass
*/
private function __construct(string $value, ?string $_entityClass)
{
Assertion::length($value, 18, self::lengthErrorMessage(...));
Assertion::regex(
$value,
'/[a-zA-Z0-9]{18}/',
static fn(array $args) => "{$args['value']} does not match pattern for a Salesforce ID"
);
$caseCorrected = self::correctCase($value);
Assertion::same(\strtolower($caseCorrected), \strtolower($value));
$this->value = $caseCorrected;
}
/**
* @throws AssertionFailedException
* @return self<SalesforceProxy>
*/
public static function of(string $value): self
{
// I think we could also validate a checksum here but as the IDs are generally not hand typed that won't get us
// much.
return new self($value, null);
}
/**
* @return self<Charity>
*/
public static function ofCharity(string $id): self
{
return new self($id, Charity::class);
}
/**
* @return self<Fund>
*/
public static function ofFund(string $id): self
{
return new self($id, Fund::class);
}
/**
* @return self<Campaign>
*/
public static function ofCampaign(string $id): self
{
return new self($id, Campaign::class);
}
/**
* @return self<MetaCampaign>
*/
public static function ofMetaCampaign(string $id): self
{
return new self($id, MetaCampaign::class);
}
/**
* @return self<RegularGivingMandate>
*/
public static function ofRegularGivingMandate(string $id)
{
return new self($id, RegularGivingMandate::class);
}
public function __toString(): string
{
return $this->value;
}
#[\Override]
public function jsonSerialize(): string
{
return $this->value;
}
/**
* @param array{length: int, value: string} $args
*/
private static function lengthErrorMessage(array $args): string
{
return "Salesforce ID should have {$args['length']} chars, '{$args['value']}' has " . strlen($args['value']);
}
/**
* Function implementation by Daniel Ballinger, Adrian Larson,
* and shadowhand at https://salesforce.stackexchange.com
*
* (Daniel wrote in C#, Adrian ported to Apex, shadowhand ported that to PHP)
*
* https://salesforce.stackexchange.com/a/388044/145088
* https://salesforce.stackexchange.com/users/123722/shadowhand
* CC Licensed Attribution-ShareAlike 4.0 International
*/
private function correctCase(string $input): string
{
assert(assertion: strlen($input) === 18);
$id = '';
$capitalize = [
...self::shouldCapitalize(letter: substr($input, offset: 15, length: 1)),
...self::shouldCapitalize(letter: substr($input, offset: 16, length: 1)),
...self::shouldCapitalize(letter: substr($input, offset: 17, length: 1)),
];
for ($i = 0; $i < 15; $i++) {
$letter = substr($input, offset: $i, length: 1);
$id .= $capitalize[$i] ? strtoupper($letter) : strtolower($letter);
}
$id .= strtoupper(substr($input, offset: 15, length: 3));
return $id;
}
/**
* Function implementation by Daniel Ballinger, Adrian Larson,
* and shadowhand at https://salesforce.stackexchange.com
*
* (Daniel wrote in C#, Adrian ported to Apex, shadowhand ported that to PHP)
*
* https://salesforce.stackexchange.com/a/388044/145088
* https://salesforce.stackexchange.com/users/123722/shadowhand
* CC Licensed Attribution-ShareAlike 4.0 International
*
* @psalm-suppress PossiblyFalseOperand
* @return list<bool>
*/
private static function shouldCapitalize(string $letter): array
{
$index = strpos(haystack: self::CHARS, needle: strtoupper($letter));
$result = [];
for ($bit = 0; $bit < 5; $bit++) {
$result[] = ($index & (1 << $bit)) !== 0;
}
return $result;
}
}