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

74 lines
1.7 KiB
PHP
Raw Normal View History

2016-01-09 08:39:34 -06:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2016-01-09 08:39:34 -06:00
/**
* AccountList.php
2016-04-01 09:44:46 -05:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-01-09 08:39:34 -06:00
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Support\Binder;
use Auth;
use FireflyIII\Models\Account;
2016-02-05 02:25:15 -06:00
use Illuminate\Support\Collection;
2016-01-09 08:39:34 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class AccountList
*
* @package FireflyIII\Support\Binder
*/
class AccountList implements BinderInterface
{
/**
* @param $value
* @param $route
*
2016-02-05 02:25:15 -06:00
* @return Collection
2016-01-09 08:39:34 -06:00
*/
2016-02-06 03:11:06 -06:00
public static function routeBinder($value, $route): Collection
2016-01-09 08:39:34 -06:00
{
if (Auth::check()) {
$ids = explode(',', $value);
2016-03-02 04:48:53 -06:00
// filter ids:
$ids = self::filterIds($ids);
2016-01-09 08:39:34 -06:00
/** @var \Illuminate\Support\Collection $object */
$object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('account_types.editable', 1)
->whereIn('accounts.id', $ids)
->where('user_id', Auth::user()->id)
->get(['accounts.*']);
if ($object->count() > 0) {
return $object;
}
}
throw new NotFoundHttpException;
}
2016-03-02 04:48:53 -06: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;
}
}