Some small updates to various classes to support new stuff.

This commit is contained in:
James Cole
2014-09-12 17:34:54 +02:00
parent a1ba340ead
commit 0203fee174
8 changed files with 225 additions and 177 deletions

View File

@@ -3,6 +3,8 @@
namespace Firefly\Helper\Toolkit;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
/**
* Class Toolkit
@@ -140,4 +142,41 @@ class Toolkit implements ToolkitInterface
return $end;
}
/**
* Takes any collection and tries to make a sensible select list compatible array of it.
*
* @param Collection $set
* @param null $titleField
*
* @return mixed
*/
public function makeSelectList(Collection $set, $titleField = null)
{
$selectList = [];
/** @var Model $entry */
foreach ($set as $entry) {
$id = intval($entry->id);
$title = null;
if (is_null($titleField)) {
// try 'title' field.
if (isset($entry->title)) {
$title = $entry->title;
}
// try 'name' field
if (is_null($title)) {
$title = $entry->name;
}
// try 'description' field
if (is_null($title)) {
$title = $entry->description;
}
} else {
$title = $entry->$titleField;
}
$selectList[$id] = $title;
}
return $selectList;
}
}

View File

@@ -3,6 +3,7 @@
namespace Firefly\Helper\Toolkit;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
/**
* Interface ToolkitInterface
@@ -17,4 +18,14 @@ interface ToolkitInterface
*/
public function getDateRange();
/**
* Takes any collection and tries to make a sensible select list compatible array of it.
*
* @param Collection $set
* @param null $titleField
*
* @return mixed
*/
public function makeSelectList(Collection $set, $titleField = null);
}