/**
* Construct Closet object with owner's uid.
*
* @param int $uid
*/
public function __construct($uid)
{
$this->uid = $uid;
$this->db = DB::table('closets');
// create a new closet if not exists
if (!$this->db->where('uid', $uid)->get()) {
$this->db->insert(['uid' => $uid, 'textures' => '']);
}
// load items from json string
$this->textures = json_decode($this->db->where('uid', $uid)->get()[0]->textures, true);
$this->textures = is_array($this->textures) ? $this->textures : [];
$textures_invalid = [];
// traverse items in the closet
foreach ($this->textures as $texture) {
$result = Texture::find($texture['tid']);
if ($result) {
// set user custom texture name
$result->name = $texture['name'];
// push instances of App\Models\Texture to the bag
if ($result->type == "cape") {
$this->textures_cape[] = $result;
} else {
$this->textures_skin[] = $result;
}
} else {
$textures_invalid[] = $texture['tid'];
continue;
}
}
// remove invalid textures from closet
foreach ($textures_invalid as $tid) {
$this->remove($tid);
}
unset($textures_invalid);
}