Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 015fb7f

Browse files
committed
feat: ContactsToArray transformer
allows exporting contacts from Perun into SP metadata
1 parent 9629f82 commit 015fb7f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Module\perun\transformers;
6+
7+
use SimpleSAML\Configuration;
8+
use SimpleSAML\Module\perun\AttributeTransformer;
9+
10+
/**
11+
* Get contacts array from lists in contact attributes.
12+
*/
13+
class ContactsToArray extends AttributeTransformer
14+
{
15+
private $contactTypes;
16+
17+
private $removeSource;
18+
19+
private $outputAttr;
20+
21+
/**
22+
* @override
23+
*/
24+
public function __construct(Configuration $config)
25+
{
26+
$this->contactTypes = $config->getArray('contactTypes');
27+
$this->removeSource = !$config->getBoolean('keepSource', true);
28+
$this->outputAttr = $config->getString('outputAttr', 'contacts');
29+
}
30+
31+
/**
32+
* @override
33+
*/
34+
public function transform(array $attributes)
35+
{
36+
$result = [];
37+
$contacts = [];
38+
foreach ($attributes as $attribute => $emailList) {
39+
if ($this->removeSource) {
40+
$result[$attribute] = null;
41+
}
42+
if (!empty($emailList) && isset($this->contactTypes[$attribute])) {
43+
foreach ($emailList as $email) {
44+
$contacts[] = [
45+
'contactType' => $this->contactTypes[$attribute],
46+
'emailAddress' => $email,
47+
];
48+
}
49+
}
50+
}
51+
// contacts array
52+
return array_merge($result, [
53+
$this->outputAttr => $contacts,
54+
]);
55+
}
56+
}

0 commit comments

Comments
 (0)