1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Exif LineResolution 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 \InvalidArgumentException; |
15
|
|
|
use \RuntimeException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* LineResolution class |
19
|
|
|
* |
20
|
|
|
* A value object to describe the LineResolution data |
21
|
|
|
* |
22
|
|
|
* @category PHPExif |
23
|
|
|
* @package Common |
24
|
|
|
*/ |
25
|
|
|
class LineResolution extends MeasuredObject |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* |
30
|
|
|
* @throws InvalidArgumentException If given argument is not a string |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
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 |
54
|
|
|
* |
55
|
|
|
* @param LineResolution $resolution |
56
|
|
|
* |
57
|
|
|
* @return LineResolution |
58
|
|
|
*/ |
59
|
|
|
public static function fromLineResolution(LineResolution $resolution) |
60
|
|
|
{ |
61
|
|
|
return new self( |
62
|
|
|
$resolution->getValue() . '/1', |
63
|
|
|
(clone $resolution->getUnit()) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creates a new instance with given value and a |
69
|
|
|
* Unit of "dpi" |
70
|
|
|
* |
71
|
|
|
* @param mixed $value |
72
|
|
|
* |
73
|
|
|
* @return LineResolution |
74
|
|
|
*/ |
75
|
|
|
public static function dpi($value) |
76
|
|
|
{ |
77
|
|
|
return new self( |
78
|
|
|
$value, |
79
|
|
|
new Unit('dpi') |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.