Skip to content

Instantly share code, notes, and snippets.

@jlem
Created August 20, 2015 06:40
Show Gist options
  • Save jlem/a67782db72fc2515e112 to your computer and use it in GitHub Desktop.
Save jlem/a67782db72fc2515e112 to your computer and use it in GitHub Desktop.
Model usage
<?php namespace GR\Clan;
use Illuminate\Database\Eloquent\Model;
use GR\Section\SectionInterface;
use GR\Member\MemberInterface;
use GR\Lib\ModelCache;
use GR\Member\Member;
class Clan extends Model implements ClanInterface
{
use ModelCache;
protected $table = "clans";
protected $fillable = [
'name',
];
//------------------------------------------------------------------
// Eloquent Relations (do not use directly)
//------------------------------------------------------------------
public function leader()
{
return $this->belongsTo(Member::class, 'leader_id');
}
public function clanMembers()
{
return $this->hasMany(ClanMember::class);
}
public function members()
{
return $this->hasMany(Member::class);
}
public function teams()
{
return $this->hasMany(ClanTeam::class);
}
//------------------------------------------------------------------
// Instance API Methods (only use these)
//------------------------------------------------------------------
/**
* {@inheritdoc}
*/
public function getID()
{
return $this->getKey();
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function createClanMember(MemberInterface $member)
{
return $this->clanMembers()->create([
'member_id' => $member->getID(),
]);
}
/**
* {@inheritdoc}
*/
public function addClanMember(ClanMemberInterface $clanMember)
{
$this->clanMembers()->associate($clanMember);
}
/**
* {@inheritdoc}
*/
public function getTeams()
{
return $this->teams;
}
/**
* {@inheritdoc}
*/
public function getTeam(SectionInterface $section)
{
return $this->cache(function(SectionInterface $section) {
return $this->teams()
->where('section_id', $section->getID())
->first();
});
}
/**
* {@inheritdoc}
*/
public function hasTeam(SectionInterface $section)
{
return !is_null($this->getTeam($section));
}
/**
* {@inheritdoc}
*/
public function createTeam(SectionInterface $section)
{
return $this->teams()->create([
'section_id' => $section->getID()
]);
}
/**
* {@inheritdoc}
*/
public function addTeam(ClanTeamInterface $team)
{
$this->teams()->save($team);
}
/**
* {@inheritdoc}
*/
public function getLeader()
{
return $this->leader;
}
/**
* {@inheritdoc}
*/
public function hasLeader()
{
return !is_null($this->leader_id);
}
/**
* {@inheritdoc}
*/
public function setLeader(ClanMemberInterface $section, $force = false)
{
if ($this->hasLeader() && $force === false) {
throw new \LogicException("This clan already has a leader");
}
// Update the clan with the leader id
$this->leader_id = $section->member->id;
$this->save();
// Update the clan member to be the leader
$section->is_leader = 1;
$section->save();
}
/**
* {@inheritdoc}
*/
public function getMembers()
{
return $this->members;
}
/**
* {@inheritdoc}
*/
public function getClanMembers()
{
return $this->clanMembers;
}
}
<?php
namespace GR\Clan;
use GR\Member\MemberInterface;
use GR\Section\SectionInterface;
use Illuminate\Database\Collection;
interface ClanInterface
{
/**
* Returns the primary key identifier for this Clan
* @return integer
*/
public function getID();
/**
* Returns the full name of the clan
* @return string
*/
public function getName();
/**
* Creates a new ClanMember for the clan
* @param MemberInterface $member
* @return ClanMember
*/
public function createClanMember(MemberInterface $member);
/**
* Attaches an existing clan member to the clan
* @param ClanMemberInterface $clanMember
* @return void
*/
public function addClanMember(ClanMemberInterface $clanMember);
/**
* Gets a collection of teams
* @return Collection
*/
public function getTeams();
/**
* Gets a single team from the clan, if it exists
* @param SectionInterface $section
* @return ClanTeamInterface|null
*/
public function getTeam(SectionInterface $section);
/**
* Checks to see if the clan has a team for the given section
* @param SectionInterface $section
* @return bool
*/
public function hasTeam(SectionInterface $section);
/**
* @param SectionInterface $section
* @return ClanTeam
*/
public function createTeam(SectionInterface $section);
/**
* @param ClanTeam|ClanTeamInterface $team
* @return
*/
public function addTeam(ClanTeamInterface $team);
/**
* Gets the leader of the clan
* @return MemberInterface
*/
public function getLeader();
/**
* Checks to see if the clan has a leader or not
* @return bool
*/
public function hasLeader();
/**
* Sets a given clan member as the leader if the leader is not already set.
* Use $force = true to override the leader
* @param ClanMember|ClanMemberInterface $section
* @param bool|false $force
* @return
*/
public function setLeader(ClanMemberInterface $section, $force = false);
/**
* Gets the Members associated with this Clan
* @return Collection
*/
public function getMembers();
/**
* Gets the ClanMembers associated with this Clan
* @return Collection
*/
public function getClanMembers();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment