Unique.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  5. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  6. class Unique
  7. {
  8. /**
  9. * UNIQUE
  10. * The UNIQUE function searches for value either from a one-row or one-column range or from an array.
  11. *
  12. * @param mixed $lookupVector The range of cells being searched
  13. * @param mixed $byColumn Whether the uniqueness should be determined by row (the default) or by column
  14. * @param mixed $exactlyOnce Whether the function should return only entries that occur just once in the list
  15. *
  16. * @return mixed The unique values from the search range
  17. */
  18. public static function unique($lookupVector, $byColumn = false, $exactlyOnce = false)
  19. {
  20. if (!is_array($lookupVector)) {
  21. // Scalars are always returned "as is"
  22. return $lookupVector;
  23. }
  24. $byColumn = (bool) $byColumn;
  25. $exactlyOnce = (bool) $exactlyOnce;
  26. return ($byColumn === true)
  27. ? self::uniqueByColumn($lookupVector, $exactlyOnce)
  28. : self::uniqueByRow($lookupVector, $exactlyOnce);
  29. }
  30. /**
  31. * @return mixed
  32. */
  33. private static function uniqueByRow(array $lookupVector, bool $exactlyOnce)
  34. {
  35. // When not $byColumn, we count whole rows or values, not individual values
  36. // so implode each row into a single string value
  37. array_walk(
  38. $lookupVector,
  39. function (array &$value): void {
  40. $value = implode(chr(0x00), $value);
  41. }
  42. );
  43. $result = self::countValuesCaseInsensitive($lookupVector);
  44. if ($exactlyOnce === true) {
  45. $result = self::exactlyOnceFilter($result);
  46. }
  47. if (count($result) === 0) {
  48. return ExcelError::CALC();
  49. }
  50. $result = array_keys($result);
  51. // restore rows from their strings
  52. array_walk(
  53. $result,
  54. function (string &$value): void {
  55. $value = explode(chr(0x00), $value);
  56. }
  57. );
  58. return (count($result) === 1) ? array_pop($result) : $result;
  59. }
  60. /**
  61. * @return mixed
  62. */
  63. private static function uniqueByColumn(array $lookupVector, bool $exactlyOnce)
  64. {
  65. $flattenedLookupVector = Functions::flattenArray($lookupVector);
  66. if (count($lookupVector, COUNT_RECURSIVE) > count($flattenedLookupVector, COUNT_RECURSIVE) + 1) {
  67. // We're looking at a full column check (multiple rows)
  68. $transpose = Matrix::transpose($lookupVector);
  69. $result = self::uniqueByRow($transpose, $exactlyOnce);
  70. return (is_array($result)) ? Matrix::transpose($result) : $result;
  71. }
  72. $result = self::countValuesCaseInsensitive($flattenedLookupVector);
  73. if ($exactlyOnce === true) {
  74. $result = self::exactlyOnceFilter($result);
  75. }
  76. if (count($result) === 0) {
  77. return ExcelError::CALC();
  78. }
  79. $result = array_keys($result);
  80. return $result;
  81. }
  82. private static function countValuesCaseInsensitive(array $caseSensitiveLookupValues): array
  83. {
  84. $caseInsensitiveCounts = array_count_values(
  85. array_map(
  86. function (string $value) {
  87. return StringHelper::strToUpper($value);
  88. },
  89. $caseSensitiveLookupValues
  90. )
  91. );
  92. $caseSensitiveCounts = [];
  93. foreach ($caseInsensitiveCounts as $caseInsensitiveKey => $count) {
  94. if (is_numeric($caseInsensitiveKey)) {
  95. $caseSensitiveCounts[$caseInsensitiveKey] = $count;
  96. } else {
  97. foreach ($caseSensitiveLookupValues as $caseSensitiveValue) {
  98. if ($caseInsensitiveKey === StringHelper::strToUpper($caseSensitiveValue)) {
  99. $caseSensitiveCounts[$caseSensitiveValue] = $count;
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. return $caseSensitiveCounts;
  106. }
  107. private static function exactlyOnceFilter(array $values): array
  108. {
  109. return array_filter(
  110. $values,
  111. function ($value) {
  112. return $value === 1;
  113. }
  114. );
  115. }
  116. }