snipe-it/app/Http/Transformers/SelectlistTransformer.php
Laravel Shift 934afa036f Adopt Laravel coding style
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions.

You may customize the adopted coding style by adding your own [PHP CS Fixer][1] `.php_cs` config file to your project root. Feel free to use [Shift's Laravel ruleset][2] to help you get started.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200
2021-06-10 20:15:52 +00:00

47 lines
1.4 KiB
PHP

<?php
namespace App\Http\Transformers;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* Class SelectlistTransformer
*
* This handles the standardized formatting of the API response we need to provide for
* the rich (text and images) Select2 javascript.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0.16]
* @return \Illuminate\Http\Response
*/
class SelectlistTransformer
{
public function transformSelectlist(LengthAwarePaginator $select_items)
{
$items_array = [];
// Loop through the paginated collection to set the array values
foreach ($select_items as $select_item) {
$items_array[] = [
'id' => (int) $select_item->id,
'text' => ($select_item->use_text) ? $select_item->use_text : $select_item->name,
'image' => ($select_item->use_image) ? $select_item->use_image : null,
];
}
$results = [
'results' => $items_array,
'pagination' => [
'more' => ($select_items->currentPage() >= $select_items->lastPage()) ? false : true,
'per_page' => $select_items->perPage(),
],
'total_count' => $select_items->total(),
'page' => $select_items->currentPage(),
'page_count' => $select_items->lastPage(),
];
return $results;
}
}