Skip to content

Commit 9e367c1

Browse files
committed
add createData function
1 parent 91a7a5b commit 9e367c1

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Fractal.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function transformWith($transformer)
110110
}
111111

112112
/**
113-
* Specify the includes
113+
* Specify the includes.
114114
*
115115
* @param array|string $includes Array or csv string of resources to include
116116
*
@@ -188,6 +188,21 @@ public function toArray()
188188
* @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified
189189
*/
190190
protected function transform($conversionMethod)
191+
{
192+
$fractalData = $this->createData();
193+
194+
return $fractalData->$conversionMethod();
195+
}
196+
197+
/**
198+
* Create fractal data.
199+
*
200+
* @return \League\Fractal\Scope
201+
*
202+
* @throws \Spatie\Fractal\Exceptions\InvalidTransformation
203+
* @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified
204+
*/
205+
public function createData()
191206
{
192207
if (is_null($this->transformer)) {
193208
throw new NoTransformerSpecified();
@@ -203,9 +218,7 @@ protected function transform($conversionMethod)
203218

204219
$resource = $this->getResource();
205220

206-
$fractalData = $this->manager->createData($resource);
207-
208-
return $fractalData->$conversionMethod();
221+
return $this->manager->createData($resource);
209222
}
210223

211224
/**

tests/Integration/FractalTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,16 @@ public function it_can_create_a_resource()
9797

9898
$this->assertInstanceOf(\League\Fractal\Resource\ResourceInterface::class, $resource);
9999
}
100+
101+
/**
102+
* @test
103+
*/
104+
public function it_can_create_fractal_data()
105+
{
106+
$resource = $this->fractal
107+
->collection($this->testBooks, new TestTransformer())
108+
->createData();
109+
110+
$this->assertInstanceOf(\League\Fractal\Scope::class, $resource);
111+
}
100112
}

0 commit comments

Comments
 (0)