Skip to content

Release v12.0.1 #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
matrix:
# quotes are needed it is treated as a number and zero at decimal part is gone
# at runtime i.e. 8.10 -> 8.1, while "8.10" => "8.10".
laravel: ["11.0"]
php: ["8.2"]
laravel: ["12.0"]
php: ["8.2", "8.3", "8.4"]

runs-on: ubuntu-latest

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ vendor/
*.swp
*~
*.bak

.roo
.roomodes
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016-2020 Marcin Orlowski <mail (#) marcinorlowski (.) com>
Copyright (c) 2016-2025 Marcin Orlowski <mail (#) marcinorlowski (.) com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ Development branch:

## License ##

* Written and copyrighted &copy;2016-2024 by Marcin Orlowski <mail (#) marcinorlowski (.) com>
* Written and copyrighted &copy;2016-2025 by Marcin Orlowski <mail (#) marcinorlowski (.) com>
* ResponseBuilder is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "marcin-orlowski/laravel-api-response-builder",
"description": "Helps building nice, normalized and easy to consume Laravel REST API.",
"homepage": "https://github.com/MarcinOrlowski/laravel-api-response-builder",
"version": "12.0.0",
"version": "12.0.1",
"keywords": [
"laravel",
"laravel10",
Expand Down
7 changes: 5 additions & 2 deletions docs/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ should be able to easily backport future new features to older versions rather e

## CHANGE LOG ##

* v12.0.1 (2025-04-16)
* [RB-255] Fixed `ToArrayConverter` using a new Request instance instead of the actual request.
* [BR-256] Fixed `ToArrayConverter` potentially causing fatal error if object lacks `toArray` method.
* [GH-261] Updated GitHub Actions workflow to test against Laravel 12, PHP 8.3, and PHP 8.4.

* v12.0.0 (2025-04-16)
* Added support for Laravel v12.

* v11.0.0 (2024-05-06)
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](compatibility.md)).
* Added support for Laravel v11.
* Corrected documentation (thanks to Ehsan Soleimanian)

* v10.0.0 (2023-02-20)
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](compatibility.md)).
* Added support for Laravel v10.

* v9.4.0 (2023-02-18)
Expand Down
10 changes: 8 additions & 2 deletions src/Converters/ToArrayConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @link https://github.com/MarcinOrlowski/laravel-api-response-builder
*/

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Resources\Json\JsonResource;
use MarcinOrlowski\ResponseBuilder\Contracts\ConverterContract;
use MarcinOrlowski\ResponseBuilder\Exceptions as Ex;
Expand All @@ -38,8 +37,15 @@ public function convert(object $obj, array $config): array
{
Validator::assertIsObject('obj', $obj);

if (!\method_exists($obj, 'toArray')) {
$cls = \get_class($obj);
throw new \InvalidArgumentException(
"Object of class '{$cls}' does not have a 'toArray' method."
);
}

/** @var JsonResource $obj */
$request = new \Illuminate\Http\Request();
$request = \request();
return (array)$obj->toArray($request);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/Converter/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,29 @@ public function testSubclassOfConfiguredClassConversion(): void
$this->assertEquals($child_val, $result[ TestModel::FIELD_NAME ]);
}

/**
* Checks if ToArrayConverter throws exception for object without toArray method.
*/
public function testToArrayConverterThrowsExceptionForObjectWithoutToArrayMethod(): void
{
// GIVEN an object without a toArray method
$obj = new \stdClass();
$obj->foo = Generator::getRandomString('foo');

// HAVING configuration mapping stdClass to ToArrayConverter
$key = Generator::getRandomString('key');
Config::set(RB::CONF_KEY_CONVERTER_CLASSES, [
\get_class($obj) => [
RB::KEY_HANDLER => ToArrayConverter::class,
RB::KEY_KEY => $key,
],
]);

// THEN we expect an exception
$this->expectException(\InvalidArgumentException::class);

// WHEN we try to convert the object
(new Converter())->convert($obj);
}

} // end of class