I18nUsageValidator.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. require_once __DIR__ . '/I18nValidatorInterface.php';
  3. class I18nUsageValidator implements I18nValidatorInterface {
  4. private $code;
  5. private $reference;
  6. private $totalEntries = 0;
  7. private $failedEntries = 0;
  8. private $result = '';
  9. public function __construct($reference, $code) {
  10. $this->code = $code;
  11. $this->reference = $reference;
  12. }
  13. public function displayReport() {
  14. return sprintf('%5.1f%% of translation keys are unused.', $this->failedEntries / $this->totalEntries * 100) . PHP_EOL;
  15. }
  16. public function displayResult() {
  17. return $this->result;
  18. }
  19. public function validate($ignore) {
  20. foreach ($this->reference as $file => $data) {
  21. foreach ($data as $key => $value) {
  22. $this->totalEntries++;
  23. if (preg_match('/\._$/', $key) && in_array(preg_replace('/\._$/', '', $key), $this->code)) {
  24. continue;
  25. }
  26. if (is_array($ignore) && in_array($key, $ignore)) {
  27. continue;
  28. }
  29. if (!in_array($key, $this->code)) {
  30. $this->result .= sprintf('Unused key %s - %s', $key, $value) . PHP_EOL;
  31. $this->failedEntries++;
  32. continue;
  33. }
  34. }
  35. }
  36. return 0 === $this->failedEntries;
  37. }
  38. }