Exemplo n.º 1
0
 /**
  * Headwords generator.
  * @param string|\rhoone\extension\Extension|\rhoone\models\Extension|mixed $class
  * `string` if extension class.
  * @param string[] $words
  * @throws InvalidParamException
  */
 public function getHeadwords($class = null, $words = [])
 {
     // Method One:
     if ($class === null || $class === false || is_string($class) && empty($class)) {
         $query = Headword::find();
     } else {
         $extensions = ExtensionManager::getModels($class);
         $guids = [];
         foreach ($extensions as $extension) {
             $guids[] = $extension->guid;
         }
         $query = Headword::find()->guid($guids);
     }
     if (is_array($words) && !empty($words)) {
         $query = $query->word($words);
     }
     foreach ($query->all() as $headword) {
         (yield $headword);
     }
     // Method Two:
     //return Headword::find()->all();
 }
Exemplo n.º 2
0
 /**
  * Validate dictionary.
  * Dictionary array example:
  * ```
  * [
  *     [
  *         0 => 'word1',    // Headword, required, it's key must be 0.
  *         'word2',         // The others are synonyms to headword.
  *         ...
  *     ],
  *     ...
  * ]
  * ```
  * @param array|\rhoone\extension\Dictionary|string|\rhoone\extension\Extension $dictionary
  * `array`: dictionary array.
  * `\rhoone\extension\Dictionary`: dictionary model.
  * `string`: extension class string.
  * `\rhoone\extension\Extension`: extension model.
  * @return array|false Dictionary array if validated, or false if invalid.
  * @throws InvalidParamException if error occured.
  */
 public static function validate($dictionary)
 {
     if (is_array($dictionary)) {
         return static::validateArray($dictionary);
     }
     if ($dictionary instanceof \rhoone\extension\Dictionary) {
         return static::validateArray($dictionary->getDictionary());
     }
     if (is_string($dictionary)) {
         $class = ExtensionManager::normalizeClass($dictionary);
         $dictionary = new $class();
     }
     if ($dictionary instanceof \rhoone\extension\Extension) {
         return static::validateWithExtension($dictionary);
     }
     return false;
 }