Skip to content

Commit b704289

Browse files
Use public properties instead of array
1 parent 4e5031f commit b704289

23 files changed

+256
-184
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
},
1515
"require": {
1616
"php" : "^7.1",
17-
"symfony/serializer": "^4.3"
17+
"symfony/serializer": "^4.3",
18+
"ext-curl": "*"
1819
},
1920
"require-dev": {
20-
"phpunit/phpunit": "^7.5"
21+
"phpunit/phpunit": "^7.5",
22+
"symfony/var-dumper": "^5.0"
2123
}
2224
}

composer.lock

Lines changed: 136 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Extractor/AbstractExtractor.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ abstract class AbstractExtractor implements ExtractInterface
1111

1212
/**
1313
* @param array $requiredFields
14-
* @param array $params
1514
* @throws MissingParameter
1615
*/
17-
protected function requiredParameters(array $requiredFields, array $params)
16+
protected function requiredParameters(array $requiredFields): void
1817
{
1918
foreach ($requiredFields as $field) {
20-
if (!isset($params[$field])) {
19+
if (null === $this->$field) {
2120
throw new MissingParameter('Parameter "'.$field.'" is missing.');
2221
}
2322
}

src/Extractor/Extension/SupportDecoders.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
trait SupportDecoders
1414
{
15+
public $decoder;
16+
17+
public $contextDecoder = [];
18+
1519
/**
1620
* Content-Type decoders matches.
1721
*/
@@ -91,14 +95,13 @@ protected function findDecoder($contentType)
9195
/**
9296
* @param $body
9397
* @param $typeMime
94-
* @param array $context
9598
* @return mixed
9699
* @throws BadInterface
97100
* @throws DecoderNotFound
98101
*/
99-
protected function decode($body, $typeMime, $context = [])
102+
protected function decode($body, $typeMime)
100103
{
101-
$contextDecoder = $context['decoder'] ?? [];
104+
$contextDecoder = $this->decoder ?? [];
102105
$decoderClass = $contextDecoder['class'] ?? $this->findDecoder($typeMime);
103106

104107
if (null === $decoderClass) {
@@ -114,6 +117,6 @@ protected function decode($body, $typeMime, $context = [])
114117

115118
$format = defined($decoderClass.'::FORMAT') ? $decoderClass::FORMAT : null;
116119

117-
return $decoder->decode($body, $format, $context);
120+
return $decoder->decode($body, $format, $this->contextDecoder);
118121
}
119122
}

src/Extractor/ExtractInterface.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,5 @@
44

55
interface ExtractInterface
66
{
7-
/**
8-
* @param array $params
9-
* @return iterable
10-
*/
11-
public function __invoke(array $params = []): iterable;
7+
public function __invoke(): iterable;
128
}

src/Extractor/Http.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,57 @@
33
namespace Camillebaronnet\ETL\Extractor;
44

55
use Camillebaronnet\ETL\Exception\BadInterface;
6+
use Camillebaronnet\ETL\Exception\DecoderNotFound;
67
use Camillebaronnet\ETL\Exception\MissingParameter;
78

89
class Http extends AbstractExtractor
910
{
10-
/**
11-
* Default context.
12-
*/
13-
public const DEFAULT_CONTEXT = [
14-
'method' => 'GET',
15-
'data' => null,
16-
'headers' => [],
17-
'curl_opts' => [
18-
CURLOPT_RETURNTRANSFER => true,
19-
CURLOPT_FOLLOWLOCATION => true,
20-
CURLOPT_USERAGENT => '-',
21-
CURLOPT_AUTOREFERER => true,
22-
CURLOPT_SSL_VERIFYHOST => 2,
23-
CURLOPT_SSL_VERIFYPEER => true,
24-
],
11+
public $method = 'GET';
12+
public $url;
13+
public $data;
14+
public $curlOpts = [
15+
CURLOPT_RETURNTRANSFER => true,
16+
CURLOPT_FOLLOWLOCATION => true,
17+
CURLOPT_USERAGENT => '-',
18+
CURLOPT_AUTOREFERER => true,
19+
CURLOPT_SSL_VERIFYHOST => 2,
20+
CURLOPT_SSL_VERIFYPEER => true,
2521
];
2622

2723
/**
28-
* @param array $context
29-
* @return iterable
30-
* @throws MissingParameter
3124
* @throws BadInterface
25+
* @throws MissingParameter
26+
* @throws DecoderNotFound
3227
*/
33-
public function __invoke(array $context = []): iterable
28+
public function __invoke(): iterable
3429
{
35-
$this->requiredParameters(['url'], $context);
30+
$this->requiredParameters(['url']);
3631

37-
$context = array_merge(static::DEFAULT_CONTEXT, $context);
3832
$curl_opts = [
39-
CURLOPT_URL => $context['url'],
40-
CURLOPT_CUSTOMREQUEST => strtoupper($context['method']),
33+
CURLOPT_URL => $this->url,
34+
CURLOPT_CUSTOMREQUEST => strtoupper($this->method),
4135
];
4236

43-
if (null !== $context['data']) {
37+
if (null !== $this->data) {
4438
$curl_opts += [
4539
CURLOPT_POST => true,
46-
CURLOPT_POSTFIELDS => $context['data'],
40+
CURLOPT_POSTFIELDS => $this->data,
4741
];
4842
}
4943

50-
if ('HEAD' === strtoupper($context['method'])) {
44+
if ('HEAD' === strtoupper($this->method)) {
5145
$curl_opts += [CURLOPT_NOBODY => 1];
5246
}
5347

5448
$ch = curl_init();
55-
curl_setopt_array($ch, $curl_opts + $context['curl_opts']);
49+
curl_setopt_array($ch, $curl_opts + $this->curlOpts);
5650
$content = curl_exec($ch);
5751
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
5852
curl_close($ch);
5953

6054
return $this->decode(
6155
$content,
62-
$contentType,
63-
$context
56+
$contentType
6457
);
6558
}
6659
}

src/Loader/Display.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,14 @@
66

77
class Display implements LoaderInterface, StreamLoaderInterface
88
{
9-
/**
10-
* @param array $data
11-
* @param array $params
12-
* @return array
13-
*/
14-
public function __invoke(array $data, array $params = [])
9+
public function __invoke(array $data): void
1510
{
16-
return print_r($data);
11+
print_r($data);
1712
}
1813

19-
/**
20-
* @param Generator $collection
21-
* @param array $params
22-
* @return void
23-
*/
24-
public function stream(Generator $collection, array $params = [])
14+
public function stream(Generator $collection): void
2515
{
26-
foreach($collection as $row){
16+
foreach ($collection as $row) {
2717
print_r($row);
2818
}
2919
}

src/Loader/Json.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
class Json implements LoaderInterface
88
{
9-
/**
10-
* @param array $data
11-
* @param array $params
12-
* @return array
13-
*/
14-
public function __invoke(array $data, array $params = [])
9+
public $context = [];
10+
11+
public function __invoke(array $data)
1512
{
16-
return (new JsonEncoder())->encode($data, JsonEncoder::FORMAT, $params);
13+
return (new JsonEncoder())->encode($data, JsonEncoder::FORMAT, $this->context);
1714
}
1815
}

src/Loader/LoaderInterface.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,5 @@
44

55
interface LoaderInterface
66
{
7-
/**
8-
* @param array $collection
9-
* @param array $context
10-
* @return mixed
11-
*/
12-
public function __invoke(array $collection, array $context = []);
7+
public function __invoke(array $collection);
138
}

src/Loader/StreamLoaderInterface.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,5 @@
66

77
interface StreamLoaderInterface
88
{
9-
/**
10-
* @param Generator $collection
11-
* @param array $context
12-
* @return void
13-
*/
14-
public function stream(Generator $collection, array $context = []);
9+
public function stream(Generator $collection): void;
1510
}

0 commit comments

Comments
 (0)