x = $x; $this->y = $y; $this->width = $width; $this->height = $height; } /** * @param array $config * @return Area * @throws InvalidConfigurationException */ public static function createFromConfiguration(array $config): Area { try { return new self( $config['x'], $config['y'], $config['width'], $config['height'] ); } catch (\Throwable $throwable) { throw new InvalidConfigurationException(sprintf('Invalid type for area property given: %s', $throwable->getMessage()), 1485279226, $throwable); } } /** * @param array $config * @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); } /** * @return array * @internal */ public function asArray(): array { return [ 'x' => $this->x, 'y' => $this->y, 'width' => $this->width, 'height' => $this->height, ]; } /** * @param FileInterface $file * @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') ); } /** * @param FileInterface $file * @return Area */ public function makeRelativeBasedOnFile(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') ); } /** * @param Ratio $ratio * @return Area */ 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 ''; } else { return json_encode($this->asArray()); } } }