|
1
|
|
|
<?php |
|
2
|
|
|
namespace FilmTools\Developing; |
|
3
|
|
|
|
|
4
|
|
|
use FilmTools\Commons\Zones; |
|
5
|
|
|
use FilmTools\Commons\FStops; |
|
6
|
|
|
use FilmTools\Commons\Exposures; |
|
7
|
|
|
use FilmTools\Commons\ExposuresProviderInterface; |
|
8
|
|
|
use FilmTools\Commons\Densities; |
|
9
|
|
|
use FilmTools\Commons\DensitiesProviderInterface; |
|
10
|
|
|
|
|
11
|
|
|
class DevelopingFactory |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* PHP class name (FQDN) of the DevelopingInterface instance this factory produces. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
public $developing_php_class; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string|null $developing_php_class DevelopingInterface instance FQDN |
|
24
|
|
|
* |
|
25
|
|
|
* @throws InvalidArgumentException If FQDN does not implement DevelopingInterface |
|
26
|
|
|
*/ |
|
27
|
33 |
|
public function __construct( string $developing_php_class = null ) |
|
28
|
|
|
{ |
|
29
|
33 |
|
$this->developing_php_class = $developing_php_class ?: Developing::class; |
|
30
|
|
|
|
|
31
|
33 |
|
if (!is_subclass_of($this->developing_php_class, DevelopingInterface::class )): |
|
|
|
|
|
|
32
|
3 |
|
$msg = sprintf("Class name must implement '%s'.", DevelopingInterface::class); |
|
33
|
3 |
|
throw new DevelopingInvalidArgumentException( $msg ); |
|
34
|
|
|
endif; |
|
35
|
30 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The factory method. |
|
40
|
|
|
* |
|
41
|
|
|
* Expects an array or ArrayAccess with at least elements "time", "densities", and "exposures". |
|
42
|
|
|
* |
|
43
|
|
|
* If no "exposures" are given, but "zones" numbers are instead, the zone numbers |
|
44
|
|
|
* will be converted internally. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array|ArrayAccess $developing |
|
47
|
|
|
* @return DevelopingInterface |
|
48
|
|
|
*/ |
|
49
|
30 |
|
public function __invoke( $developing ) |
|
50
|
|
|
{ |
|
51
|
30 |
|
if (!is_array($developing) and !$developing instanceOf \ArrayAccess) |
|
52
|
3 |
|
throw new DevelopingInvalidArgumentException("Array or ArrayAccess expected"); |
|
53
|
|
|
|
|
54
|
27 |
|
$densities = $this->extractDensities($developing); |
|
55
|
27 |
|
$exposures = $this->extractExposures($developing); |
|
56
|
27 |
|
$time = $this->extractTime($developing); |
|
57
|
|
|
|
|
58
|
12 |
|
$developing_php_class = $this->developing_php_class; |
|
59
|
12 |
|
return new $developing_php_class( $exposures, $densities, $time ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
27 |
|
protected function extractDensities( $developing ) : DensitiesProviderInterface |
|
64
|
|
|
{ |
|
65
|
27 |
|
$densities = $developing['densities'] ?? array(); |
|
66
|
|
|
|
|
67
|
27 |
|
return new Densities($densities); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
27 |
|
protected function extractExposures( $developing ) : ExposuresProviderInterface |
|
72
|
|
|
{ |
|
73
|
27 |
|
$fstops = $developing['fstops'] ?? array(); |
|
74
|
27 |
|
$zones = $developing['zones'] ?? array(); |
|
75
|
27 |
|
$exposures = $developing['exposures'] ?? array(); |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
27 |
|
if (empty($exposures) and !empty($zones)): |
|
79
|
18 |
|
$exposures = new Zones( $zones ); |
|
80
|
9 |
|
elseif (empty($exposures) and !empty($fstops)): |
|
81
|
3 |
|
$exposures = new FStops( $fstops ); |
|
82
|
|
|
else: |
|
83
|
6 |
|
$exposures = new Exposures( $exposures ); |
|
84
|
|
|
endif; |
|
85
|
|
|
|
|
86
|
27 |
|
return $exposures; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
27 |
|
protected function extractTime( $developing ) : int |
|
91
|
|
|
{ |
|
92
|
27 |
|
if (!array_key_exists("time", $developing) |
|
93
|
27 |
|
and !array_key_exists("seconds", $developing)) |
|
94
|
3 |
|
throw new NoTimeGivenException("The data array must contain either 'time' or 'seconds' element."); |
|
95
|
|
|
|
|
96
|
24 |
|
$time = $developing['seconds'] ?? ($developing['time'] ?? false); |
|
97
|
|
|
|
|
98
|
24 |
|
if (filter_var($time, \FILTER_VALIDATE_INT, ['options' => array( 'min_range' => 0 )]) === false) |
|
99
|
12 |
|
throw new NoTimeGivenException("The developing time must be integer (positive or 0)."); |
|
100
|
|
|
|
|
101
|
12 |
|
return $time; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|