1<?php
2
3namespace Mpdf\Conversion;
4
5use Mpdf\Utils\UtfString;
6
7class DecToHebrew
8{
9
10	public function convert($in, $reverse = false)
11	{
12		// reverse is used when called from Lists, as these do not pass through bidi-algorithm
13		$i = (int) $in; // I initially be the counter value
14		$s = ''; // S initially be the empty string
15
16		// and glyph list initially be the list of additive tuples.
17		$additive_nums = [400, 300, 200, 100, 90, 80, 70, 60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
18		$additive_glyphs = [0x05EA, 0x05E9, 0x05E8, 0x05E7, 0x05E6, 0x05E4, 0x05E2, 0x05E1, 0x05E0, 0x05DE, 0x05DC, 0x05DB,
19			[0x05D9, 0x05D8], [0x05D9, 0x05D7], [0x05D9, 0x05D6], [0x05D8, 0x05D6], [0x05D8, 0x05D5], 0x05D9,
20			0x05D8, 0x05D7, 0x05D6, 0x05D5, 0x05D4, 0x05D3, 0x05D2, 0x05D1, 0x05D0];
21
22		// NB This system manually specifies the values for 19-15 to force the correct display of 15 and 16, which are commonly
23		// rewritten to avoid a close resemblance to the Tetragrammaton.
24		// This function only works up to 1,000
25		if ($i > 999) {
26			return $in;
27		}
28
29		// return as initial numeric string
30		// If I is initially 0, and there is an additive tuple with a weight of 0, append that tuple's counter glyph to S and return S.
31		if ($i === 0) {
32			return '0';
33		}
34
35		// Otherwise, while I is greater than 0 and there are elements left in the glyph list:
36		$additiveNumsCount = count($additive_nums);
37		for ($t = 0; $t < $additiveNumsCount; $t++) {
38			// Pop the first additive tuple from the glyph list. This is the current tuple.
39			$ct = $additive_nums[$t];
40			// Append the current tuple's counter glyph to S x floor( I / current tuple's weight ) times (this may be 0).
41			$n = floor($i / $ct);
42			for ($j = 0; $j < $n; $j++) {
43				if (is_array($additive_glyphs[$t])) {
44					foreach ($additive_glyphs[$t] as $ag) {
45						if ($reverse) {
46							$s = UtfString::code2utf($ag) . $s;
47						} else {
48							$s .= UtfString::code2utf($ag);
49						}
50					}
51				} else {
52					if ($reverse) {
53						$s = UtfString::code2utf($additive_glyphs[$t]) . $s;
54					} else {
55						$s .= UtfString::code2utf($additive_glyphs[$t]);
56					}
57				}
58				$i -= ($ct * $n);
59			}
60			if ($i === .0 || $i === 0) {
61				return $s;
62			}
63		}
64
65		return $in; // return as initial string
66	}
67
68}
69