x = $x; $this->y = $y; $this->width = $width; $this->height = $height; } /** * @throws InvalidConfigurationException */ public static function createFromConfiguration(array $config): Area { try { return new self( (float)$config['x'], (float)$config['y'], (float)$config['width'], (float)$config['height'] ); } catch (\Throwable $throwable) { throw new InvalidConfigurationException(sprintf('Invalid type for area property given: %s', $throwable->getMessage()), 1485279226, $throwable); } } /** * @return Area[] * @throws InvalidConfigurationException */ public static function createMultipleFromConfiguration(array $config): array { $areas = []; foreach ($config as $areaConfig) { $areas[] = self::createFromConfiguration($areaConfig); } return $areas; } /** * @return Area */ public static function createEmpty() { return new self(0.0, 0.0, 1.0, 1.0); } public function getWidth(): float { return $this->width; } public function getHeight(): float { return $this->height; } public function getOffsetLeft(): float { return $this->x; } public function getOffsetTop(): float { return $this->y; } /** * @internal */ public function asArray(): array { return [ 'x' => $this->x, 'y' => $this->y, 'width' => $this->width, 'height' => $this->height, ]; } /** * @return Area */ public function makeAbsoluteBasedOnFile(FileInterface $file) { return new self( $this->x * $file->getProperty('width'), $this->y * $file->getProperty('height'), $this->width * $file->getProperty('width'), $this->height * $file->getProperty('height') ); } /** * @return Area */ public function makeRelativeBasedOnFile(FileInterface $file) { $width = $file->getProperty('width'); $height = $file->getProperty('height'); if (empty($width) || empty($height)) { return self::createEmpty(); } return new self( $this->x / $width, $this->y / $height, $this->width / $width, $this->height / $height ); } public function applyRatioRestriction(Ratio $ratio): Area { if ($ratio->isFree()) { return $this; } $expectedRatio = $ratio->getRatioValue(); $newArea = clone $this; if ($newArea->height * $expectedRatio > $newArea->width) { $newArea->height = $newArea->width / $expectedRatio; $newArea->y += ($this->height - $newArea->height) / 2; } else { $newArea->width = $newArea->height * $expectedRatio; $newArea->x += ($this->width - $newArea->width) / 2; } return $newArea; } /** * @return bool */ public function isEmpty() { return $this->x === 0.0 && $this->y === 0.0 && $this->width === 1.0 && $this->height === 1.0; } /** * @return string */ public function __toString() { if ($this->isEmpty()) { return ''; } return json_encode($this->asArray()); } }