sponsors.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 getOpenCollectiveSponsors(): string
  23. {
  24. $customSponsorImages = [
  25. // For consistency and equity among sponsors, as of now, we kindly ask our sponsors
  26. // to provide an image having a width/height ratio between 1/1 and 2/1.
  27. // By default, we'll show the member picture from OpenCollective, and will resize it if bigger
  28. // int(OpenCollective.MemberId) => ImageURL
  29. ];
  30. $members = json_decode(file_get_contents('https://opencollective.com/carbon/members/all.json'), true);
  31. $list = array_filter($members, static function ($member): bool {
  32. return ($member['lastTransactionAmount'] > 3 || $member['isActive']) &&
  33. $member['role'] === 'BACKER' &&
  34. $member['type'] !== 'USER' &&
  35. (
  36. $member['totalAmountDonated'] > 100 ||
  37. $member['lastTransactionAt'] > CarbonImmutable::now()
  38. ->subMonthsNoOverflow(getMaxHistoryMonthsByAmount($member['lastTransactionAmount']))
  39. ->format('Y-m-d h:i') ||
  40. $member['isActive'] && $member['lastTransactionAmount'] >= 30
  41. );
  42. });
  43. $list = array_map(static function (array $member): array {
  44. $createdAt = CarbonImmutable::parse($member['createdAt']);
  45. $lastTransactionAt = CarbonImmutable::parse($member['lastTransactionAt']);
  46. if ($createdAt->format('d H:i:s.u') > $lastTransactionAt->format('d H:i:s.u')) {
  47. $createdAt = $createdAt
  48. ->setDay($lastTransactionAt->day)
  49. ->modify($lastTransactionAt->format('H:i:s.u'));
  50. }
  51. $monthlyContribution = (float) ($member['totalAmountDonated'] / ceil($createdAt->floatDiffInMonths()));
  52. if (
  53. $lastTransactionAt->isAfter('last month') &&
  54. $member['lastTransactionAmount'] > $monthlyContribution
  55. ) {
  56. $monthlyContribution = (float) $member['lastTransactionAmount'];
  57. }
  58. $yearlyContribution = (float) ($member['totalAmountDonated'] / max(1, $createdAt->floatDiffInYears()));
  59. $status = null;
  60. if ($monthlyContribution > 29) {
  61. $status = 'sponsor';
  62. } elseif ($monthlyContribution > 4.5 || $yearlyContribution > 29) {
  63. $status = 'backer';
  64. } elseif ($member['totalAmountDonated'] > 0) {
  65. $status = 'helper';
  66. }
  67. return array_merge($member, [
  68. 'star' => ($monthlyContribution > 98 || $yearlyContribution > 500),
  69. 'status' => $status,
  70. 'monthlyContribution' => $monthlyContribution,
  71. 'yearlyContribution' => $yearlyContribution,
  72. ]);
  73. }, $list);
  74. usort($list, static function (array $a, array $b): int {
  75. return ($b['monthlyContribution'] <=> $a['monthlyContribution'])
  76. ?: ($b['totalAmountDonated'] <=> $a['totalAmountDonated']);
  77. });
  78. return implode('', array_map(static function (array $member) use ($customSponsorImages): string {
  79. $href = htmlspecialchars($member['website'] ?? $member['profile']);
  80. $src = $customSponsorImages[$member['MemberId'] ?? ''] ?? $member['image'] ?? (strtr($member['profile'], ['https://opencollective.com/' => 'https://images.opencollective.com/']).'/avatar/256.png');
  81. [$x, $y] = @getimagesize($src) ?: [0, 0];
  82. $validImage = ($x && $y);
  83. $src = $validImage ? htmlspecialchars($src) : 'https://opencollective.com/static/images/default-guest-logo.svg';
  84. $height = $member['status'] === 'sponsor' ? 64 : 48;
  85. $width = min(128, $validImage ? round($x * $height / $y) : $height);
  86. $href .= (strpos($href, '?') === false ? '?' : '&amp;').'utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon';
  87. $title = htmlspecialchars(($member['description'] ?? null) ?: $member['name']);
  88. $alt = htmlspecialchars($member['name']);
  89. return "\n".'<a title="'.$title.'" href="'.$href.'" target="_blank">'.
  90. '<img alt="'.$alt.'" src="'.$src.'" width="'.$width.'" height="'.$height.'">'.
  91. '</a>';
  92. }, $list))."\n";
  93. }
  94. file_put_contents('readme.md', preg_replace_callback(
  95. '/(<!-- <open-collective-sponsors> -->)[\s\S]+(<!-- <\/open-collective-sponsors> -->)/',
  96. static function (array $match): string {
  97. return $match[1].getOpenCollectiveSponsors().$match[2];
  98. },
  99. file_get_contents('readme.md')
  100. ));