nmsg()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 6
nop 4
dl 0
loc 16
ccs 0
cts 7
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PHP: Nelson Martell Library file
5
 *
6
 * Content:
7
 * - Global functions definition for NML.
8
 *
9
 * Copyright © 2016-2024 Nelson Martell (http://nelson6e65.github.io)
10
 *
11
 * Licensed under The MIT License (MIT)
12
 * For full copyright and license information, please see the LICENSE
13
 * Redistributions of files must retain the above copyright notice.
14
 *
15
 * @copyright 2016-2024 Nelson Martell
16
 * @link      http://nelson6e65.github.io/php_nml/
17
 * @since     0.6.0
18
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
19
 * */
20
21
declare(strict_types=1);
22
23
namespace NelsonMartell;
24
25
use InvalidArgumentException;
26
use NelsonMartell\Extensions\Text;
27
28
/**
29
 * Busca un mensaje único traducido en el dominio 'nml'.
30
 * El mensaje puede contener cadenas de formato.
31
 *
32
 * @param string      $message Mensaje con formato que se va a buscar.
33
 * @param array|mixed $args    Lista de objetos que se van a incluir en las cadenas de formato del mensaje.
34
 *
35
 * @return string
36
 * @since 0.6.0
37
 * @see \dgettext()
38
 * */
39
function msg(string $message, ...$args): string
40
{
41 71
    $translated = extension_loaded('gettext') ? \dgettext(NML_GETTEXT_DOMAIN, $message) : $message;
42
43 71
    $data = $args;
44
45 71
    if (count($args) === 1 && is_array($args[0])) {
46 63
        $data = $args[0];
47
    }
48
49 71
    return Text::format($translated, $data);
50
}
51
52
/**
53
 * Busca un mensaje único, en singular y plural, traducido en el dominio 'nml'.
54
 * El mensaje puede contener cadenas de formato.
55
 *
56
 * @param string      $singular Mensaje con formato que se va a buscar cuando $n
57
 *   es uno (1).
58
 * @param string      $plural   Mensaje con formato que se va a buscar cuando $n
59
 *   es distinto a (1).
60
 * @param int         $n        Cantidad
61
 * @param array|mixed $args     Lista de objetos que se van a incluir en las cadenas de formato del mensaje.
62
 *
63
 * @return string
64
 * @since 0.6.0
65
 * @see \dngettext()
66
 * @internal
67
 * */
68
function nmsg(string $singular, string $plural, int $n, ...$args): string
69
{
70
    if (extension_loaded('gettext')) {
71
        $translated = \dngettext(NML_GETTEXT_DOMAIN, $singular, $plural, $n);
72
    } else {
73
        // Simple implementation without Gettext
74
        $translated = $n === 1 ? $singular : $plural;
75
    }
76
77
    $data = $args;
78
79
    if (count($args) === 1 && is_array($args[0])) {
80
        $data = $args[0];
81
    }
82
83
    return Text::format($translated, $data);
84
}
85
86
/**
87
 * Obtiene el tipo del objeto especificado.
88
 * Es un alias para el constructor de la clase Type.
89
 *
90
 * @param string|mixed  $obj
91
 * @param bool          $searchName
92
 *
93
 * @return Type
94
 * @throws InvalidArgumentException
95
 *
96
 * @since 0.6.0
97
 *
98
 * @see Type::__construct()
99
 * */
100
function typeof($obj, bool $searchName = false): Type
101
{
102 280
    return new Type($obj, $searchName);
103
}
104