composer package updates
[openemr.git] / vendor / zendframework / zend-mail / src / Transport / Factory.php
blob6c3d5acddea4f6b46f9bf31586b43db209d24579
1 <?php
2 /**
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
6 */
8 namespace Zend\Mail\Transport;
10 use Traversable;
11 use Zend\Stdlib\ArrayUtils;
13 abstract class Factory
15 /**
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',
27 /**
28 * @param array $spec
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"',
42 __METHOD__,
43 (is_object($spec) ? get_class($spec) : gettype($spec))
44 ));
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"',
58 __METHOD__,
59 $type
60 ));
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"',
69 __METHOD__,
70 $type
71 ));
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']));
82 return $transport;