src/Entity/Score.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Symfony\Component\Validator\Constraints as Assert;
  4. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use App\Entity\Traits\IdTrait;
  7. use App\Util\ScoreUtil;
  8. /**
  9.  *
  10.  * @ORM\Entity(repositoryClass="App\Repository\ScoreRepository")
  11.  *
  12.  * @author wendell.zheng <wxzheng@ustc.edu.cn>
  13.  */
  14. class Score
  15. {
  16.     use IdTrait;
  17.     /**
  18.      *
  19.      * @ORM\Column(length=1, nullable=true)
  20.      * @Assert\Choice(callback={"App\Util\ScoreUtil", "getRanks"})
  21.      */
  22.     protected $rank;
  23.     /**
  24.      *
  25.      * @ORM\Column(type="integer", nullable=true)
  26.      */
  27.     protected $score;
  28.     /**
  29.      *
  30.      * @ORM\Column(type="text", nullable=true)
  31.      * @Assert\NotBlank()
  32.      */
  33.     protected $suggestion;
  34.     /**
  35.      *
  36.      * @ORM\Column(type="boolean")
  37.      */
  38.     protected $done false;
  39.     /**
  40.      *
  41.      * @ORM\ManyToOne(targetEntity="App\Entity\Reviewer")
  42.      */
  43.     protected $reviewer;
  44.     /**
  45.      *
  46.      * @ORM\ManyToOne(targetEntity="App\Entity\Review", inversedBy="scores")
  47.      */
  48.     protected $review;
  49.     public function getRank(): ?string
  50.     {
  51.         return $this->rank;
  52.     }
  53.     public function setRank(?string $rank)
  54.     {
  55.         $this->rank $rank;
  56.     }
  57.     public function getScore(): ?float
  58.     {
  59.         return $this->score;
  60.     }
  61.     public function setScore(?float $score)
  62.     {
  63.         $this->score $score;
  64.     }
  65.     public function getSuggestion(): ?string
  66.     {
  67.         return $this->suggestion;
  68.     }
  69.     public function setSuggestion(?string $suggestion)
  70.     {
  71.         $this->suggestion $suggestion;
  72.     }
  73.     public function getDone(): bool
  74.     {
  75.         return $this->done;
  76.     }
  77.     public function setDone(bool $done)
  78.     {
  79.         $this->done $done;
  80.     }
  81.     public function getReview(): ?Review
  82.     {
  83.         return $this->review;
  84.     }
  85.     public function setReview(?Review $review)
  86.     {
  87.         $this->review $review;
  88.     }
  89.     public function getReviewer(): ?Reviewer
  90.     {
  91.         return $this->reviewer;
  92.     }
  93.     public function setReviewer(?Reviewer $reviewer)
  94.     {
  95.         $this->reviewer $reviewer;
  96.     }
  97.     public function formatDone(): string
  98.     {
  99.         return $this->done '已评审' '未评审';
  100.     }
  101.     public function getFormatRank(): string
  102.     {
  103.         return $this->rank $this->rank '(' ScoreUtil::RANK_OPTIONS[$this->rank] . ')' '';
  104.     }
  105.     /**
  106.      *
  107.      * @Assert\Callback
  108.      */
  109.     public function validate(ExecutionContextInterface $context)
  110.     {
  111.         try {
  112.             $options ScoreUtil::RANK_SCORES[$this->rank];
  113.             if (array_key_exists('maxe'$options)) {
  114.                 if ($this->score $options['maxe']) {
  115.                     throw new \OutOfRangeException();
  116.                 }
  117.             }
  118.             if (array_key_exists('maxne'$options)) {
  119.                 if ($this->score >= $options['maxne']) {
  120.                     throw new \OutOfRangeException();
  121.                 }
  122.             }
  123.             if ($this->score $options['min']) {
  124.                 throw new \OutOfRangeException();
  125.             }
  126.         } catch (\OutOfRangeException $e) {
  127.             $context->buildViolation('分数和等级不符合')
  128.                 ->atPath('score')
  129.                 ->addViolation();
  130.         }
  131.     }
  132. }