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