<?php
/**
 * AnyWareX Clone Translate – Language Class
 */

if (!defined('ABSPATH')) {
    exit;
}

class AWXCT_Clone_Translate_Language
{

    public $id;
    public $code;
    public $name; // Display label (not necessarily the DB column "name")

    public static function from_db_row($row)
    {
        if (!$row)
            return null;
        $o = new self();
        $o->id = isset($row->id) ? (int) $row->id : 0;
        $o->code = isset($row->code) ? strtolower($row->code) : '';

        if (isset($row->native_name) && $row->native_name !== '') {
            $o->name = $row->native_name;
        } elseif (isset($row->english_name) && $row->english_name !== '') {
            $o->name = $row->english_name;
        } elseif (isset($row->name) && $row->name !== '') {
            $o->name = $row->name;
        } else {
            $o->name = strtoupper($o->code);
        }
        return $o;
    }

    public function to_array()
    {
        return ['id' => $this->id, 'code' => $this->code, 'name' => $this->name];
    }
}
