3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Captcha
;
13 use Zend\Stdlib\ArrayUtils
;
15 abstract class Factory
18 * @var array Known captcha types
20 protected static $classMap = array(
21 'dumb' => 'Zend\Captcha\Dumb',
22 'figlet' => 'Zend\Captcha\Figlet',
23 'image' => 'Zend\Captcha\Image',
24 'recaptcha' => 'Zend\Captcha\ReCaptcha',
28 * Create a captcha adapter instance
30 * @param array|Traversable $options
31 * @return AdapterInterface
32 * @throws Exception\InvalidArgumentException for a non-array, non-Traversable $options
33 * @throws Exception\DomainException if class is missing or invalid
35 public static function factory($options)
37 if ($options instanceof Traversable
) {
38 $options = ArrayUtils
::iteratorToArray($options);
41 if (!is_array($options)) {
42 throw new Exception\
InvalidArgumentException(sprintf(
43 '%s expects an array or Traversable argument; received "%s"',
45 (is_object($options) ?
get_class($options) : gettype($options))
49 if (!isset($options['class'])) {
50 throw new Exception\
DomainException(sprintf(
51 '%s expects a "class" attribute in the options; none provided',
56 $class = $options['class'];
57 if (isset(static::$classMap[strtolower($class)])) {
58 $class = static::$classMap[strtolower($class)];
60 if (!class_exists($class)) {
61 throw new Exception\
DomainException(sprintf(
62 '%s expects the "class" attribute to resolve to an existing class; received "%s"',
68 unset($options['class']);
70 if (isset($options['options'])) {
71 $options = $options['options'];
73 $captcha = new $class($options);
75 if (!$captcha instanceof AdapterInterface
) {
76 throw new Exception\
DomainException(sprintf(
77 '%s expects the "class" attribute to resolve to a valid Zend\Captcha\AdapterInterface instance; received "%s"',