Response.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage ReCaptcha
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Zend_Service_ReCaptcha_Response
  23. *
  24. * @category Zend
  25. * @package Zend_Service
  26. * @subpackage ReCaptcha
  27. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @version $Id$
  30. */
  31. class Zend_Service_ReCaptcha_Response
  32. {
  33. /**
  34. * Status
  35. *
  36. * true if the response is valid or false otherwise
  37. *
  38. * @var boolean
  39. */
  40. protected $_status = null;
  41. /**
  42. * Error code
  43. *
  44. * The error code if the status is false. The different error codes can be found in the
  45. * recaptcha API docs.
  46. *
  47. * @var string
  48. */
  49. protected $_errorCode = null;
  50. /**
  51. * Class constructor used to construct a response
  52. *
  53. * @param string $status
  54. * @param string $errorCode
  55. * @param Zend_Http_Response $httpResponse If this is set the content will override $status and $errorCode
  56. */
  57. public function __construct($status = null, $errorCode = null, Zend_Http_Response $httpResponse = null)
  58. {
  59. if ($status !== null) {
  60. $this->setStatus($status);
  61. }
  62. if ($errorCode !== null) {
  63. $this->setErrorCode($errorCode);
  64. }
  65. if ($httpResponse !== null) {
  66. $this->setFromHttpResponse($httpResponse);
  67. }
  68. }
  69. /**
  70. * Set the status
  71. *
  72. * @param string $status
  73. * @return Zend_Service_ReCaptcha_Response
  74. */
  75. public function setStatus($status)
  76. {
  77. if ($status === 'true') {
  78. $this->_status = true;
  79. } else {
  80. $this->_status = false;
  81. }
  82. return $this;
  83. }
  84. /**
  85. * Get the status
  86. *
  87. * @return boolean
  88. */
  89. public function getStatus()
  90. {
  91. return $this->_status;
  92. }
  93. /**
  94. * Alias for getStatus()
  95. *
  96. * @return boolean
  97. */
  98. public function isValid()
  99. {
  100. return $this->getStatus();
  101. }
  102. /**
  103. * Set the error code
  104. *
  105. * @param string $errorCode
  106. * @return Zend_Service_ReCaptcha_Response
  107. */
  108. public function setErrorCode($errorCode)
  109. {
  110. $this->_errorCode = $errorCode;
  111. return $this;
  112. }
  113. /**
  114. * Get the error code
  115. *
  116. * @return string
  117. */
  118. public function getErrorCode()
  119. {
  120. return $this->_errorCode;
  121. }
  122. /**
  123. * Populate this instance based on a Zend_Http_Response object
  124. *
  125. * @param Zend_Http_Response $response
  126. * @return Zend_Service_ReCaptcha_Response
  127. */
  128. public function setFromHttpResponse(Zend_Http_Response $response)
  129. {
  130. $body = $response->getBody();
  131. // Default status and error code
  132. $status = 'false';
  133. $errorCode = '';
  134. $parts = explode("\n", $body);
  135. if ($parts[0] === 'true') {
  136. $status = 'true';
  137. }
  138. if (!empty($parts[1])) {
  139. $errorCode = $parts[1];
  140. }
  141. $this->setStatus($status);
  142. $this->setErrorCode($errorCode);
  143. return $this;
  144. }
  145. }