mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
38 lines
941 B
PHP
38 lines
941 B
PHP
<?php
|
|
|
|
namespace App\Macros;
|
|
|
|
use App\JikanApiModel;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* @mixin Collection
|
|
* @method offsetGetFirst(string $offset, mixed $default = null)
|
|
*/
|
|
final class CollectionOffsetGetFirst
|
|
{
|
|
public function __invoke(): \Closure
|
|
{
|
|
return function (string $offset, mixed $default = null) {
|
|
/**
|
|
* @var $this Collection
|
|
*/
|
|
$firstItem = $this->first();
|
|
if (is_array($firstItem)) {
|
|
$result = collect($firstItem)->get($offset, $default);
|
|
}
|
|
else if ($firstItem instanceof JikanApiModel) {
|
|
$result = $firstItem[$offset];
|
|
}
|
|
else if ($firstItem instanceof Collection) {
|
|
$result = $firstItem->get($offset, $default);
|
|
}
|
|
else {
|
|
$result = $default;
|
|
}
|
|
|
|
return $result;
|
|
};
|
|
}
|
|
}
|