File indexing completed on 2024-05-19 06:02:48

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Captcha
0017  * @subpackage Adapter
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /** @see Zend_Captcha_Adapter */
0023 // require_once 'Zend/Captcha/Adapter.php';
0024 
0025 /** @see Zend_Validate_Abstract */
0026 // require_once 'Zend/Validate/Abstract.php';
0027 
0028 /**
0029  * Base class for Captcha adapters
0030  *
0031  * Provides some utility functionality to build on
0032  *
0033  * @category   Zend
0034  * @package    Zend_Captcha
0035  * @subpackage Adapter
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  * @version    $Id$
0039  */
0040 abstract class Zend_Captcha_Base extends Zend_Validate_Abstract implements Zend_Captcha_Adapter
0041 {
0042     /**
0043      * Element name
0044      *
0045      * Useful to generate/check form fields
0046      *
0047      * @var string
0048      */
0049     protected $_name;
0050 
0051     /**
0052      * Captcha options
0053      *
0054      * @var array
0055      */
0056     protected $_options = array();
0057 
0058     /**
0059      * Options to skip when processing options
0060      * @var array
0061      */
0062     protected $_skipOptions = array(
0063         'options',
0064         'config',
0065     );
0066 
0067     /**
0068      * Get name
0069      *
0070      * @return string
0071      */
0072     public function getName()
0073     {
0074         return $this->_name;
0075     }
0076 
0077     /**
0078      * Set name
0079      *
0080      * @param string $name
0081      * @return Zend_Captcha_Adapter
0082      */
0083     public function setName($name)
0084     {
0085         $this->_name = $name;
0086         return $this;
0087     }
0088 
0089     /**
0090      * Constructor
0091      *
0092      * @param  array|Zend_Config $options
0093      */
0094     public function __construct($options = null)
0095     {
0096         // Set options
0097         if (is_array($options)) {
0098             $this->setOptions($options);
0099         } else if ($options instanceof Zend_Config) {
0100             $this->setConfig($options);
0101         }
0102     }
0103 
0104     /**
0105      * Set single option for the object
0106      *
0107      * @param string $key
0108      * @param string $value
0109      * @return Zend_Form_Element
0110      */
0111     public function setOption($key, $value)
0112     {
0113         if (in_array(strtolower($key), $this->_skipOptions)) {
0114             return $this;
0115         }
0116 
0117         $method = 'set' . ucfirst ($key);
0118         if (method_exists ($this, $method)) {
0119             // Setter exists; use it
0120             $this->$method ($value);
0121             $this->_options[$key] = $value;
0122         } elseif (property_exists($this, $key)) {
0123             // Assume it's metadata
0124             $this->$key = $value;
0125             $this->_options[$key] = $value;
0126         }
0127         return $this;
0128     }
0129 
0130     /**
0131      * Set object state from options array
0132      *
0133      * @param  array $options
0134      * @return Zend_Form_Element
0135      */
0136     public function setOptions($options = null)
0137     {
0138         foreach ($options as $key => $value) {
0139             $this->setOption($key, $value);
0140         }
0141         return $this;
0142     }
0143 
0144     /**
0145      * Retrieve options representing object state
0146      *
0147      * @return array
0148      */
0149     public function getOptions()
0150     {
0151         return $this->_options;
0152     }
0153 
0154     /**
0155      * Set object state from config object
0156      *
0157      * @param  Zend_Config $config
0158      * @return Zend_Captcha_Base
0159      */
0160     public function setConfig(Zend_Config $config)
0161     {
0162         return $this->setOptions($config->toArray());
0163     }
0164 
0165     /**
0166      * Get optional decorator
0167      *
0168      * By default, return null, indicating no extra decorator needed.
0169      *
0170      * @return null
0171      */
0172     public function getDecorator()
0173     {
0174         return null;
0175     }
0176 }