Services_ReCaptcha
[ class tree: Services_ReCaptcha ] [ index: Services_ReCaptcha ] [ all elements ]

Source for file Base.php

Documentation is available at Base.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Services_ReCaptcha package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Services
  15.  * @package   Services_ReCaptcha
  16.  * @author    David Jean Louis <izi@php.net>
  17.  * @copyright 2008-2009 David Jean Louis
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id$
  20.  * @link      http://pear.php.net/package/Services_ReCaptcha
  21.  * @link      http://www.recaptcha.net
  22.  * @since     File available since release 0.1.0
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * Dependencies.
  28.  */
  29. require_once 'Services/ReCaptcha/Exception.php';
  30.  
  31. /**
  32.  * Base class for Services_ReCaptcha and Services_ReCaptcha_MailHide.
  33.  *
  34.  * @category  Services
  35.  * @package   Services_ReCaptcha
  36.  * @author    David Jean Louis <izi@php.net>
  37.  * @copyright 2008-2009 David Jean Louis
  38.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  39.  * @version   Release: @package_version@
  40.  * @link      http://pear.php.net/package/Services_ReCaptcha
  41.  * @link      http://www.recaptcha.net
  42.  * @since     Class available since release 0.1.0
  43.  */
  44. abstract class Services_ReCaptcha_Base
  45. {
  46.     // properties {{{
  47.  
  48.     /**
  49.      * reCAPTCHA/reCAPTCHA Mailhide API URL.
  50.      *
  51.      * @var string $apiURL 
  52.      */
  53.     public $apiURL;
  54.  
  55.     /**
  56.      * reCAPTCHA/reCAPTCHA Mailhide API public key.
  57.      *
  58.      * @var string $apiPublicKey 
  59.      */
  60.     protected $apiPublicKey;
  61.  
  62.     /**
  63.      * reCAPTCHA/reCAPTCHA Mailhide API private key.
  64.      *
  65.      * @var string $apiPrivateKey 
  66.      */
  67.     protected $apiPrivateKey;
  68.  
  69.     /**
  70.      * Array of options.
  71.      *
  72.      * @var array $options 
  73.      */
  74.     protected $options = array();
  75.  
  76.     // }}}
  77.     // __construct() {{{
  78.     
  79.     /**
  80.      * Constructor, you must pass a valid public and private API key, and
  81.      * optionnaly an array of options.
  82.      *
  83.      * @param string $pubKey  The public API key (mandatory)
  84.      * @param string $privKey The private API key (mandatory)
  85.      * @param array  $options An array of options (optional)
  86.      * 
  87.      * @return void 
  88.      */
  89.     public function __construct($pubKey$privKeyarray $options = array())
  90.     {
  91.         $this->apiPublicKey  = $pubKey;
  92.         $this->apiPrivateKey = $privKey;
  93.         if (!empty($options)) {
  94.             $this->setOptions($options);
  95.         }
  96.     }
  97.     
  98.     // }}}
  99.     // __toString() {{{
  100.     
  101.     /**
  102.      * Returns the corresponding HTML code, this is a "magic" shortcut to the
  103.      * getHTML() method.
  104.      *
  105.      * @return string The html code
  106.      * @see Services_ReCaptcha_Base::getHTML()
  107.      */
  108.     public function __toString()
  109.     {
  110.         try {
  111.             return $this->getHTML();
  112.         catch (Services_ReCaptcha_Exception $exc{
  113.             trigger_error($exc->getMessage()E_USER_ERROR);
  114.         }
  115.     }
  116.     
  117.     // }}}
  118.     // getHTML() {{{
  119.  
  120.     /**
  121.      * Returns the corresponding HTML code.
  122.      *
  123.      * @return string 
  124.      */
  125.     abstract public function getHTML();
  126.  
  127.     // }}}
  128.     // getOption() {{{
  129.  
  130.     /**
  131.      * Returns an option from {@link Services_ReCaptcha::$options}.
  132.      *
  133.      * @param string $option Name of option
  134.      *
  135.      * @return mixed The value of the option
  136.      */
  137.     public function getOption($option)
  138.     {
  139.         if (array_key_exists($option$this->options)) {
  140.             return $this->options[$option];
  141.         }
  142.     }
  143.  
  144.     // }}}
  145.     // setOption() {{{
  146.  
  147.     /**
  148.      * Sets an option in {@link Services_ReCaptcha::$options} and returns the
  149.      * current Services_ReCaptcha_Base instance.
  150.      *
  151.      * @param string $option Name of option
  152.      * @param mixed  $value  Value of option
  153.      *
  154.      * @return Services_ReCaptcha_Base 
  155.      */
  156.     public function setOption($option$value)
  157.     {
  158.         if (array_key_exists($option$this->options)) {
  159.             $this->options[$option$value;
  160.         }
  161.         return $this;
  162.     }
  163.  
  164.     // }}}
  165.     // getOptions() {{{
  166.  
  167.     /**
  168.      * Returns the {@link Services_ReCaptcha::$options} array.
  169.      *
  170.      * @return array The options array
  171.      */
  172.     public function getOptions()
  173.     {
  174.         return $this->options;
  175.     }
  176.  
  177.     // }}}
  178.     // setOptions() {{{
  179.  
  180.     /**
  181.      * Sets a number of options at once in {@link Services_ReCaptcha::$options}
  182.      * and returns the current Services_ReCaptcha_Base instance.
  183.      *
  184.      * @param array $options Associative array of options name/value
  185.      *
  186.      * @return Services_ReCaptcha_Base 
  187.      * @see Services_ReCaptcha::setOption()
  188.      */
  189.     public function setOptions(array $options)
  190.     {
  191.         foreach ($options as $option => $value{
  192.             $this->setOption($option$value);
  193.         }
  194.         return $this;
  195.     }
  196.  
  197.     // }}}
  198. }

Documentation generated on Mon, 11 Mar 2019 15:43:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.