| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* This file is part of Optimole PHP SDK. |
| 7 |
* |
| 8 |
* (c) Optimole Team <friends@optimole.com> |
| 9 |
* |
| 10 |
* For the full copyright and license information, please view the LICENSE |
| 11 |
* file that was distributed with this source code. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Optimole\Sdk\ValueObject; |
| 15 |
|
| 16 |
use Optimole\Sdk\Exception\InvalidArgumentException; |
| 17 |
|
| 18 |
final class Position |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Center. |
| 22 |
*/ |
| 23 |
public const CENTER = 'ce'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Right Edge. |
| 27 |
*/ |
| 28 |
public const EAST = 'ea'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Top edge. |
| 32 |
*/ |
| 33 |
public const NORTH = 'no'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Top left corner. |
| 37 |
*/ |
| 38 |
public const NORTH_EAST = 'nowe'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Top right corner. |
| 42 |
*/ |
| 43 |
public const NORTH_WEST = 'noea'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Bottom Edge. |
| 47 |
*/ |
| 48 |
public const SOUTH = 'so'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Bottom right corner. |
| 52 |
*/ |
| 53 |
public const SOUTH_EAST = 'soea'; |
| 54 |
|
| 55 |
/** |
| 56 |
* Bottom left corner. |
| 57 |
*/ |
| 58 |
public const SOUTH_WEST = 'sowe'; |
| 59 |
|
| 60 |
/** |
| 61 |
* Left edge. |
| 62 |
*/ |
| 63 |
public const WEST = 'we'; |
| 64 |
|
| 65 |
/** |
| 66 |
* The position. |
| 67 |
*/ |
| 68 |
private string $position; |
| 69 |
|
| 70 |
/** |
| 71 |
* Constructor. |
| 72 |
*/ |
| 73 |
public function __construct(string $position) |
| 74 |
{ |
| 75 |
if (!self::isValid($position)) { |
| 76 |
throw new InvalidArgumentException(sprintf('Position "%s" is invalid.', $position)); |
| 77 |
} |
| 78 |
|
| 79 |
$this->position = $position; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Convert the position to its string representation. |
| 84 |
*/ |
| 85 |
public function __toString(): string |
| 86 |
{ |
| 87 |
return $this->position; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Validates if the given value is one of the defined position constants. |
| 92 |
*/ |
| 93 |
public static function isValid($value): bool |
| 94 |
{ |
| 95 |
return in_array($value, (new \ReflectionClass(__CLASS__))->getConstants()); |
| 96 |
} |
| 97 |
} |
| 98 |
|