1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace leinonen\DataLoader; |
4
|
|
|
|
5
|
|
|
class DataLoaderOptions |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var bool |
9
|
|
|
*/ |
10
|
|
|
private $shouldBatch; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var null|int |
14
|
|
|
*/ |
15
|
|
|
private $maxBatchSize; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
private $shouldCache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initiates new DataLoaderOptions. |
24
|
|
|
* |
25
|
|
|
* @param null|int $maxBatchSize |
26
|
|
|
* @param bool $shouldBatch |
27
|
|
|
* @param bool $shouldCache |
28
|
|
|
*/ |
29
|
36 |
|
public function __construct( |
30
|
|
|
$maxBatchSize = null, |
31
|
|
|
$shouldBatch = true, |
32
|
|
|
$shouldCache = true |
33
|
|
|
) { |
34
|
36 |
|
$this->validateOptions($maxBatchSize, $shouldBatch, $shouldCache); |
35
|
33 |
|
$this->shouldBatch = $shouldBatch; |
36
|
33 |
|
$this->maxBatchSize = $maxBatchSize; |
37
|
33 |
|
$this->shouldCache = $shouldCache; |
38
|
33 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
30 |
|
public function shouldBatch() |
44
|
|
|
{ |
45
|
30 |
|
return $this->shouldBatch; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return null|int |
50
|
|
|
*/ |
51
|
29 |
|
public function getMaxBatchSize() |
52
|
|
|
{ |
53
|
29 |
|
return $this->maxBatchSize; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
31 |
|
public function shouldCache() |
60
|
|
|
{ |
61
|
31 |
|
return $this->shouldCache; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Validates the options. |
66
|
|
|
* |
67
|
|
|
* @param null|int $maxBatchSize |
68
|
|
|
* @param bool $shouldBatch |
69
|
|
|
* @param bool $shouldCache |
70
|
|
|
*/ |
71
|
36 |
|
private function validateOptions($maxBatchSize, $shouldBatch, $shouldCache) |
72
|
|
|
{ |
73
|
36 |
|
$this->validateMaxBatchSizeOption($maxBatchSize); |
74
|
35 |
|
$this->validateBatchOption($shouldBatch); |
75
|
34 |
|
$this->validateCacheOption($shouldCache); |
76
|
33 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param bool $shouldBatch |
80
|
|
|
*/ |
81
|
35 |
|
private function validateBatchOption($shouldBatch) |
82
|
|
|
{ |
83
|
35 |
|
if (!\is_bool($shouldBatch)) { |
84
|
1 |
|
throw new \InvalidArgumentException('Expected argument $shouldBatch to be a boolean'); |
85
|
|
|
} |
86
|
34 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool $shouldCache |
90
|
|
|
*/ |
91
|
34 |
|
private function validateCacheOption($shouldCache) |
92
|
|
|
{ |
93
|
34 |
|
if (!\is_bool($shouldCache)) { |
94
|
1 |
|
throw new \InvalidArgumentException('Expected argument $shouldCache to be a boolean'); |
95
|
|
|
} |
96
|
33 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param null|int $maxBatchSize |
100
|
|
|
*/ |
101
|
36 |
|
private function validateMaxBatchSizeOption($maxBatchSize) |
102
|
|
|
{ |
103
|
36 |
|
if ($maxBatchSize !== null && !\is_int($maxBatchSize)) { |
104
|
1 |
|
throw new \InvalidArgumentException('Expected argument $maxBatchSize to be null or an integer'); |
105
|
|
|
} |
106
|
35 |
|
} |
107
|
|
|
} |
108
|
|
|
|