CaptureRequest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Http;
  9. use JonnyW\PhantomJs\Exception\NotWritableException;
  10. /**
  11. * PHP PhantomJs
  12. *
  13. * @author Jon Wenmoth <contact@jonnyw.me>
  14. */
  15. class CaptureRequest extends AbstractRequest
  16. implements CaptureRequestInterface
  17. {
  18. /**
  19. * Request type
  20. *
  21. * @var string
  22. * @access protected
  23. */
  24. protected $type;
  25. /**
  26. * File to save output.
  27. *
  28. * @var string
  29. * @access protected
  30. */
  31. protected $outputFile;
  32. /**
  33. * Rect top
  34. *
  35. * @var int
  36. * @access protected
  37. */
  38. protected $rectTop;
  39. /**
  40. * Rect left
  41. *
  42. * @var int
  43. * @access protected
  44. */
  45. protected $rectLeft;
  46. /**
  47. * Rect width
  48. *
  49. * @var int
  50. * @access protected
  51. */
  52. protected $rectWidth;
  53. /**
  54. * Rect height
  55. *
  56. * @var int
  57. * @access protected
  58. */
  59. protected $rectHeight;
  60. /**
  61. * Internal constructor
  62. *
  63. * @access public
  64. * @param string $url (default: null)
  65. * @param string $method (default: RequestInterface::METHOD_GET)
  66. * @param int $timeout (default: 5000)
  67. * @return \JonnyW\PhantomJs\Http\CaptureRequest
  68. */
  69. public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000)
  70. {
  71. parent::__construct($url, $method, $timeout);
  72. $this->rectTop = 0;
  73. $this->rectLeft = 0;
  74. $this->rectWidth = 0;
  75. $this->rectHeight = 0;
  76. }
  77. /**
  78. * Get request type
  79. *
  80. * @access public
  81. * @return string
  82. */
  83. public function getType()
  84. {
  85. if (!$this->type) {
  86. return RequestInterface::REQUEST_TYPE_CAPTURE;
  87. }
  88. return $this->type;
  89. }
  90. /**
  91. * Set request type
  92. *
  93. * @access public
  94. * @param string $type
  95. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  96. */
  97. public function setType($type)
  98. {
  99. $this->type = $type;
  100. return $this;
  101. }
  102. /**
  103. * Set viewport size.
  104. *
  105. * @access public
  106. * @param int $width
  107. * @param int $height
  108. * @param int $top (default: 0)
  109. * @param int $left (default: 0)
  110. * @return \JonnyW\PhantomJs\Http\AbstractRequest
  111. */
  112. public function setCaptureDimensions($width, $height, $top = 0, $left = 0)
  113. {
  114. $this->rectWidth = (int) $width;
  115. $this->rectHeight = (int) $height;
  116. $this->rectTop = (int) $top;
  117. $this->rectLeft = (int) $left;
  118. return $this;
  119. }
  120. /**
  121. * Get rect top.
  122. *
  123. * @access public
  124. * @return int
  125. */
  126. public function getRectTop()
  127. {
  128. return (int) $this->rectTop;
  129. }
  130. /**
  131. * Get rect left.
  132. *
  133. * @access public
  134. * @return int
  135. */
  136. public function getRectLeft()
  137. {
  138. return (int) $this->rectLeft;
  139. }
  140. /**
  141. * Get rect width.
  142. *
  143. * @access public
  144. * @return int
  145. */
  146. public function getRectWidth()
  147. {
  148. return (int) $this->rectWidth;
  149. }
  150. /**
  151. * Get rect height.
  152. *
  153. * @access public
  154. * @return int
  155. */
  156. public function getRectHeight()
  157. {
  158. return (int) $this->rectHeight;
  159. }
  160. /**
  161. * Set file to save output.
  162. *
  163. * @access public
  164. * @param string $file
  165. * @throws \JonnyW\PhantomJs\Exception\NotWritableException
  166. * @return \JonnyW\PhantomJs\Http\CaptureRequest
  167. */
  168. public function setOutputFile($file)
  169. {
  170. if (!is_writable(dirname($file))) {
  171. throw new NotWritableException(sprintf('Output file is not writeable by PhantomJs: %s', $file));
  172. }
  173. $this->outputFile = $file;
  174. return $this;
  175. }
  176. /**
  177. * Get output file.
  178. *
  179. * @access public
  180. * @return string
  181. */
  182. public function getOutputFile()
  183. {
  184. return $this->outputFile;
  185. }
  186. }