|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HexMakina\kadro\Models; |
|
4
|
|
|
|
|
5
|
|
|
use HexMakina\BlackBox\Database\SelectInterface; |
|
6
|
|
|
use HexMakina\TightORM\TightModel; |
|
7
|
|
|
|
|
8
|
|
|
class Lingvo extends TightModel |
|
9
|
|
|
{ |
|
10
|
|
|
public const TABLE_NAME = 'kartisto_ISO639'; |
|
11
|
|
|
|
|
12
|
|
|
public const ISO_3 = 'Part3'; |
|
13
|
|
|
public const ISO_2B = 'Part2B'; |
|
14
|
|
|
public const ISO_2T = 'Part2T'; |
|
15
|
|
|
public const ISO_1 = 'Part1'; |
|
16
|
|
|
public const ISO_NAME = 'Ref_Name'; |
|
17
|
|
|
|
|
18
|
|
|
public const ISO_SET = 'Set'; |
|
19
|
|
|
public const ISO_SETS = [self::ISO_1 => 'ISO 639-1', self::ISO_3 => 'ISO 639-3', self::ISO_2B => 'ISO 639-2B', self::ISO_2T => 'ISO 639-2T']; |
|
20
|
|
|
public const ISO_DEFAULT = self::ISO_3; |
|
21
|
|
|
|
|
22
|
|
|
public const ISO_SCOPE = 'Scope'; |
|
23
|
|
|
public const ISO_SCOPES = ['I' => 'Individual', 'M' => 'Macrolanguage', 'S' => 'Special']; |
|
24
|
|
|
|
|
25
|
|
|
public const ISO_TYPE = 'Type'; |
|
26
|
|
|
public const ISO_TYPES = ['A' => 'Ancient', 'C' => 'Constructed', 'E' => 'Extinct', 'H' => 'Historical', 'L' => 'Living', 'S' => 'Special']; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
public function __toString() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->iso_name() . ' (' . $this->get(self::ISO_3) . ')'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function traceable(): bool |
|
35
|
|
|
{ |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function iso_name() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->get('Ref_Name'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function iso_scope() |
|
45
|
|
|
{ |
|
46
|
|
|
return self::ISO_SCOPES[$this->get('scope')]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function iso_type() |
|
50
|
|
|
{ |
|
51
|
|
|
return self::ISO_TYPES[$this->get('scope')]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
public static function query_retrieve($filter = [], $options = []): SelectInterface |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
$searchable_fields = [self::ISO_NAME, self::ISO_3, self::ISO_2B, self::ISO_2T, self::ISO_1]; |
|
59
|
|
|
|
|
60
|
|
|
$Query = static::table()->select(); |
|
61
|
|
|
|
|
62
|
|
|
if (isset($filter['name'])) { |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (isset($filter['code'])) { |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (isset($filter['term'])) { |
|
69
|
|
|
$Query->whereFilterContent(['term' => $filter['term'], 'fields' => $searchable_fields], $Query->tableLabel(), 'OR'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (isset($filter['requires_authority']) && isset(self::ISO_SETS[$filter['requires_authority']])) { |
|
73
|
|
|
$Query->whereNotEmpty($filter['requires_authority']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (isset($filter['types'])) { |
|
77
|
|
|
$wc = sprintf("AND " . self::ISO_TYPE . " IN ('%s') ", implode('\', \'', array_keys(self::ISO_TYPES))); |
|
78
|
|
|
$Query->where($wc); |
|
79
|
|
|
} |
|
80
|
|
|
if (isset($filter['scopes'])) { |
|
81
|
|
|
$wc = sprintf("AND " . self::ISO_SCOPE . " IN ('%s') ", implode('\', \'', array_keys(self::ISO_SCOPES))); |
|
82
|
|
|
$Query->where($wc); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$Query->orderBy([self::TABLE_NAME, self::ISO_1, 'DESC']); |
|
86
|
|
|
$Query->orderBy([self::TABLE_NAME, self::ISO_2T, 'DESC']); |
|
87
|
|
|
$Query->orderBy([self::TABLE_NAME, self::ISO_3, 'DESC']); |
|
88
|
|
|
$Query->orderBy([self::TABLE_NAME, self::ISO_NAME, 'DESC']); |
|
89
|
|
|
|
|
90
|
|
|
return $Query; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public static function search_language($term, $authority = null) |
|
94
|
|
|
{ |
|
95
|
|
|
$rows = self::query_retrieve(['term' => $term, 'requires_authority' => $authority])->retAss(); |
|
96
|
|
|
$ret = []; |
|
97
|
|
|
foreach ($rows as $row) { |
|
98
|
|
|
$ret[$row[self::ISO_3]] = $row[self::ISO_NAME]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $ret; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function language_name($code) |
|
105
|
|
|
{ |
|
106
|
|
|
$Query = self::table()->select([self::ISO_NAME]); |
|
107
|
|
|
$Query->whereEQ(self::ISO_3, $code); |
|
108
|
|
|
$rows = $Query->retCol(); |
|
109
|
|
|
|
|
110
|
|
|
if (isset($rows[0])) { // only 1 result |
|
111
|
|
|
return current($rows); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return null; // no results |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public static function ISO639_1_to_ISO639_3($code) |
|
118
|
|
|
{ |
|
119
|
|
|
$Query = self::table()->select([self::ISO_3])->whereEQ(self::ISO_1, $code, self::TABLE_NAME)->limit(1); |
|
120
|
|
|
$row = static::retrieve($Query); |
|
121
|
|
|
$row = get_object_vars($row[0]); |
|
122
|
|
|
return $row[self::ISO_3]; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|