| @@ 34-52 (lines=19) @@ | ||
| 31 | * |
|
| 32 | * @throws InvalidArgumentException If given argument is not a string |
|
| 33 | */ |
|
| 34 | public function __construct($stringData) |
|
| 35 | { |
|
| 36 | if (!is_string($stringData)) { |
|
| 37 | throw new InvalidArgumentException('Given data is not a string'); |
|
| 38 | } |
|
| 39 | ||
| 40 | if (!preg_match('#^([0-9]+)/([0-9]+)s?$#', $stringData, $matches)) { |
|
| 41 | throw new RuntimeException('Given exposure time is not in a valid format. |
|
| 42 | Need: "1/<number>" or "1/<number>s"'); |
|
| 43 | } |
|
| 44 | ||
| 45 | $numerator = (int) $matches[1]; |
|
| 46 | $denominator = (int) $matches[2]; |
|
| 47 | ||
| 48 | // normalize: |
|
| 49 | $denominator /= $numerator; |
|
| 50 | ||
| 51 | $this->setStringData("1/{$denominator}"); |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * Creates a new instance from given ExposureTime object |
|
| @@ 33-50 (lines=18) @@ | ||
| 30 | * |
|
| 31 | * @throws InvalidArgumentException If given argument is not a string |
|
| 32 | */ |
|
| 33 | public function __construct($stringData) |
|
| 34 | { |
|
| 35 | if (!is_string($stringData)) { |
|
| 36 | throw new InvalidArgumentException('Given data is not a string'); |
|
| 37 | } |
|
| 38 | ||
| 39 | if (!preg_match('#^([0-9]+)/([0-9]+)$#', $stringData, $matches)) { |
|
| 40 | throw new RuntimeException('Given focal length is not in a valid format. Need: "<number>/<number>"'); |
|
| 41 | } |
|
| 42 | ||
| 43 | $numerator = (int) $matches[1]; |
|
| 44 | $denominator = (int) $matches[2]; |
|
| 45 | ||
| 46 | // normalize: |
|
| 47 | $numerator /= $denominator; |
|
| 48 | ||
| 49 | $this->setStringData("{$numerator}mm"); |
|
| 50 | } |
|
| 51 | ||
| 52 | /** |
|
| 53 | * Creates new instance with data in milimeter |
|
| @@ 32-50 (lines=19) @@ | ||
| 29 | * |
|
| 30 | * @throws InvalidArgumentException If given argument is not a string |
|
| 31 | */ |
|
| 32 | public function __construct($value, Unit $unit) |
|
| 33 | { |
|
| 34 | if (!is_string($value)) { |
|
| 35 | throw new InvalidArgumentException('Given value is not a string'); |
|
| 36 | } |
|
| 37 | ||
| 38 | if (!preg_match('#^([0-9]+)/([0-9]+)$#', $value, $matches)) { |
|
| 39 | throw new RuntimeException('Given resolution is not in a valid format. Need: "<number>/<number>"'); |
|
| 40 | } |
|
| 41 | ||
| 42 | $numerator = (int) $matches[1]; |
|
| 43 | $denominator = (int) $matches[2]; |
|
| 44 | ||
| 45 | // normalize: |
|
| 46 | $numerator /= $denominator; |
|
| 47 | ||
| 48 | $this->setValue($numerator); |
|
| 49 | $this->setUnit($unit); |
|
| 50 | } |
|
| 51 | ||
| 52 | /** |
|
| 53 | * Creates a new instance from given Resolution object |
|