Skip to content

Commit cd43188

Browse files
author
Fabian Widmann
committed
Added Fractal Support for compactparameters
1 parent 2d1d408 commit cd43188

File tree

6 files changed

+145
-12
lines changed

6 files changed

+145
-12
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"require": {
1313
"mjaschen/phpgeo": "^1.3",
1414
"nesbot/carbon": "^1.22",
15-
"monolog/monolog": "~1.12"
15+
"monolog/monolog": "~1.12",
16+
"league/fractal": "^0.17.0"
1617
},
1718
"autoload": {
1819
"psr-0": {

composer.lock

+65-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dwd_geo_test.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
use FWidm\DWDHourlyCrawler\DWDLib;
44
use FWidm\DWDHourlyCrawler\Hourly\Variables\DWDHourlyParameters;
55
use FWidm\DWDHourlyCrawler\Model\DWDCompactParameter;
6+
use FWidm\DWDHourlyCrawler\Transformer\CompactParameterTransformer;
7+
use League\Fractal\Manager;
8+
use League\Fractal\Serializer\ArraySerializer;
9+
use League\Fractal\Serializer\DataArraySerializer;
610
use Location\Coordinate;
711
use Location\Formatter\Coordinate\GeoJSON;
812
use Carbon\Carbon;
@@ -65,5 +69,12 @@ function prettyPrint($obj)
6569
print "<hr>";
6670
$dwdCompact = new DWDCompactParameter(1,["a"=>"b"],"none",100.3,10,20,Carbon::now(),2030.45,"x");
6771
var_dump($dwdCompact);
68-
prettyPrint(json_encode([$dwdCompact],JSON_PRETTY_PRINT));
69-
prettyPrint($dwdCompact->getVarArray());
72+
73+
prettyPrint(json_encode($dwdCompact));
74+
$resource=$dwdCompact->toResource();
75+
76+
$manager = new Manager();
77+
$manager->setSerializer(new ArraySerializer());
78+
79+
// Run all transformers
80+
prettyPrint($manager->createData($resource)->toJson(JSON_PRETTY_PRINT));

src/fwidm/dwdHourlyCrawler/model/DWDCloudiness.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getDate(): DateTime
8484
}
8585

8686

87-
public function exportSingleVariables():array
87+
public function exportSingleVariables(): array
8888
{
8989
return [
9090
new DWDCompactParameter($this->stationId,

src/fwidm/dwdHourlyCrawler/model/DWDCompactParameter.php

+33-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
namespace FWidm\DWDHourlyCrawler\Model;
1010

1111
use Carbon\Carbon;
12+
use FWidm\DWDHourlyCrawler\Exceptions\DWDLibException;
13+
use FWidm\DWDHourlyCrawler\Transformer\CompactParameterTransformer;
14+
use League\Fractal\Manager;
15+
use League\Fractal\Resource\Item;
16+
use League\Fractal\Serializer\ArraySerializer;
17+
use League\Fractal\Serializer\DataArraySerializer;
1218

1319
/**
1420
* Class DWDCompactParameter
1521
* @package FWidm\DWDHourlyCrawler\fwidm\dwdHourlyCrawler\model
16-
* @author Fabian Widmann <fabian.widmann@uni-ulm.de>
22+
* @author Fabian Widmann <fabian.widmann@gmail.com>
1723
* target: json
1824
* "$variable": [
1925
* {
@@ -78,10 +84,7 @@ public function __construct($stationID, $description, $classification, $distance
7884
*/
7985
function jsonSerialize()
8086
{
81-
$vars = get_object_vars($this);
82-
//replace standard format by ISO DateTime::ATOM Format.
83-
$vars['date'] = $this->date->toIso8601String();
84-
return $vars;
87+
return $this->toArray();
8588
}
8689

8790
/**
@@ -161,9 +164,32 @@ public function __toString()
161164
return "DWDCompactParameter: [type=" . $this->type . "; classification=" . $this->getClassification() . "; value=" . $this->value . "]";
162165
}
163166

164-
public function getVarArray(): array
167+
/**
168+
* Applies the default (or given) transformer to this object, returns a fractal resource containing the values of the object.
169+
* @param string $customTransformer
170+
* @return Item
171+
*/
172+
public function toResource($customTransformer = CompactParameterTransformer::class): Item
165173
{
166-
return get_object_vars($this);
174+
try {
175+
$resource = new Item($this, new $customTransformer());
176+
return $resource;
177+
178+
} catch (\Error $e) {
179+
throw new DWDLibException("Specified transformer is not a class. Got transformer class=$customTransformer");
180+
}
167181
}
168182

183+
/** Uses the given serializer and transformer to transform $this into an array of the expected format.
184+
* @param string $serializer
185+
* @param string $transformer
186+
* @return array
187+
*/
188+
function toArray($serializer = ArraySerializer::class, $transformer = CompactParameterTransformer::class)
189+
{
190+
$resource = $this->toResource(new $transformer());
191+
$manager = new Manager();
192+
$manager->setSerializer(new $serializer());
193+
return $manager->createData($resource)->toArray();
194+
}
169195
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* User: Fabian Widmann
4+
* Date: 30.11.17
5+
* Time: 15:37
6+
*/
7+
8+
namespace FWidm\DWDHourlyCrawler\Transformer;
9+
10+
11+
use FWidm\DWDHourlyCrawler\Model\DWDCompactParameter;
12+
use League\Fractal\TransformerAbstract;
13+
14+
class CompactParameterTransformer extends TransformerAbstract
15+
{
16+
public function transform(DWDCompactParameter $parameter)
17+
{
18+
return [
19+
'station_id' => $parameter->getStationID(),
20+
'description' => $parameter->getDescription(),
21+
'classification' => $parameter->getClassification(),
22+
'distance' => $parameter->getDistance(),
23+
'lon' => $parameter->getLongitude(),
24+
'lat' => $parameter->getLatitude(),
25+
'date' => $parameter->getDate()->toIso8601String(),
26+
'value' => $parameter->getValue(),
27+
'type' => $parameter->getType(),
28+
];
29+
}
30+
31+
}

0 commit comments

Comments
 (0)