3 * @see https://github.com/zendframework/zend-mail for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-mail/blob/master/LICENSE.md New BSD License
8 namespace Zend\Mail\Transport
;
11 use Zend\Stdlib\ArrayUtils
;
13 abstract class Factory
16 * @var array Known transport types
18 protected static $classMap = [
19 'file' => 'Zend\Mail\Transport\File',
20 'inmemory' => 'Zend\Mail\Transport\InMemory',
21 'memory' => 'Zend\Mail\Transport\InMemory',
22 'null' => 'Zend\Mail\Transport\InMemory',
23 'sendmail' => 'Zend\Mail\Transport\Sendmail',
24 'smtp' => 'Zend\Mail\Transport\Smtp',
29 * @return TransportInterface
30 * @throws Exception\InvalidArgumentException
31 * @throws Exception\DomainException
33 public static function create($spec = [])
35 if ($spec instanceof Traversable
) {
36 $spec = ArrayUtils
::iteratorToArray($spec);
39 if (! is_array($spec)) {
40 throw new Exception\
InvalidArgumentException(sprintf(
41 '%s expects an array or Traversable argument; received "%s"',
43 (is_object($spec) ?
get_class($spec) : gettype($spec))
47 $type = isset($spec['type']) ?
$spec['type'] : 'sendmail';
49 $normalizedType = strtolower($type);
51 if (isset(static::$classMap[$normalizedType])) {
52 $type = static::$classMap[$normalizedType];
55 if (! class_exists($type)) {
56 throw new Exception\
DomainException(sprintf(
57 '%s expects the "type" attribute to resolve to an existing class; received "%s"',
63 $transport = new $type;
65 if (! $transport instanceof TransportInterface
) {
66 throw new Exception\
DomainException(sprintf(
67 '%s expects the "type" attribute to resolve to a valid'
68 . ' Zend\Mail\Transport\TransportInterface instance; received "%s"',
74 if ($transport instanceof Smtp
&& isset($spec['options'])) {
75 $transport->setOptions(new SmtpOptions($spec['options']));
78 if ($transport instanceof File
&& isset($spec['options'])) {
79 $transport->setOptions(new FileOptions($spec['options']));