Esempio n. 1
0
 /**
  * Main vote function.
  *
  * @param  VoterInterface    $voter
  * @param  VoteableInterface $votable
  * @param  int               $weight
  * @return Vote|null
  */
 public static function store(VoterInterface $voter, VoteableInterface $voteable, $weight)
 {
     $vote = Vote::where('voter_id', $voter->getKey())->where('voter_type', get_class($voter))->where('voteable_id', $voter->getKey())->where('voteable_type', get_class($voteable))->first();
     if (is_null($vote)) {
         // Previous vote does not exist
         if ($weight == 0) {
             // Trying to cancel vote that does not exist
             return;
         }
         $vote = new Vote();
         $vote->voteable()->associate($voteable);
         $vote->voter()->associate($voter);
     }
     if ($weight == 0) {
         // Cancelling previous vote
         $vote->delete();
         return;
     }
     $vote->weight = $weight;
     $vote->save();
     return $vote;
 }
Esempio n. 2
0
 /**
  * Vote on resource by voter.
  *
  * @param  VoterInterface $voter
  * @param  int            $weight
  * @return Vote|null
  */
 public function voteBy(VoterInterface $voter, $weight)
 {
     return Vote::store($voter, $this, $weight);
 }
Esempio n. 3
0
 /**
  * Vote on a voteable.
  *
  * @param  VoteableInterface $voteable
  * @param  int               $weight
  * @return Vote
  */
 public function vote(VoteableInterface $voteable, $weight)
 {
     return Vote::store($this, $voteable, $weight);
 }