├── .coveralls.yml
├── .github
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ ├── feature_request.md
│ └── other.md
├── dependabot.yml
└── workflows
│ └── build.yaml
├── .gitignore
├── .mergify.yml
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── Version
├── composer.json
├── config
└── json-mapper.php
├── phpstan.neon.dist
├── phpunit.xml.dist
├── src
├── JsonMapper.php
├── JsonMapperInterface.php
└── ServiceProvider.php
└── tests
├── Implementation
├── ChuckNorris
│ ├── Joke.php
│ └── SearchResponse.php
├── EloquentModel.php
└── SimpleObject.php
├── Integration
└── JsonMapperTests.php
├── Unit
├── JsonMapperTest.php
└── ServiceProviderTest.php
└── database
└── migrations
└── 2020_08_25_000000_create_eloquent_models_table.php
/.coveralls.yml:
--------------------------------------------------------------------------------
1 | coverage_clover: build/logs/clover-*.xml
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | labels: bug
5 | ---
6 |
7 | **Describe the bug**
8 | A clear and concise description of what the bug is.
9 |
10 | **To Reproduce**
11 | Steps to reproduce the behavior:
12 | 1. When calling '...'
13 | 2. At the JSON contains '....'
14 | 3. Add Im using the following middleware '....'
15 | 4. See error
16 |
17 | **Expected behavior**
18 | A clear and concise description of what you expected to happen.
19 |
20 | **Stacktrace**
21 | If applicable, add the stack trace to help explain your problem.
22 |
23 | **Environment (please complete the following information):**
24 | - PHP: [e.g. 7.4.1]
25 |
26 | **Additional context**
27 | Add any other context about the problem here.
28 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | labels: feature-request
5 | ---
6 |
7 | **Is your feature request related to a problem? Please describe.**
8 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
9 |
10 | **Describe the solution you'd like**
11 | A clear and concise description of what you want to happen.
12 |
13 | **Describe alternatives you've considered**
14 | A clear and concise description of any alternative solutions or features you've considered.
15 |
16 | **Additional context**
17 | Add any other context or screenshots about the feature request here.
18 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/other.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Other
3 | about: Your issue doesn't fit the above
4 | ---
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: composer
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | time: "04:00"
8 | open-pull-requests-limit: 10
9 |
--------------------------------------------------------------------------------
/.github/workflows/build.yaml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on:
4 | push:
5 | branches: [ develop, master ]
6 | pull_request:
7 | branches: [ develop, master ]
8 |
9 | jobs:
10 | build:
11 | name: PHP ${{ matrix.name }}
12 | strategy:
13 | matrix:
14 | include:
15 | - php: 7.2
16 | allow_fail: false
17 | name: 'PHP 7.2 with latest deps'
18 | - php: 7.2
19 | allow_fail: false
20 | composer_update_flags: '--prefer-lowest --prefer-stable'
21 | name: 'PHP 7.2 with lowest stable deps'
22 | - php: 7.3
23 | allow_fail: false
24 | name: 'PHP 7.3 with latest deps'
25 | - php: 7.3
26 | allow_fail: false
27 | composer_update_flags: '--prefer-lowest --prefer-stable'
28 | name: 'PHP 7.3 with lowest stable deps'
29 | - php: 7.4
30 | allow_fail: false
31 | name: 'PHP 7.4 with latest deps'
32 | - php: 7.4
33 | allow_fail: false
34 | composer_update_flags: '--prefer-lowest --prefer-stable'
35 | name: 'PHP 7.4 with lowest stable deps'
36 | - php: 8.0
37 | allow_fail: false
38 | php_ini: 'xdebug.coverage_enable=On'
39 | name: 'PHP 8.0 with latest deps'
40 | - php: 8.0
41 | allow_fail: false
42 | composer_update_flags: '--prefer-lowest --prefer-stable'
43 | php_ini: 'xdebug.coverage_enable=On'
44 | name: 'PHP 8.0 with lowest stable deps'
45 | - php: 8.1
46 | allow_fail: true
47 | php_ini: 'xdebug.coverage_enable=On'
48 | name: 'PHP 8.1 with latest deps'
49 | - php: 8.1
50 | allow_fail: true
51 | composer_update_flags: '--prefer-lowest --prefer-stable'
52 | php_ini: 'xdebug.coverage_enable=On'
53 | name: 'PHP 8.1 with lowest stable deps'
54 | - php: 8.2
55 | allow_fail: true
56 | php_ini: 'xdebug.coverage_enable=On'
57 | name: 'PHP 8.2 with latest deps'
58 | - php: 8.2
59 | allow_fail: true
60 | composer_update_flags: '--prefer-lowest --prefer-stable'
61 | php_ini: 'xdebug.coverage_enable=On'
62 | name: 'PHP 8.2 with lowest stable deps'
63 | - php: 8.3
64 | allow_fail: true
65 | php_ini: 'xdebug.coverage_enable=On'
66 | name: 'PHP 8.3 with latest deps'
67 | - php: 8.3
68 | allow_fail: true
69 | composer_update_flags: '--prefer-lowest --prefer-stable'
70 | php_ini: 'xdebug.coverage_enable=On'
71 | name: 'PHP 8.3 with lowest stable deps'
72 | - php: 8.4
73 | allow_fail: true
74 | php_ini: 'xdebug.coverage_enable=On'
75 | name: 'PHP 8.4 with latest deps'
76 | - php: 8.4
77 | allow_fail: true
78 | composer_update_flags: '--prefer-lowest --prefer-stable'
79 | php_ini: 'xdebug.coverage_enable=On'
80 | name: 'PHP 8.4 with lowest stable deps'
81 |
82 | runs-on: ubuntu-latest
83 |
84 | steps:
85 | - uses: actions/checkout@v2
86 |
87 | - name: Validate composer.json and composer.lock
88 | run: composer validate
89 |
90 | - name: Setup PHP ${{ matrix.php }}
91 | uses: shivammathur/setup-php@v2
92 | with:
93 | php-version: ${{ matrix.php }}
94 | coverage: xdebug
95 | ini-values: ${{ matrix.php_ini }}
96 |
97 | - name: Install dependencies
98 | run: composer install ${{ matrix.composer_flags }}
99 | continue-on-error: ${{ matrix.allow_fail }}
100 |
101 | - name: Update dependencies
102 | if: matrix.composer_update_flags
103 | run: composer update ${{ matrix.composer_update_flags }} && composer update phpunit/phpunit --with-dependencies
104 | continue-on-error: ${{ matrix.allow_fail }}
105 |
106 | - name: Create log folder
107 | run: mkdir -p build/logs;
108 |
109 | - name: Run unit tests
110 | run: composer unit-tests
111 | continue-on-error: ${{ matrix.allow_fail }}
112 |
113 | - name: Upload test coverage
114 | run: php vendor/bin/php-coveralls -vvv
115 | env:
116 | COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
117 | COVERALLS_PARALLEL: true
118 | COVERALLS_FLAG_NAME: ${{ matrix.name }}
119 | continue-on-error: ${{ matrix.allow_fail }}
120 |
121 | finish:
122 | needs: build
123 | runs-on: ubuntu-latest
124 | steps:
125 | - name: Coveralls Finished
126 | run: |
127 | curl --header "Content-Type: application/json" \
128 | --request POST \
129 | --data '{"repo_token":"${{ secrets.GITHUB_TOKEN }}","repo_name":"JsonMapper/LaravelPackage", "payload": {"build_num": "${{ github.sha }}", "status": "done"}}' \
130 | https://coveralls.io/webhook
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | composer.phar
2 | composer.lock
3 | /vendor/
4 | /.idea/
5 | /.phpunit.result.cache
6 | /build
--------------------------------------------------------------------------------
/.mergify.yml:
--------------------------------------------------------------------------------
1 | pull_request_rules:
2 | - name: automatic merge for Dependabot pull requests
3 | conditions:
4 | - author~=^dependabot(|-preview)\[bot\]$
5 | - status-success=Build
6 | actions:
7 | merge:
8 | method: squash
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 | All notable changes to this project will be documented in this file.
3 |
4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6 |
7 | ## [Unreleased]
8 |
9 | ## [2.6.0] - 2025-02-17
10 | ### Added
11 | - Support Laravel 12 [PR#23](https://github.com/JsonMapper/LaravelPackage/pull/23). Thanks to [laravel-shift](https://github.com/laravel-shift) for creating the PR.
12 | - Extend workflow matrix to include PHP 8.2, 8.3 and 8.4 [PR#24](https://github.com/JsonMapper/LaravelPackage/pull/24)
13 |
14 | ## [2.5.0] - 2024-04-05
15 | ### Added
16 | - Support Laravel 11 [PR#21](https://github.com/JsonMapper/LaravelPackage/pull/21). Thanks to [laravel-shift](https://github.com/laravel-shift) for creating the PR.
17 |
18 | ## [2.4.0] - 2023-04-04
19 | ### Added
20 | - Support Laravel 10 [PR#19](https://github.com/JsonMapper/LaravelPackage/pull/19). Thanks to [Sunil Bhatia](https://github.com/sunil19822701) for creating the PR.
21 |
22 | ## [2.3.0] - 2022-02-15
23 | ### Added
24 | - Support Laravel 9 [PR#17](https://github.com/JsonMapper/LaravelPackage/pull/17)
25 |
26 | ## [2.2.0] - 2021-03-18
27 | ### Added
28 | - Utilise the new builder factory; Add mapToCollection methods [PR#13](https://github.com/JsonMapper/LaravelPackage/pull/13)
29 |
30 | ## [2.1.0] - 2021-01-28
31 | ### Changed
32 | - Migrate to GitHub actions [PR#12](https://github.com/JsonMapper/LaravelPackage/pull/12)
33 |
34 | ## [2.0.0] - 2021-01-07
35 | ### Changed
36 | - Update to version ^2.0 JsonMapper
37 |
38 | ## [1.1.3] - 2020-09-14
39 | ### Added
40 | - Add support for Laravel 8
41 |
42 | ## [1.1.2] - 2020-07-12
43 | ### Fixed
44 | - Late config binding added for runtime override
45 |
46 | ## [1.1.1] - 2020-07-12
47 | ### Fixed
48 | - Make JsonMapperInterface available in ServiceProvider
49 |
50 | ## [1.1.0] - 2020-07-12
51 | ### Fixed
52 | - Optimised composer dependencies. [PR#2](https://github.com/JsonMapper/LaravelPackage/pull/2)
53 |
54 | ## [1.0.1] - 2020-04-28
55 | ### Fixed
56 | - Fixed readme shields
57 |
58 | ## [1.0.0] - 2020-04-27
59 | ### Added
60 | - Add initial ServiceProvider and package config
61 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as
6 | contributors and maintainers pledge to making participation in our project and
7 | our community a harassment-free experience for everyone, regardless of age, body
8 | size, disability, ethnicity, sex characteristics, gender identity and expression,
9 | level of experience, education, socio-economic status, nationality, personal
10 | appearance, race, religion, or sexual identity and orientation.
11 |
12 | ## Our Standards
13 |
14 | Examples of behavior that contributes to creating a positive environment
15 | include:
16 |
17 | * Using welcoming and inclusive language
18 | * Being respectful of differing viewpoints and experiences
19 | * Gracefully accepting constructive criticism
20 | * Focusing on what is best for the community
21 | * Showing empathy towards other community members
22 |
23 | Examples of unacceptable behavior by participants include:
24 |
25 | * The use of sexualized language or imagery and unwelcome sexual attention or
26 | advances
27 | * Trolling, insulting/derogatory comments, and personal or political attacks
28 | * Public or private harassment
29 | * Publishing others' private information, such as a physical or electronic
30 | address, without explicit permission
31 | * Other conduct which could reasonably be considered inappropriate in a
32 | professional setting
33 |
34 | ## Our Responsibilities
35 |
36 | Project maintainers are responsible for clarifying the standards of acceptable
37 | behavior and are expected to take appropriate and fair corrective action in
38 | response to any instances of unacceptable behavior.
39 |
40 | Project maintainers have the right and responsibility to remove, edit, or
41 | reject comments, commits, code, wiki edits, issues, and other contributions
42 | that are not aligned to this Code of Conduct, or to ban temporarily or
43 | permanently any contributor for other behaviors that they deem inappropriate,
44 | threatening, offensive, or harmful.
45 |
46 | ## Scope
47 |
48 | This Code of Conduct applies both within project spaces and in public spaces
49 | when an individual is representing the project or its community. Examples of
50 | representing a project or community include using an official project e-mail
51 | address, posting via an official social media account, or acting as an appointed
52 | representative at an online or offline event. Representation of a project may be
53 | further defined and clarified by project maintainers.
54 |
55 | ## Enforcement
56 |
57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
58 | reported by contacting the project team at danny.vandersluijs@icloud.com. All
59 | complaints will be reviewed and investigated and will result in a response that
60 | is deemed necessary and appropriate to the circumstances. The project team is
61 | obligated to maintain confidentiality with regard to the reporter of an incident.
62 | Further details of specific enforcement policies may be posted separately.
63 |
64 | Project maintainers who do not follow or enforce the Code of Conduct in good
65 | faith may face temporary or permanent repercussions as determined by other
66 | members of the project's leadership.
67 |
68 | ## Attribution
69 |
70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72 |
73 | [homepage]: https://www.contributor-covenant.org
74 |
75 | For answers to common questions about this code of conduct, see
76 | https://www.contributor-covenant.org/faq
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing to JsonMapper Laravel package
2 |
3 | :+1::tada: First off, thanks for taking the time to contribute! :tada::+1:
4 |
5 | Since JsonMapper Laravel package currently only is a small project (though it has great ambitions) we welcome any contribution
6 | that helps us move forward. The project could use your help.
7 |
8 | ## Ideal pull request
9 | To avoid going back and forth the project `composer.json` was setup in such a way that it allows you to run
10 | the same checks we do. These checks are:
11 | ```bash
12 | composer unit-tests # For running the unit tests
13 | composer phpcbf # For applying the correct code style (PSR-12) to the sources
14 | composer phpcs # For scanning the sources for the correct style (PSR-12) being used
15 | composer phpstan # For analysing the sources for potentional bugs
16 | ```
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Danny van der Sluijs
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 all
13 | 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 THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | ---
7 | **This is a Laravel package for using JsonMapper in you Laravel application.**
8 |
9 | JsonMapper is a PHP library that allows you to map a JSON response to your PHP objects that are either annotated using doc blocks or use typed properties.
10 | For more information see the project website: https://jsonmapper.net
11 |
12 | 
13 | 
14 | 
15 | 
16 | [](https://coveralls.io/github/JsonMapper/LaravelPackage?branch=master)
17 |
18 | # Why use JsonMapper
19 | Continuously mapping your JSON responses to your own objects becomes tedious and is error prone. Not mentioning the
20 | tests that needs to be written for said mapping.
21 |
22 | JsonMapper has been build with the most common usages in mind. In order to allow for those edge cases which are not
23 | supported by default, it can easily be extended as its core has been designed using middleware.
24 |
25 | JsonMapper supports the following features
26 | * Case conversion
27 | * Debugging
28 | * DocBlock annotations
29 | * Final callback
30 | * Namespace resolving
31 | * PHP 7.4 Types properties
32 |
33 | # Installing JsonMapper laravel package
34 | The installation of JsonMapper Laravel package can easily be done with [Composer](https://getcomposer.org)
35 | ```bash
36 | $ composer require json-mapper/laravel-package
37 | ```
38 | The example shown above assumes that `composer` is on your `$PATH`.
39 |
40 | # Contributing
41 | Please refer to [CONTRIBUTING.md](https://github.com/JsonMapper/LaravelPackage/blob/master/CONTRIBUTING.md) for information on how to contribute to JsonMapper Laravel package.
42 |
43 | ## List of Contributors
44 | Thanks to everyone who has contributed to JsonMapper Laravel package! You can find a detailed list of contributors of JsonMapper on [GitHub](https://github.com/JsonMapper/LaravelPackage/graphs/contributors).
45 |
46 | # License
47 | The MIT License (MIT). Please see [License File](https://github.com/JsonMapper/LaravelPackage/blob/master/LICENSE) for more information.
48 |
--------------------------------------------------------------------------------
/Version:
--------------------------------------------------------------------------------
1 | 2.6.0
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "json-mapper/laravel-package",
3 | "description": "The JsonMapper package for Laravel",
4 | "keywords": [
5 | "json",
6 | "mapper",
7 | "JsonMapper",
8 | "middleware",
9 | "laravel"
10 | ],
11 | "type": "library",
12 | "license": "MIT",
13 | "minimum-stability": "stable",
14 | "require": {
15 | "json-mapper/json-mapper": "^2.3",
16 | "php": "^7.2 || ^8.0",
17 | "illuminate/support": "^5.5|^6|^7|^8|^9|^10 || ^11.0 || ^12.0"
18 | },
19 | "autoload": {
20 | "psr-4": {
21 | "JsonMapper\\LaravelPackage\\": "src/"
22 | }
23 | },
24 | "autoload-dev": {
25 | "psr-4": {
26 | "JsonMapper\\LaravelPackage\\Tests\\": "tests/"
27 | }
28 | },
29 | "scripts": {
30 | "phpcs": "phpcs --standard=PSR12 src tests",
31 | "phpcbf": "phpcbf --standard=PSR12 src tests",
32 | "phpstan": "phpstan analyse",
33 | "unit-tests": "phpunit --testsuite unit --testdox --coverage-clover=build/logs/clover-unit-tests.xml"
34 | },
35 | "extra": {
36 | "laravel": {
37 | "providers": [
38 | "JsonMapper\\LaravelPackage\\ServiceProvider"
39 | ]
40 | }
41 | },
42 | "require-dev": {
43 | "ext-json": "*",
44 | "squizlabs/php_codesniffer": "^3.5",
45 | "phpstan/phpstan": "^0.12.19 || ^1.0.0 || ^2.1",
46 | "phpunit/phpunit": "^8.0 || ^9.0 || ^10.5 || ^11.5.3",
47 | "orchestra/testbench": "^5.3|^6.0 || ^9.0 || ^10.0",
48 | "php-coveralls/php-coveralls": "^2.4",
49 | "guzzlehttp/guzzle": "^6.5 || ^7.0"
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/config/json-mapper.php:
--------------------------------------------------------------------------------
1 | 'default' // default, best-fit
5 | ];
--------------------------------------------------------------------------------
/phpstan.neon.dist:
--------------------------------------------------------------------------------
1 | parameters:
2 | level: max
3 | checkMissingIterableValueType: false
4 | checkGenericClassInNonGenericObjectType: false
5 | paths:
6 | - src
7 | - tests
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
13 | tests/Unit
14 |
15 |
16 | tests/Integration
17 |
18 |
19 |
20 |
21 |
22 | src
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/src/JsonMapper.php:
--------------------------------------------------------------------------------
1 | mapArray($json, $object));
14 | }
15 |
16 | public function mapToCollectionFromString(string $json, object $object): Collection
17 | {
18 | return collect($this->mapArrayFromString($json, $object));
19 | }
20 | }
--------------------------------------------------------------------------------
/src/JsonMapperInterface.php:
--------------------------------------------------------------------------------
1 | mergeConfigFrom(self::CONFIG_FILE, 'json-mapper');
21 |
22 | $this->app->singleton(JsonMapperInterface::class, function ($app) {
23 | $config = $app->get('config')->get('json-mapper.type');
24 | $builder = JsonMapperBuilder::new()
25 | ->withJsonMapperClassName(\JsonMapper\LaravelPackage\JsonMapper::class);
26 | $factory = new JsonMapperFactory($builder);
27 |
28 | switch ($config) {
29 | case 'best-fit':
30 | return $factory->bestFit();
31 | case 'default':
32 | default:
33 | return $factory->default();
34 | }
35 | });
36 |
37 | $this->app->alias(JsonMapperInterface::class, JsonMapper::class);
38 | $this->app->alias(JsonMapperInterface::class, \JsonMapper\LaravelPackage\JsonMapperInterface::class);
39 | $this->app->alias(JsonMapperInterface::class, \JsonMapper\LaravelPackage\JsonMapper::class);
40 | }
41 |
42 | /**
43 | * @return void
44 | */
45 | public function boot()
46 | {
47 | $this->publishes([self::CONFIG_FILE => \config_path('json-mapper.php')]);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/tests/Implementation/ChuckNorris/Joke.php:
--------------------------------------------------------------------------------
1 | number;
16 | }
17 |
18 | public function setNumber(int $number): void
19 | {
20 | $this->number = $number;
21 | }
22 |
23 | public static function withNumber(int $number): self
24 | {
25 | $instance = new self();
26 | $instance->setNumber($number);
27 |
28 | return $instance;
29 | }
30 | }
--------------------------------------------------------------------------------
/tests/Integration/JsonMapperTests.php:
--------------------------------------------------------------------------------
1 | app->make(JsonMapperInterface::class);
26 | $url = "https://api.chucknorris.io/jokes/search?query=save";
27 | $data = (string) file_get_contents($url);
28 | $json = json_decode($data, false);
29 | $jokes = $mapper->mapToCollection($json->result, new Joke());
30 |
31 | self::assertContainsOnlyInstancesOf(Joke::class, $jokes->all());
32 | }
33 |
34 | /** @covers \JsonMapper\LaravelPackage\JsonMapper */
35 | public function testJsonMapperCanMapToCollectionWithLargeJson(): void
36 | {
37 | /** @var JsonMapperInterface $mapper */
38 | $mapper = $this->app->make(JsonMapperInterface::class);
39 | $url = "https://api.chucknorris.io/jokes/search?query=kick";
40 | $data = (string) file_get_contents($url);
41 | $json = json_decode($data, false);
42 | $jokes = $mapper->mapToCollection($json->result, new Joke());
43 |
44 | self::assertContainsOnlyInstancesOf(Joke::class, $jokes->all());
45 | }
46 |
47 | }
--------------------------------------------------------------------------------
/tests/Unit/JsonMapperTest.php:
--------------------------------------------------------------------------------
1 | offsetSet('config', new Repository(['json-mapper.type' => 'best-fit']));
21 | $serviceProvider = new ServiceProvider($app);
22 |
23 | $serviceProvider->boot();
24 | $serviceProvider->register();
25 | /** @var JsonMapperInterface $jsonMapper */
26 | $jsonMapper = $app->make(JsonMapperInterface::class);
27 | $data = json_decode('[{"number": 12}, {"number": 13}]', false);
28 |
29 | $result = $jsonMapper->mapToCollection($data, new SimpleObject());
30 |
31 | self::assertCount(2, $result);
32 | self::assertEquals(collect([SimpleObject::withNumber(12), SimpleObject::withNumber(13)]), $result);
33 | }
34 |
35 | /** @covers \JsonMapper\LaravelPackage\JsonMapper */
36 | public function testCanMapToCollectionFromstring(): void
37 | {
38 | $app = new Application();
39 | $app->offsetSet('config', new Repository(['json-mapper.type' => 'best-fit']));
40 | $serviceProvider = new ServiceProvider($app);
41 |
42 | $serviceProvider->boot();
43 | $serviceProvider->register();
44 | /** @var JsonMapperInterface $jsonMapper */
45 | $jsonMapper = $app->make(JsonMapperInterface::class);
46 |
47 | $result = $jsonMapper->mapToCollectionFromString('[{"number": 12}, {"number": 13}]', new SimpleObject());
48 |
49 | self::assertCount(2, $result);
50 | self::assertEquals(collect([SimpleObject::withNumber(12), SimpleObject::withNumber(13)]), $result);
51 | }
52 | }
--------------------------------------------------------------------------------
/tests/Unit/ServiceProviderTest.php:
--------------------------------------------------------------------------------
1 | boot();
24 |
25 | self::assertArrayHasKey(ServiceProvider::class, $serviceProvider::$publishes);
26 | self::assertIsArray($serviceProvider::$publishes[ServiceProvider::class]);
27 | self::assertCount(1, $serviceProvider::$publishes[ServiceProvider::class]);
28 | }
29 |
30 | /**
31 | * @covers \JsonMapper\LaravelPackage\ServiceProvider
32 | * @dataProvider configOptionsDataProvider
33 | * @param string|null $type
34 | */
35 | public function testRegisterMakesJsonMapperAvailableInApp($type): void
36 | {
37 | $app = new \Illuminate\Foundation\Application();
38 | $app->offsetSet('config', new Repository(['json-mapper.type' => $type]));
39 | $serviceProvider = new ServiceProvider($app);
40 |
41 | $serviceProvider->register();
42 |
43 | self::assertTrue($app->has(JsonMapperInterface::class));
44 | self::assertInstanceOf(JsonMapperInterface::class, $app->make(JsonMapperInterface::class));
45 |
46 | self::assertTrue($app->has(JsonMapper::class));
47 | self::assertInstanceOf(JsonMapper::class, $app->make(JsonMapper::class));
48 | }
49 |
50 | public function configOptionsDataProvider(): array
51 | {
52 | return [
53 | 'default' => ['default'],
54 | 'best-fit' => ['best-fit'],
55 | 'unspecified' => [null],
56 | ];
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/tests/database/migrations/2020_08_25_000000_create_eloquent_models_table.php:
--------------------------------------------------------------------------------
1 | id();
18 | $table->string('name');
19 | $table->string('email')->unique();
20 | $table->timestamp('email_verified_at')->nullable();
21 | $table->string('password');
22 | $table->timestamps();
23 | });
24 | }
25 |
26 | /**
27 | * Reverse the migrations.
28 | *
29 | * @return void
30 | */
31 | public function down()
32 | {
33 | Schema::dropIfExists('eloquent_models');
34 | }
35 | }
36 |
--------------------------------------------------------------------------------