<?php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Traits\IdTrait;
use App\Util\ScoreUtil;
/**
*
* @ORM\Entity(repositoryClass="App\Repository\ScoreRepository")
*
* @author wendell.zheng <wxzheng@ustc.edu.cn>
*/
class Score
{
use IdTrait;
/**
*
* @ORM\Column(length=1, nullable=true)
* @Assert\Choice(callback={"App\Util\ScoreUtil", "getRanks"})
*/
protected $rank;
/**
*
* @ORM\Column(type="integer", nullable=true)
*/
protected $score;
/**
*
* @ORM\Column(type="text", nullable=true)
* @Assert\NotBlank()
*/
protected $suggestion;
/**
*
* @ORM\Column(type="boolean")
*/
protected $done = false;
/**
*
* @ORM\ManyToOne(targetEntity="App\Entity\Reviewer")
*/
protected $reviewer;
/**
*
* @ORM\ManyToOne(targetEntity="App\Entity\Review", inversedBy="scores")
*/
protected $review;
public function getRank(): ?string
{
return $this->rank;
}
public function setRank(?string $rank)
{
$this->rank = $rank;
}
public function getScore(): ?float
{
return $this->score;
}
public function setScore(?float $score)
{
$this->score = $score;
}
public function getSuggestion(): ?string
{
return $this->suggestion;
}
public function setSuggestion(?string $suggestion)
{
$this->suggestion = $suggestion;
}
public function getDone(): bool
{
return $this->done;
}
public function setDone(bool $done)
{
$this->done = $done;
}
public function getReview(): ?Review
{
return $this->review;
}
public function setReview(?Review $review)
{
$this->review = $review;
}
public function getReviewer(): ?Reviewer
{
return $this->reviewer;
}
public function setReviewer(?Reviewer $reviewer)
{
$this->reviewer = $reviewer;
}
public function formatDone(): string
{
return $this->done ? '已评审' : '未评审';
}
public function getFormatRank(): string
{
return $this->rank ? $this->rank . '(' . ScoreUtil::RANK_OPTIONS[$this->rank] . ')' : '';
}
/**
*
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
try {
$options = ScoreUtil::RANK_SCORES[$this->rank];
if (array_key_exists('maxe', $options)) {
if ($this->score > $options['maxe']) {
throw new \OutOfRangeException();
}
}
if (array_key_exists('maxne', $options)) {
if ($this->score >= $options['maxne']) {
throw new \OutOfRangeException();
}
}
if ($this->score < $options['min']) {
throw new \OutOfRangeException();
}
} catch (\OutOfRangeException $e) {
$context->buildViolation('分数和等级不符合')
->atPath('score')
->addViolation();
}
}
}