Files
firefly-iii/app/Support/Binder/AccountList.php

83 lines
2.3 KiB
PHP
Raw Normal View History

2016-01-09 15:39:34 +01:00
<?php
/**
* AccountList.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-01-09 15:39:34 +01:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-01-09 15:39:34 +01:00
*/
declare(strict_types=1);
2016-01-09 15:39:34 +01:00
namespace FireflyIII\Support\Binder;
use FireflyIII\Models\Account;
2016-02-05 09:25:15 +01:00
use Illuminate\Support\Collection;
2016-01-09 15:39:34 +01:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
2017-11-15 12:25:49 +01:00
* Class AccountList.
2016-01-09 15:39:34 +01:00
*/
class AccountList implements BinderInterface
{
/**
* @param $value
* @param $route
*
2016-02-05 09:25:15 +01:00
* @return Collection
2016-01-09 15:39:34 +01:00
*/
2016-02-06 10:11:06 +01:00
public static function routeBinder($value, $route): Collection
2016-01-09 15:39:34 +01:00
{
2016-09-16 12:07:45 +02:00
if (auth()->check()) {
2016-01-09 15:39:34 +01:00
$ids = explode(',', $value);
2016-03-02 11:48:53 +01:00
// filter ids:
$ids = self::filterIds($ids);
2016-01-09 15:39:34 +01:00
/** @var \Illuminate\Support\Collection $object */
$object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->whereIn('accounts.id', $ids)
2016-09-16 12:15:58 +02:00
->where('user_id', auth()->user()->id)
2016-01-09 15:39:34 +01:00
->get(['accounts.*']);
if ($object->count() > 0) {
2016-12-14 18:44:56 +01:00
$object = $object->sortBy(
function (Account $account) {
return $account->name;
}
);
2016-01-09 15:39:34 +01:00
return $object;
}
}
throw new NotFoundHttpException;
}
2016-03-02 11:48:53 +01:00
/**
* @param array $ids
*
* @return array
*/
protected static function filterIds(array $ids): array
{
$new = [];
foreach ($ids as $id) {
if (intval($id) > 0) {
$new[] = $id;
}
}
$new = array_unique($new);
return $new;
}
}