PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/user/plugins/gantry5/src/classes/Gantry/Component/Assignments/AssignmentFilter.php

https://gitlab.com/akbaryu/project_magang_blog_grav
PHP | 160 lines | 70 code | 18 blank | 72 comment | 5 complexity | 04ae4827233f484f4407ac2de1694368 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Gantry5
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2016 RocketTheme, LLC
  6. * @license Dual License: MIT or GNU/GPLv2 and later
  7. *
  8. * http://opensource.org/licenses/MIT
  9. * http://www.gnu.org/licenses/gpl-2.0.html
  10. *
  11. * Gantry Framework code that extends GPL code is considered GNU/GPLv2 and later
  12. */
  13. namespace Gantry\Component\Assignments;
  14. /**
  15. * Class AssignmentFilter
  16. * @package Gantry\Assignments
  17. */
  18. class AssignmentFilter
  19. {
  20. protected $method;
  21. /**
  22. * Return all matching candidates with their score. Candidates are ordered by their scores.
  23. *
  24. * @param array $candidates In format of candidates[name][section][rule].
  25. * @param array $page In format of page[section][rule].
  26. * @return array
  27. */
  28. public function scores(array &$candidates, array &$page)
  29. {
  30. $matches = $this->matches($candidates, $page);
  31. $scores = [];
  32. foreach ($matches as $type => $candidate) {
  33. $scores[$type] = $this->getScore($candidate);
  34. }
  35. ksort($scores, SORT_STRING);
  36. arsort($scores);
  37. return $scores;
  38. }
  39. /**
  40. * Returns all matching candidates with matching rules.
  41. *
  42. * @param array $candidates In format of candidates[name][section][rule].
  43. * @param array $page In format of page[section][rule].
  44. * @return array
  45. */
  46. public function matches(array &$candidates, array &$page)
  47. {
  48. $matches = [];
  49. foreach ($candidates as $type => $candidate) {
  50. foreach ($candidate as $section => $list) {
  51. foreach ($list as $name => $rules) {
  52. if (isset($page[$section][$name])) {
  53. $match =\array_intersect_key($page[$section][$name], $rules);
  54. if ($match) {
  55. $matches[$type][$section][$name] = $match;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. return $matches;
  62. }
  63. /**
  64. * Returns the calculated score for the assignment.
  65. *
  66. * @param array $matches
  67. * @param string $method
  68. * @return int
  69. */
  70. public function getScore(array &$matches, $method = 'max')
  71. {
  72. $this->method = 'calc' . ucfirst($method);
  73. if (!method_exists($this, $this->method)) {
  74. $this->method = 'calcMax';
  75. }
  76. return $this->calcArray(null, $matches);
  77. }
  78. /**
  79. * @param int|float $carry
  80. * @param int|float|array $item
  81. * @return int|float
  82. * @internal
  83. */
  84. protected function calcArray($carry, $item)
  85. {
  86. if (is_array($item)) {
  87. return array_reduce($item, [$this, 'calcArray'], $carry);
  88. }
  89. $method = $this->method;
  90. return $this->{$method}($carry, $item);
  91. }
  92. /**
  93. * @param int|float $carry
  94. * @param int|float $item
  95. * @return int|float
  96. * @internal
  97. */
  98. protected function calcOr($carry, $item)
  99. {
  100. return (int) ($carry || $item);
  101. }
  102. /**
  103. * @param int|float $carry
  104. * @param int|float $item
  105. * @return int|float
  106. * @internal
  107. */
  108. protected function calcMin($carry, $item)
  109. {
  110. return isset($carry) ? min($carry, $item) : $item;
  111. }
  112. /**
  113. * @param int|float $carry
  114. * @param int|float $item
  115. * @return int|float
  116. * @internal
  117. */
  118. protected function calcMax($carry, $item)
  119. {
  120. return max($carry, $item);
  121. }
  122. /**
  123. * @param int|float $carry
  124. * @param int|float $item
  125. * @return int|float
  126. * @internal
  127. */
  128. protected function calcSum($carry, $item)
  129. {
  130. return $carry + $item;
  131. }
  132. /**
  133. * @param int|float $carry
  134. * @param int|float $item
  135. * @return int|float
  136. * @internal
  137. */
  138. protected function calcMul($carry, $item)
  139. {
  140. return isset($carry) ? $carry * $item : $item;
  141. }
  142. }