Skip to content

Commit 4f20d25

Browse files
davidgraystonRodion Liuborets
andauthored
SDK-1151: Allow multiple attributes with the same name (#138)
* SDK-1151: Support multiple attributes with the same name but different constraints * SDK-1151: Update example to allow multiple attributes * Change from md5 to sha-256 * Remove old examples Co-authored-by: Rodion Liuborets <[email protected]>
1 parent 86938f3 commit 4f20d25

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

src/Profile/BaseProfile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function ($carry, Attribute $attr) {
5050
*
5151
* @return \Yoti\Profile\Attribute[]
5252
*/
53-
private function getAttributesByName(string $attributeName): array
53+
public function getAttributesByName(string $attributeName): array
5454
{
5555
return $this->attributesMap[$attributeName] ?? [];
5656
}

src/ShareUrl/Policy/DynamicPolicyBuilder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yoti\ShareUrl\Policy;
66

77
use Yoti\Profile\UserProfile;
8+
use Yoti\Util\Json;
89

910
/**
1011
* Builder for DynamicPolicy.
@@ -49,10 +50,15 @@ class DynamicPolicyBuilder
4950
public function withWantedAttribute(WantedAttribute $wantedAttribute): self
5051
{
5152
$key = $wantedAttribute->getName();
53+
5254
if ($wantedAttribute->getDerivation() !== null) {
5355
$key = $wantedAttribute->getDerivation();
5456
}
5557

58+
if ($wantedAttribute->getConstraints() !== null) {
59+
$key .= '-' . hash('sha256', Json::encode($wantedAttribute->getConstraints()));
60+
}
61+
5662
$this->wantedAttributes[$key] = $wantedAttribute;
5763
return $this;
5864
}

tests/Profile/BaseProfileTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function setup(): void
6969
* @covers ::__construct
7070
* @covers ::getProfileAttribute
7171
* @covers ::setAttributesMap
72-
* @covers ::getAttributesByName
7372
*/
7473
public function testGetProfileAttribute()
7574
{
@@ -88,4 +87,24 @@ public function testGetAttributesList()
8887
$this->assertCount(3, $attributesList);
8988
$this->assertContainsOnlyInstancesOf(Attribute::class, $attributesList);
9089
}
90+
91+
/**
92+
* @covers ::__construct
93+
* @covers ::getAttributesByName
94+
*/
95+
public function testGetAttributesByName()
96+
{
97+
$attributes = $this->baseProfile->getAttributesByName(self::SOME_ATTRIBUTE);
98+
$this->assertCount(2, $attributes);
99+
$this->assertSame($this->someAttribute, $attributes[0]);
100+
$this->assertSame($this->someOtherAttributeWithSameName, $attributes[1]);
101+
102+
$invalidAttributes = $this->baseProfile->getAttributesByName(self::SOME_INVALID_ATTRIBUTE);
103+
$this->assertIsArray($invalidAttributes);
104+
$this->assertEmpty($invalidAttributes);
105+
106+
$missingAttributes = $this->baseProfile->getAttributesByName(self::SOME_MISSING_ATTRIBUTE);
107+
$this->assertIsArray($missingAttributes);
108+
$this->assertEmpty($missingAttributes);
109+
}
91110
}

tests/ShareUrl/Policy/DynamicPolicyBuilderTest.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testBuildWithAttributes()
8585
/**
8686
* @covers ::withWantedAttributeByName
8787
*/
88-
public function testWithWantedAttributeByName()
88+
public function testWithWantedAttributeByNameWithConstraints()
8989
{
9090
$someAttributeName = 'some_attribute_name';
9191

@@ -159,11 +159,47 @@ public function testWithDuplicateAttribute()
159159
$this->assertEquals(json_encode($expectedWantedAttributeData), json_encode($dynamicPolicy));
160160
}
161161

162+
/**
163+
* @covers ::withWantedAttribute
164+
* @covers ::withFamilyName
165+
*/
166+
public function testWithDuplicateAttributeDifferentConstraints()
167+
{
168+
$passportConstraints = (new ConstraintsBuilder())
169+
->withSourceConstraint(
170+
(new SourceConstraintBuilder())
171+
->withPassport()
172+
->build()
173+
)
174+
->build();
175+
176+
$drivingLicenseConstraints = (new ConstraintsBuilder())
177+
->withSourceConstraint(
178+
(new SourceConstraintBuilder())
179+
->withDrivingLicence()
180+
->build()
181+
)
182+
->build();
183+
184+
$dynamicPolicy = (new DynamicPolicyBuilder())
185+
->withFamilyName()
186+
->withFamilyName($passportConstraints)
187+
->withFamilyName($drivingLicenseConstraints)
188+
->build();
189+
190+
$jsonData = $dynamicPolicy->jsonSerialize();
191+
192+
$this->assertCount(3, $jsonData->wanted);
193+
foreach ($jsonData->wanted as $wantedAttribute) {
194+
$this->assertEquals('family_name', $wantedAttribute->getName());
195+
}
196+
}
197+
162198
/**
163199
* @covers ::build
164200
* @covers ::withWantedAttributeByName
165201
*/
166-
public function testWithAttributesByName()
202+
public function testWithWantedAttributeByName()
167203
{
168204
$dynamicPolicy = (new DynamicPolicyBuilder())
169205
->withWantedAttributeByName('family_name')

0 commit comments

Comments
 (0)