| @@ 27-66 (lines=40) @@ | ||
| 24 | * @category PHPExif |
|
| 25 | * @package Common |
|
| 26 | */ |
|
| 27 | class ExposureTime extends StringObject |
|
| 28 | { |
|
| 29 | /** |
|
| 30 | * @param string $stringData |
|
| 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 |
|
| 56 | * |
|
| 57 | * @param ExposureTime $exposureTime |
|
| 58 | * |
|
| 59 | * @return ExposureTime |
|
| 60 | */ |
|
| 61 | public static function fromExposureTime(ExposureTime $exposureTime) |
|
| 62 | { |
|
| 63 | return new self( |
|
| 64 | (string) $exposureTime |
|
| 65 | ); |
|
| 66 | } |
|
| 67 | } |
|
| 68 | ||
| @@ 26-51 (lines=26) @@ | ||
| 23 | * @category PHPExif |
|
| 24 | * @package Common |
|
| 25 | */ |
|
| 26 | abstract class Resolution extends IntegerObject |
|
| 27 | { |
|
| 28 | /** |
|
| 29 | * @param string $stringData |
|
| 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 resolution 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->setValue($numerator); |
|
| 50 | } |
|
| 51 | } |
|
| 52 | ||