2014-11-02 14:58:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace FireflyIII\Shared\Toolkit;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Form
|
|
|
|
|
*
|
|
|
|
|
* @package FireflyIII\Shared\Toolkit
|
|
|
|
|
*/
|
2014-11-12 22:37:09 +01:00
|
|
|
class Form
|
|
|
|
|
{
|
2014-11-02 14:58:12 +01:00
|
|
|
/**
|
|
|
|
|
* Takes any collection and tries to make a sensible select list compatible array of it.
|
|
|
|
|
*
|
|
|
|
|
* @param Collection $set
|
2014-11-12 22:37:09 +01:00
|
|
|
* @param null $titleField
|
2014-12-14 20:40:02 +01:00
|
|
|
* @param bool $addEmpty
|
|
|
|
|
* @SuppressWarnings("CyclomaticComplexity")
|
2014-11-02 14:58:12 +01:00
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2014-12-14 20:40:02 +01:00
|
|
|
public function makeSelectList(Collection $set, $titleField = null, $addEmpty = false)
|
2014-11-02 14:58:12 +01:00
|
|
|
{
|
|
|
|
|
$selectList = [];
|
2014-12-14 20:40:02 +01:00
|
|
|
if ($addEmpty) {
|
|
|
|
|
$selectList[0] = '(none)';
|
|
|
|
|
}
|
|
|
|
|
$fields = ['title', 'name', 'description'];
|
|
|
|
|
/** @var \Eloquent $entry */
|
2014-11-02 14:58:12 +01:00
|
|
|
foreach ($set as $entry) {
|
2014-12-14 20:40:02 +01:00
|
|
|
/** @noinspection PhpUndefinedFieldInspection */
|
2014-11-02 14:58:12 +01:00
|
|
|
$id = intval($entry->id);
|
2014-12-14 20:40:02 +01:00
|
|
|
$title = $titleField;
|
2014-11-02 14:58:12 +01:00
|
|
|
|
2014-12-14 20:40:02 +01:00
|
|
|
foreach ($fields as $field) {
|
|
|
|
|
if (is_null($title) && isset($entry->$field)) {
|
|
|
|
|
$title = $entry->$field;
|
2014-11-02 14:58:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$selectList[$id] = $title;
|
|
|
|
|
}
|
2014-11-12 22:37:09 +01:00
|
|
|
|
2014-12-14 20:40:02 +01:00
|
|
|
|
2014-11-02 14:58:12 +01:00
|
|
|
return $selectList;
|
|
|
|
|
}
|
2014-12-14 20:40:02 +01:00
|
|
|
}
|