<?php

namespace App\Admin\Repositories;

use App\Models\BaseIso3166 as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class BaseIso3166 extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    public static function selectOptions()
    {
        $result = [];
        $rows = Model::orderBy('id', 'asc')->get();
        foreach ($rows as $row) {
            $result[$row->country_name_en] = $row->country_name_en . ' - '. $row->country_name_zh;
        }
        return $result;
    }

    /*
     * 通过英文国家名得到国家简称
     */
    public static function getAlpha2($countryNameEn)
    {
        $row = Model::where('country_name_en', $countryNameEn)->first();
        if (empty($row)) {
            return '';
        }
        return $row->alpha_2;
    }
}
