closedquestion-8.x-3.x-dev/src/Question/Mapping/CqHotspotRect.php
src/Question/Mapping/CqHotspotRect.php
<?php
namespace Drupal\closedquestion\Question\Mapping;
/**
* Class CqHotspotRect.
*
* A rectangle-shaped hotspot formed by two opposite corner points.
*
* @package Drupal\closedquestion\Question\Mapping
*/
class CqHotspotRect extends CqAbstractHotspot {
/**
* The x-coordinate of the first corner point.
*
* @var int
*/
private $x1 = 0;
/**
* The y-coordinate of the first corner point.
*
* @var int
*/
private $y1 = 0;
/**
* The x-coordinate of the second corner point.
*
* @var int
*/
private $x2 = 0;
/**
* The y-coordinate of the second corner point.
*
* @var int
*/
private $y2 = 0;
/**
* Creates a new CqHotspotRect.
*
* @param string $identifier
* The identifier to use for this hotspot.
* @param int $x1
* The x-coordinate for the first corner point.
* @param int $y1
* The y-coordinate for the first corner point.
* @param int $x2
* The x-coordinate for the second corner point.
* @param int $y2
* The y-coordinate for the second corner point.
*/
public function __construct($identifier, $x1, $y1, $x2, $y2) {
$this->identifier = $identifier;
if ($x1 > $x2) {
$this->x1 = $x2;
$this->x2 = $x1;
}
else {
$this->x1 = $x1;
$this->x2 = $x2;
}
if ($y1 > $y2) {
$this->y1 = $y2;
$this->y2 = $y1;
}
else {
$this->y1 = $y1;
$this->y2 = $y2;
}
}
/**
* Implements CqHotspotInterface::doMatch()
*/
public function doMatch($location) {
$x = $location[0];
$y = $location[1];
return ($x >= $this->x1 && $x <= $this->x2 && $y >= $this->y1 && $y <= $this->y2);
}
/**
* Implements CqHotspotInterface::getMapHtml()
*/
public function getMapHtml() {
return 'shape="rect" coords="' . $this->x1 . ',' . $this->y1 . ',' . $this->x2 . ',' . $this->y2 . '"';
}
/**
* Implements CqHotspotInterface::getMapData()
*/
public function getMapData() {
return array('shape' => 'rect', 'coords' => $this->x1 . ',' . $this->y1 . ',' . $this->x2 . ',' . $this->y2);
}
}
