File indexing completed on 2024-12-22 05:36:17

0001 <?php
0002 namespace Cgsmith\Form\Element;
0003 
0004 /**
0005  * Class Recaptcha
0006  * Renders a div for google recaptcha to allow for use of the Google Recaptcha API
0007  *
0008  * @package Cgsmith
0009  * @license MIT
0010  * @author  Chris Smith
0011  */
0012 class Recaptcha extends \Zend_Form_Element
0013 {
0014     /** @var string specify formRecaptcha helper */
0015     public $helper = 'formRecaptcha';
0016 
0017     /** @var string siteKey for Google Recaptcha */
0018     protected $_siteKey = '';
0019 
0020     /** @var string secretKey for Google Recaptcha */
0021     protected $_secretKey = '';
0022 
0023     /**
0024      * Constructor for element and adds validator
0025      *
0026      * @param array|string|Zend_Config $spec
0027      * @param null $options
0028      * @throws \Zend_Exception
0029      * @throws \Zend_Form_Exception
0030      */
0031     public function __construct($spec, $options = null)
0032     {
0033         if (empty($options['siteKey']) || empty($options['secretKey'])) {
0034             throw new \Zend_Exception('Site key and secret key must be specified.');
0035         }
0036         $this->_siteKey = trim($options['siteKey']); // trim the white space if there is any just to be sure
0037         $this->_secretKey = trim($options['secretKey']); // trim the white space if there is any just to be sure
0038         $this->addValidator('Recaptcha', false, array('secretKey' => $this->_secretKey));
0039         $this->setAllowEmpty(false);
0040         parent::__construct($spec, $options);
0041     }
0042 }