1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\Catalogue; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
15
|
|
|
use Symfony\Component\Translation\MetadataAwareInterface; |
16
|
|
|
use Translation\Bundle\Model\Metadata; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Calculate the number of messages in a catalogue. |
20
|
|
|
* |
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class CatalogueCounter |
24
|
|
|
{ |
25
|
|
|
public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue): int |
26
|
|
|
{ |
27
|
|
|
$total = 0; |
28
|
|
|
foreach ($catalogue->getDomains() as $domain) { |
29
|
|
|
$total += \count($catalogue->all($domain)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $total; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getCatalogueStatistics(MessageCatalogueInterface $catalogue): array |
36
|
|
|
{ |
37
|
|
|
$result = []; |
38
|
|
|
$domains = $catalogue->getDomains(); |
39
|
|
|
foreach ($domains as $domain) { |
40
|
|
|
$result[$domain]['defined'] = \count($catalogue->all($domain)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Sum the number of defined messages. |
44
|
|
|
$result['_total']['defined'] = 0; |
45
|
|
|
foreach ($domains as $domain) { |
46
|
|
|
$result['_total']['defined'] += $result[$domain]['defined']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!$catalogue instanceof MetadataAwareInterface) { |
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// For each domain check if the message is new or undefined. |
54
|
|
|
foreach ($domains as $domain) { |
55
|
|
|
$result[$domain]['new'] = 0; |
56
|
|
|
$result[$domain]['obsolete'] = 0; |
57
|
|
|
$result[$domain]['approved'] = 0; |
58
|
|
|
|
59
|
|
|
foreach ($catalogue->all($domain) as $key => $text) { |
60
|
|
|
$metadata = new Metadata($catalogue->getMetadata($key, $domain)); |
61
|
|
|
$state = $metadata->getState(); |
62
|
|
|
if ('new' === $state) { |
63
|
|
|
++$result[$domain]['new']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ('obsolete' === $state) { |
67
|
|
|
++$result[$domain]['obsolete']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($metadata->isApproved()) { |
71
|
|
|
++$result[$domain]['approved']; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Sum the number of new and obsolete messages. |
77
|
|
|
$result['_total']['new'] = 0; |
78
|
|
|
$result['_total']['obsolete'] = 0; |
79
|
|
|
$result['_total']['approved'] = 0; |
80
|
|
|
foreach ($domains as $domain) { |
81
|
|
|
$result['_total']['new'] += $result[$domain]['new']; |
82
|
|
|
$result['_total']['obsolete'] += $result[$domain]['obsolete']; |
83
|
|
|
$result['_total']['approved'] += $result[$domain]['approved']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $result; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|