-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
TraversableToCollection.php
55 lines (47 loc) · 1.34 KB
/
TraversableToCollection.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
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Util;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @author Javier Spagnoletti <phansys@gmail.com>
*/
final class TraversableToCollection
{
/**
* @param mixed $value
*
* @throws \TypeError
*
* @return Collection<int|string, mixed>
*
* @phpstan-return Collection<array-key, mixed>
*/
public static function transform($value): Collection
{
if ($value instanceof Collection) {
return $value;
}
if ($value instanceof \Traversable) {
return new ArrayCollection(iterator_to_array($value));
}
if (\is_array($value)) {
return new ArrayCollection($value);
}
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be of type "%s" or "%s", %s given.',
__METHOD__,
\Traversable::class,
'array',
\is_object($value) ? 'instance of "'.\get_class($value).'"' : '"'.\gettype($value).'"'
));
}
}