sponsors.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. $members[] = [
  40. 'MemberId' => 1,
  41. 'createdAt' => '2019-01-01 02:00',
  42. 'type' => 'ORGANIZATION',
  43. 'role' => 'BACKER',
  44. 'tier' => 'backer+',
  45. 'isActive' => true,
  46. 'totalAmountDonated' => 1000,
  47. 'currency' => 'USD',
  48. 'lastTransactionAt' => CarbonImmutable::now()->format('Y-m-d').' 02:00',
  49. 'lastTransactionAmount' => 25,
  50. 'profile' => 'https://tidelift.com/',
  51. 'name' => 'Tidelift',
  52. 'description' => 'Get professional support for Carbon',
  53. 'image' => 'https://carbon.nesbot.com/tidelift-brand.png',
  54. 'website' => 'https://tidelift.com/subscription/pkg/packagist-nesbot-carbon?utm_source=packagist-nesbot-carbon&utm_medium=referral&utm_campaign=docs',
  55. ];
  56. $list = array_filter($members, static function ($member): bool {
  57. return ($member['lastTransactionAmount'] > 3 || $member['isActive']) &&
  58. $member['role'] === 'BACKER' &&
  59. $member['type'] !== 'USER' &&
  60. (
  61. $member['totalAmountDonated'] > 100 ||
  62. $member['lastTransactionAt'] > CarbonImmutable::now()
  63. ->subMonthsNoOverflow(getMaxHistoryMonthsByAmount($member['lastTransactionAmount']))
  64. ->format('Y-m-d h:i') ||
  65. $member['isActive'] && $member['lastTransactionAmount'] >= 30
  66. );
  67. });
  68. $list = array_map(static function (array $member): array {
  69. $createdAt = CarbonImmutable::parse($member['createdAt']);
  70. $lastTransactionAt = CarbonImmutable::parse($member['lastTransactionAt']);
  71. if ($createdAt->format('d H:i:s.u') > $lastTransactionAt->format('d H:i:s.u')) {
  72. $createdAt = $createdAt
  73. ->setDay($lastTransactionAt->day)
  74. ->modify($lastTransactionAt->format('H:i:s.u'));
  75. }
  76. $monthlyContribution = (float) ($member['totalAmountDonated'] / ceil($createdAt->floatDiffInMonths()));
  77. if (
  78. $lastTransactionAt->isAfter('last month') &&
  79. $member['lastTransactionAmount'] > $monthlyContribution
  80. ) {
  81. $monthlyContribution = (float) $member['lastTransactionAmount'];
  82. }
  83. if ($lastTransactionAt->isBefore('-75 days')) {
  84. $days = min(120, $lastTransactionAt->diffInDays('now') - 70);
  85. $monthlyContribution *= 1 - $days / 240;
  86. }
  87. $yearlyContribution = (float) ($member['totalAmountDonated'] / max(1, $createdAt->floatDiffInYears()));
  88. $status = null;
  89. $rank = 0;
  90. if ($monthlyContribution > 29 || $yearlyContribution > 700) {
  91. $status = 'sponsor';
  92. $rank = 4;
  93. } elseif ($monthlyContribution > 14.5 || $yearlyContribution > 500) {
  94. $status = 'backerPlus';
  95. $rank = 3;
  96. } elseif ($monthlyContribution > 4.5 || $yearlyContribution > 80) {
  97. $status = 'backer';
  98. $rank = 2;
  99. } elseif ($member['totalAmountDonated'] > 0) {
  100. $status = 'helper';
  101. $rank = 1;
  102. }
  103. return array_merge($member, [
  104. 'star' => ($monthlyContribution > 98 || $yearlyContribution > 800),
  105. 'status' => $status,
  106. 'rank' => $rank,
  107. 'monthlyContribution' => $monthlyContribution,
  108. 'yearlyContribution' => $yearlyContribution,
  109. ]);
  110. }, $list);
  111. usort($list, static function (array $a, array $b): int {
  112. return ($b['star'] <=> $a['star'])
  113. ?: ($b['rank'] <=> $a['rank'])
  114. ?: ($b['monthlyContribution'] <=> $a['monthlyContribution'])
  115. ?: ($b['totalAmountDonated'] <=> $a['totalAmountDonated']);
  116. });
  117. return implode('', array_map(static function (array $member) use ($customSponsorImages): string {
  118. $href = htmlspecialchars($member['website'] ?? $member['profile']);
  119. $src = $customSponsorImages[$member['MemberId'] ?? ''] ?? $member['image'] ?? (strtr($member['profile'], ['https://opencollective.com/' => 'https://images.opencollective.com/']).'/avatar/256.png');
  120. [$x, $y] = @getimagesize($src) ?: [0, 0];
  121. $validImage = ($x && $y);
  122. $src = $validImage ? htmlspecialchars($src) : 'https://opencollective.com/static/images/default-guest-logo.svg';
  123. $height = match ($member['status']) {
  124. 'sponsor' => 64,
  125. 'backerPlus' => 42,
  126. 'backer' => 32,
  127. default => 24,
  128. };
  129. $rel = match ($member['status']) {
  130. 'sponsor', 'backerPlus' => '',
  131. default => ' rel="sponsored"',
  132. };
  133. $width = min($height * 2, $validImage ? round($x * $height / $y) : $height);
  134. $href .= (strpos($href, '?') === false ? '?' : '&amp;').'utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon';
  135. $title = getHtmlAttribute(($member['description'] ?? null) ?: $member['name']);
  136. $alt = getHtmlAttribute($member['name']);
  137. if ($member['star']) {
  138. $width *= 1.5;
  139. $height *= 1.5;
  140. }
  141. return "\n".'<a title="'.$title.'" href="'.$href.'" target="_blank"'.$rel.'>'.
  142. '<img alt="'.$alt.'" src="'.$src.'" width="'.$width.'" height="'.$height.'">'.
  143. '</a>';
  144. }, $list))."\n";
  145. }
  146. file_put_contents('readme.md', preg_replace_callback(
  147. '/(<!-- <open-collective-sponsors> -->)[\s\S]+(<!-- <\/open-collective-sponsors> -->)/',
  148. static function (array $match): string {
  149. return $match[1].getOpenCollectiveSponsors().$match[2];
  150. },
  151. file_get_contents('readme.md')
  152. ));