firefly-iii/app/Factory/BillFactory.php

132 lines
3.5 KiB
PHP
Raw Normal View History

2018-02-19 12:44:46 -06:00
<?php
2018-05-11 03:08:34 -05:00
2018-02-19 12:44:46 -06:00
/**
* BillFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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/>.
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2018-02-19 12:44:46 -06:00
namespace FireflyIII\Factory;
use FireflyIII\Models\Bill;
use FireflyIII\Services\Internal\Support\BillServiceTrait;
2018-02-19 12:44:46 -06:00
use FireflyIII\User;
2018-03-01 13:54:50 -06:00
use Illuminate\Support\Collection;
use Log;
2018-02-19 12:44:46 -06:00
/**
* Class BillFactory
*/
class BillFactory
{
use BillServiceTrait;
2018-02-19 12:44:46 -06:00
/** @var User */
private $user;
/**
2018-03-01 13:54:50 -06:00
* @param array $data
*
* @return Bill|null
2018-02-19 12:44:46 -06:00
*/
2018-03-01 13:54:50 -06:00
public function create(array $data): ?Bill
2018-02-19 12:44:46 -06:00
{
2018-03-01 13:54:50 -06:00
/** @var Bill $bill */
$bill = Bill::create(
[
2018-04-08 09:27:52 -05:00
'name' => $data['name'],
'match' => 'MIGRATED_TO_RULES',
'amount_min' => $data['amount_min'],
'user_id' => $this->user->id,
'transaction_currency_id' => $data['transaction_currency_id'],
'amount_max' => $data['amount_max'],
'date' => $data['date'],
'repeat_freq' => $data['repeat_freq'],
'skip' => $data['skip'],
2018-06-24 06:20:29 -05:00
'automatch' => $data['automatch'] ?? true,
'active' => $data['active'] ?? true,
2018-03-01 13:54:50 -06:00
]
);
// update note:
if (isset($data['notes'])) {
$this->updateNote($bill, $data['notes']);
}
return $bill;
2018-02-19 12:44:46 -06:00
}
/**
* @param int|null $billId
* @param null|string $billName
*
* @return Bill|null
*/
public function find(?int $billId, ?string $billName): ?Bill
{
2018-04-02 07:42:07 -05:00
$billId = (int)$billId;
$billName = (string)$billName;
$bill = null;
2018-02-19 12:44:46 -06:00
// first find by ID:
if ($billId > 0) {
/** @var Bill $bill */
2018-03-01 13:54:50 -06:00
$bill = $this->user->bills()->find($billId);
2018-02-19 12:44:46 -06:00
}
// then find by name:
if (null === $bill && \strlen($billName) > 0) {
2018-03-01 13:54:50 -06:00
$bill = $this->findByName($billName);
2018-02-19 12:44:46 -06:00
}
return $bill;
2018-02-19 12:44:46 -06:00
}
/**
2018-03-01 13:54:50 -06:00
* @param string $name
*
* @return Bill|null
2018-02-19 12:44:46 -06:00
*/
2018-03-01 13:54:50 -06:00
public function findByName(string $name): ?Bill
2018-02-19 12:44:46 -06:00
{
2018-03-01 13:54:50 -06:00
/** @var Collection $collection */
$collection = $this->user->bills()->get();
$return = null;
2018-03-01 13:54:50 -06:00
/** @var Bill $bill */
foreach ($collection as $bill) {
Log::debug(sprintf('"%s" vs. "%s"', $bill->name, $name));
if ($bill->name === $name) {
$return = $bill;
break;
2018-03-01 13:54:50 -06:00
}
}
Log::debug(sprintf('Bill::find("%s") by name returns null? %s', $name, var_export($return, true)));
2018-02-19 12:44:46 -06:00
return $return;
2018-02-19 12:44:46 -06:00
}
/**
2018-03-01 13:54:50 -06:00
* @param User $user
*/
public function setUser(User $user): void
{
2018-03-01 13:54:50 -06:00
$this->user = $user;
}
2018-03-05 12:35:58 -06:00
}