sponsors.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Carbon\CarbonImmutable;
  11. require_once __DIR__.'/vendor/autoload.php';
  12. function getMaxHistoryMonthsByAmount($amount): int
  13. {
  14. if ($amount >= 50) {
  15. return 6;
  16. }
  17. if ($amount >= 20) {
  18. return 4;
  19. }
  20. return 2;
  21. }
  22. function getHtmlAttribute($rawValue): string
  23. {
  24. return str_replace(
  25. ['​', "\r"],
  26. '',
  27. trim(htmlspecialchars((string) $rawValue), "  \n\r\t\v\0"),
  28. );
  29. }
  30. function getOpenCollectiveSponsors(): string
  31. {
  32. $customSponsorImages = [
  33. // For consistency and equity among sponsors, as of now, we kindly ask our sponsors
  34. // to provide an image having a width/height ratio between 1/1 and 2/1.
  35. // By default, we'll show the member picture from OpenCollective, and will resize it if bigger
  36. // int(OpenCollective.MemberId) => ImageURL
  37. ];
  38. $members = json_decode(file_get_contents('https://opencollective.com/carbon/members/all.json'), true);
  39. $list = array_filter($members, static function ($member): bool {
  40. return ($member['lastTransactionAmount'] > 3 || $member['isActive']) &&
  41. $member['role'] === 'BACKER' &&
  42. $member['type'] !== 'USER' &&
  43. (
  44. $member['totalAmountDonated'] > 100 ||
  45. $member['lastTransactionAt'] > CarbonImmutable::now()
  46. ->subMonthsNoOverflow(getMaxHistoryMonthsByAmount($member['lastTransactionAmount']))
  47. ->format('Y-m-d h:i') ||
  48. $member['isActive'] && $member['lastTransactionAmount'] >= 30
  49. );
  50. });
  51. $list = array_map(static function (array $member): array {
  52. $createdAt = CarbonImmutable::parse($member['createdAt']);
  53. $lastTransactionAt = CarbonImmutable::parse($member['lastTransactionAt']);
  54. if ($createdAt->format('d H:i:s.u') > $lastTransactionAt->format('d H:i:s.u')) {
  55. $createdAt = $createdAt
  56. ->setDay($lastTransactionAt->day)
  57. ->modify($lastTransactionAt->format('H:i:s.u'));
  58. }
  59. $monthlyContribution = (float) ($member['totalAmountDonated'] / ceil($createdAt->floatDiffInMonths()));
  60. if (
  61. $lastTransactionAt->isAfter('last month') &&
  62. $member['lastTransactionAmount'] > $monthlyContribution
  63. ) {
  64. $monthlyContribution = (float) $member['lastTransactionAmount'];
  65. }
  66. $yearlyContribution = (float) ($member['totalAmountDonated'] / max(1, $createdAt->floatDiffInYears()));
  67. $status = null;
  68. if ($monthlyContribution > 29) {
  69. $status = 'sponsor';
  70. } elseif ($monthlyContribution > 4.5 || $yearlyContribution > 29) {
  71. $status = 'backer';
  72. } elseif ($member['totalAmountDonated'] > 0) {
  73. $status = 'helper';
  74. }
  75. return array_merge($member, [
  76. 'star' => ($monthlyContribution > 98 || $yearlyContribution > 500),
  77. 'status' => $status,
  78. 'monthlyContribution' => $monthlyContribution,
  79. 'yearlyContribution' => $yearlyContribution,
  80. ]);
  81. }, $list);
  82. usort($list, static function (array $a, array $b): int {
  83. return ($b['monthlyContribution'] <=> $a['monthlyContribution'])
  84. ?: ($b['totalAmountDonated'] <=> $a['totalAmountDonated']);
  85. });
  86. return implode('', array_map(static function (array $member) use ($customSponsorImages): string {
  87. $href = htmlspecialchars($member['website'] ?? $member['profile']);
  88. $src = $customSponsorImages[$member['MemberId'] ?? ''] ?? $member['image'] ?? (strtr($member['profile'], ['https://opencollective.com/' => 'https://images.opencollective.com/']).'/avatar/256.png');
  89. [$x, $y] = @getimagesize($src) ?: [0, 0];
  90. $validImage = ($x && $y);
  91. $src = $validImage ? htmlspecialchars($src) : 'https://opencollective.com/static/images/default-guest-logo.svg';
  92. $height = $member['status'] === 'sponsor' ? 64 : 42;
  93. $width = min($height * 2, $validImage ? round($x * $height / $y) : $height);
  94. $href .= (strpos($href, '?') === false ? '?' : '&amp;').'utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon';
  95. $title = getHtmlAttribute(($member['description'] ?? null) ?: $member['name']);
  96. $alt = getHtmlAttribute($member['name']);
  97. return "\n".'<a title="'.$title.'" href="'.$href.'" target="_blank">'.
  98. '<img alt="'.$alt.'" src="'.$src.'" width="'.$width.'" height="'.$height.'">'.
  99. '</a>';
  100. }, $list))."\n";
  101. }
  102. file_put_contents('readme.md', preg_replace_callback(
  103. '/(<!-- <open-collective-sponsors> -->)[\s\S]+(<!-- <\/open-collective-sponsors> -->)/',
  104. static function (array $match): string {
  105. return $match[1].getOpenCollectiveSponsors().$match[2];
  106. },
  107. file_get_contents('readme.md')
  108. ));