/
IdentityUtils.php
88 lines (72 loc) · 1.96 KB
/
IdentityUtils.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
<?php
namespace Stampie\Util;
use Stampie\Identity;
use Stampie\IdentityInterface;
/**
* Stampie Identity utility functions
*
* @author Christophe Coevoet <stof@notk.org>
*/
class IdentityUtils
{
/**
* This class should not be instantiated
*/
private function __construct() {}
/**
* @param IdentityInterface|string $identity
*
* @return IdentityInterface
*/
public static function normalizeIdentity($identity)
{
if (!$identity instanceof IdentityInterface) {
$identity = new Identity($identity);
}
return $identity;
}
/**
* @param IdentityInterface[]|string $identities
*
* @return IdentityInterface[]
*/
public static function normalizeIdentities($identities)
{
if (null === $identities) {
return array();
}
if (is_string($identities)) {
$identities = array(self::normalizeIdentity($identities));
}
if (!is_array($identities) && $identities instanceof IdentityInterface) {
$identities = array($identities);
}
return $identities;
}
/**
* @param IdentityInterface[]|IdentityInterface|string $identities
*
* @return string
*/
public static function buildIdentityString($identities)
{
if (null === $identities) {
return '';
}
if (is_string($identities)) {
return $identities;
}
if ($identities instanceof IdentityInterface) {
$identities = array($identities);
}
$stringIdentities = array();
foreach ($identities as $identity) {
if (null === $identity->getName()) {
$stringIdentities[] = $identity->getEmail();
continue;
}
$stringIdentities[] = sprintf('%s <%s>', $identity->getName(), $identity->getEmail());
}
return implode(',', $stringIdentities);
}
}