Skip to content

Commit 6b98587

Browse files
committed
initial commit
0 parents  commit 6b98587

18 files changed

+464
-0
lines changed

Diff for: .editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

Diff for: .gitattributes

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
composer.lock
3+
docs
4+
vendor

Diff for: .scrutinizer.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
filter:
2+
excluded_paths: [tests/*]
3+
4+
checks:
5+
php:
6+
remove_extra_empty_lines: true
7+
remove_php_closing_tag: true
8+
remove_trailing_whitespace: true
9+
fix_use_statements:
10+
remove_unused: true
11+
preserve_multiple: false
12+
preserve_blanklines: true
13+
order_alphabetically: true
14+
fix_php_opening_tag: true
15+
fix_linefeed: true
16+
fix_line_ending: true
17+
fix_identation_4spaces: true
18+
fix_doc_comments: true
19+

Diff for: .travis.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: php
2+
3+
php:
4+
- 5.5
5+
- 5.6
6+
- 7.0
7+
- hhvm
8+
9+
matrix:
10+
include:
11+
- php: 5.5
12+
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
13+
14+
before_script:
15+
- travis_retry composer self-update
16+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
17+
18+
script:
19+
- phpunit --coverage-text --coverage-clover=coverage.clover
20+
21+
after_script:
22+
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover

Diff for: CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Changelog
2+
3+
All Notable changes to `laravel-fractal` will be documented in this file
4+
5+
## NEXT - YYYY-MM-DD
6+
7+
### Added
8+
- Nothing
9+
10+
### Deprecated
11+
- Nothing
12+
13+
### Fixed
14+
- Nothing
15+
16+
### Removed
17+
- Nothing
18+
19+
### Security
20+
- Nothing

Diff for: CONTRIBUTING.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/thephpleague/laravel-fractal).
6+
7+
8+
## Pull Requests
9+
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
17+
18+
- **Create feature branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
23+
24+
25+
## Running Tests
26+
27+
``` bash
28+
$ phpunit
29+
```
30+
31+
32+
**Happy coding**!

Diff for: LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2015 Spatie bvba <[email protected]>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

Diff for: README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## WORK IN PROGRESS DO NOT USE
2+
3+
# laravel-fractal
4+
5+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-fractal.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-fractal)
6+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
7+
[![Build Status](https://img.shields.io/travis/spatie/laravel-fractal/master.svg?style=flat-square)](https://travis-ci.org/spatie/laravel-fractal)
8+
[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/xxxxxxxxx.svg?style=flat-square)](https://insight.sensiolabs.com/projects/xxxxxxxxx)
9+
[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/laravel-fractal.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/laravel-fractal)
10+
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-fractal.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-fractal)
11+
12+
This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what
13+
PSRs you support to avoid any confusion with users and contributors.
14+
15+
Spatie is webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).
16+
17+
## Install
18+
19+
You can install the package via composer:
20+
``` bash
21+
$ composer require spatie/laravel-fractal
22+
```
23+
24+
## Usage
25+
26+
``` php
27+
$skeleton = new Spatie\Skeleton();
28+
echo $skeleton->echoPhrase('Hello, Spatie!');
29+
```
30+
31+
## Change log
32+
33+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
34+
35+
## Testing
36+
37+
``` bash
38+
$ composer test
39+
```
40+
41+
## Contributing
42+
43+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
44+
45+
## Security
46+
47+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
48+
49+
## Credits
50+
51+
- [Freek Van der Herten](https://github.com/freekmurze)
52+
- [All Contributors](../../contributors)
53+
54+
## About Spatie
55+
Spatie is webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).
56+
57+
## License
58+
59+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Diff for: composer.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "spatie/laravel-fractal",
3+
"description": "A Fractal service provider for Laravel 5",
4+
"keywords": [
5+
"spatie",
6+
"laravel-fractal"
7+
],
8+
"homepage": "https://github.com/spatie/laravel-fractal",
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Freek Van der Herten",
13+
"email": "[email protected]",
14+
"homepage": "https://spatie.be",
15+
"role": "Developer"
16+
}
17+
],
18+
"require": {
19+
"php" : ">=5.5.0",
20+
"league/fractal": "^0.12.0",
21+
"illuminate/support": "^5.1"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit" : "4.*",
25+
"scrutinizer/ocular": "~1.1",
26+
"orchestra/testbench": "~3.0"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Spatie\\Fractal\\": "src"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Spatie\\Fractal\\Test\\": "tests"
36+
}
37+
},
38+
"scripts": {
39+
"test": "phpunit"
40+
}
41+
}

Diff for: phpunit.xml.dist

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="League Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

Diff for: resources/config/.gitkeep

Whitespace-only changes.

Diff for: resources/config/laravel-fractal.php

Whitespace-only changes.

Diff for: src/Fractal.php

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Spatie\Fractal;
4+
5+
use Illuminate\Contracts\Config\Repository;
6+
use League\Fractal\Manager;
7+
use League\Fractal\Resource\Collection;
8+
9+
class Fractal
10+
{
11+
/**
12+
* @var Manager
13+
*/
14+
protected $manager;
15+
16+
/**
17+
* @param \League\Fractal\TransformerAbstract|Callable $transformer
18+
*/
19+
protected $transformer;
20+
21+
protected $subject;
22+
/**
23+
* @var Repository
24+
*/
25+
protected $config;
26+
27+
public function __construct(Manager $manager, Repository $config)
28+
{
29+
$this->manager = $manager;
30+
$this->config = $config;
31+
}
32+
33+
/**
34+
* Set the collection or item that must be transformed.
35+
*
36+
* @param $subject
37+
*
38+
* @return $this
39+
*/
40+
public function setSubject($subject)
41+
{
42+
$this->subject = $subject;
43+
44+
return $this;
45+
}
46+
47+
/**
48+
* Set the class or function that will perform the transform.
49+
*
50+
* @param \League\Fractal\TransformerAbstract|Callable $transformer
51+
*
52+
* @return $this
53+
*/
54+
public function setTransformer($transformer)
55+
{
56+
$this->transformer = $transformer;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* Get the resource.
63+
*
64+
* @return Collection
65+
*/
66+
public function getResource()
67+
{
68+
return new Collection($this->subject, $this->transformer);
69+
}
70+
71+
/**
72+
* Get the data.
73+
*
74+
* @return \League\Fractal\Scope
75+
*/
76+
public function getData()
77+
{
78+
return $this->manager->createData($this->getResource());
79+
}
80+
81+
/**
82+
* Perform the transformation.
83+
*
84+
* @param string $format
85+
*
86+
* @return mixed
87+
*/
88+
public function transform($format = '')
89+
{
90+
if ($format == '') {
91+
$format == $this->getDefaultFormat();
92+
}
93+
94+
$conversionMethod = 'to'.ucFirst($format);
95+
96+
return $this->getData()->$conversionMethod();
97+
}
98+
99+
protected function getDefaultFormat()
100+
{
101+
return $this->config->get('laravel-fractal.default_output_format');
102+
}
103+
}

0 commit comments

Comments
 (0)