1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Exif FocalLength ValueObject |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/PHPExif/php-exif-common for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2016 Tom Van Herreweghe <[email protected]> |
7
|
|
|
* @license http://github.com/PHPExif/php-exif-common/blob/master/LICENSE MIT License |
8
|
|
|
* @category PHPExif |
9
|
|
|
* @package Common |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PHPExif\Common\Data\ValueObject; |
13
|
|
|
|
14
|
|
|
use PHPExif\Common\Data\ValueObject\StringObject; |
15
|
|
|
use \InvalidArgumentException; |
16
|
|
|
use \RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* FocalLength class |
20
|
|
|
* |
21
|
|
|
* A value object to describe the FocalLength data |
22
|
|
|
* |
23
|
|
|
* @category PHPExif |
24
|
|
|
* @package Common |
25
|
|
|
*/ |
26
|
|
|
class FocalLength extends StringObject |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param string $stringData |
30
|
|
|
* |
31
|
|
|
* @throws InvalidArgumentException If given argument is not a string |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
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 |
54
|
|
|
* |
55
|
|
|
* @param string $stringData |
56
|
|
|
* |
57
|
|
|
* @return FocalLength |
58
|
|
|
*/ |
59
|
|
|
public static function fromMM($stringData) |
60
|
|
|
{ |
61
|
|
|
if (!is_string($stringData)) { |
62
|
|
|
throw new InvalidArgumentException('Given data is not a string'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!preg_match('#^([0-9]+)mm$#', $stringData, $matches)) { |
66
|
|
|
throw new RuntimeException('Given focal length is not in a valid format. Need: "<number>mm"'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$numerator = $matches[1]; |
70
|
|
|
|
71
|
|
|
return new self( |
72
|
|
|
"{$numerator}/1" |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates a new instance from given FocalLength object |
78
|
|
|
* |
79
|
|
|
* @param FocalLength $focalLength |
80
|
|
|
* |
81
|
|
|
* @return FocalLength |
82
|
|
|
*/ |
83
|
|
|
public static function fromFocalLength(FocalLength $focalLength) |
84
|
|
|
{ |
85
|
|
|
return self::fromMM($focalLength->getStringData()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.