Файл: vendor/intervention/image/src/Geometry/Bezier.php
Строк: 258
<?php
declare(strict_types=1);
namespace InterventionImageGeometry;
use ArrayAccess;
use ArrayIterator;
use Countable;
use InterventionImageColorsAbstractColor;
use InterventionImageGeometryFactoriesBezierFactory;
use Traversable;
use IteratorAggregate;
use InterventionImageGeometryTraitsHasBackgroundColor;
use InterventionImageGeometryTraitsHasBorder;
use InterventionImageInterfacesDrawableFactoryInterface;
use InterventionImageInterfacesDrawableInterface;
use InterventionImageInterfacesPointInterface;
/**
* @implements IteratorAggregate<PointInterface>
* @implements ArrayAccess<int, PointInterface>
*/
class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInterface
{
use HasBorder;
use HasBackgroundColor;
/**
* Create new bezier instance.
*
* @param array<PointInterface> $points
*/
public function __construct(
protected array $points = [],
protected PointInterface $pivot = new Point(),
) {
//
}
/**
* {@inheritdoc}
*
* @see DrawableInterface::position()
*/
public function position(): PointInterface
{
return $this->pivot;
}
/**
* {@inheritdoc}
*
* @see DrawableInterface::setPosition()
*/
public function setPosition(PointInterface $position): self
{
$this->pivot = $position;
return $this;
}
/**
* Implement iteration through all points of bezier.
*
* @return Traversable<PointInterface>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->points);
}
/**
* Return current pivot point.
*/
public function pivot(): PointInterface
{
return $this->pivot;
}
/**
* Change pivot point to given point.
*/
public function setPivot(PointInterface $pivot): self
{
$this->pivot = $pivot;
return $this;
}
/**
* Return first control point of bezier.
*/
public function first(): ?PointInterface
{
if ($point = reset($this->points)) {
return $point;
}
return null;
}
/**
* Return second control point of bezier.
*/
public function second(): ?PointInterface
{
if (array_key_exists(1, $this->points)) {
return $this->points[1];
}
return null;
}
/**
* Return third control point of bezier.
*/
public function third(): ?PointInterface
{
if (array_key_exists(2, $this->points)) {
return $this->points[2];
}
return null;
}
/**
* Return last control point of bezier.
*/
public function last(): ?PointInterface
{
if ($point = end($this->points)) {
return $point;
}
return null;
}
/**
* Return bezier's point count.
*/
public function count(): int
{
return count($this->points);
}
/**
* Determine if point exists at given offset.
*/
public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->points);
}
/**
* Return point at given offset.
*/
public function offsetGet(mixed $offset): mixed
{
return $this->points[$offset];
}
/**
* Set point at given offset.
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->points[$offset] = $value;
}
/**
* Unset offset at given offset.
*/
public function offsetUnset(mixed $offset): void
{
unset($this->points[$offset]);
}
/**
* Add given point to bezier.
*/
public function addPoint(PointInterface $point): self
{
$this->points[] = $point;
return $this;
}
/**
* Return array of all x/y values of all points of bezier.
*
* @return array<int>
*/
public function toArray(): array
{
$coordinates = [];
foreach ($this->points as $point) {
$coordinates[] = $point->x();
$coordinates[] = $point->y();
}
return $coordinates;
}
/**
* {@inheritdoc}
*
* @see DrawableInterface::factory()
*/
public function factory(): DrawableFactoryInterface
{
return new BezierFactory($this);
}
/**
* {@inheritdoc}
*
* @see DrawableInterface::adjust()
*/
public function adjust(callable $adjustments): DrawableInterface
{
$factory = $this->factory();
$adjustments($factory);
return $factory->drawable();
}
/**
* Clone bezier.
*/
public function __clone(): void
{
$this->points = array_map(fn($point) => clone $point, $this->points);
$this->pivot = clone $this->pivot;
if ($this->backgroundColor instanceof AbstractColor) {
$this->backgroundColor = clone $this->backgroundColor;
}
if ($this->borderColor instanceof AbstractColor) {
$this->borderColor = clone $this->borderColor;
}
}
}