Ejemplo n.º 1
0
 /**
  * Synonyms generator.
  * @param string|\rhoone\extension\Extension|\rhoone\models\Extension|mixed $class
  * `string` if extension class.
  * @param string[] $words
  * @throws InvalidParamException
  */
 public function getSynonyms($class = null, $words = [])
 {
     // Method One:
     $query = Synonym::find();
     if (is_array($words) && !empty($words)) {
         $query = $query->word($words);
     }
     if (!($class === null || $class === false || is_string($class) && empty($class))) {
         $query = $query->extension($class);
     }
     foreach ($query->all() as $synonyms) {
         (yield $synonyms);
     }
     // Method Two:
     //return Synonym::find()->all();
 }
Ejemplo n.º 2
0
 /**
  * Remove synonyms.
  * @param string|string[]|Synonym|Synonym[] $synonyms
  * @return boolean
  */
 public function removeSynonyms($synonyms)
 {
     if (is_string($synonyms)) {
         if ($synonyms === $this->word) {
             throw new InvalidParamException("`{$synonyms}` is same as headword, it cannot be deleted.");
         }
         $model = Synonym::find()->where(['word' => $synonyms, 'headword_guid' => $this->guid])->one();
         if (!$model) {
             Yii::warning($synonyms . ' does not exist.', __METHOD__);
             return false;
         }
         return $model->delete() == 1;
     }
     if ($synonyms instanceof Synonym && $synonyms->headword == $this) {
         if ($synonyms->word === $this->word) {
             throw new InvalidParamException("Synonyms `" . $synonyms->word . "` is same as headword, it cannot be deleted.");
         }
         return $synonyms->delete() == 1;
     }
     return false;
 }