FormatsPagarmeData.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Services\Pagarme\Concerns;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Str;
  5. trait FormatsPagarmeData
  6. {
  7. protected function digits(?string $value): string
  8. {
  9. return preg_replace('/\D+/', '', (string) $value) ?? '';
  10. }
  11. protected function customerDocument(?string $value): string
  12. {
  13. $document = trim((string) $value);
  14. if (preg_match('/[A-Za-z]/', $document) === 1) {
  15. return $document;
  16. }
  17. return $this->digits($document);
  18. }
  19. protected function customerDocumentType(string $document): string
  20. {
  21. if (preg_match('/[A-Za-z]/', $document) === 1) {
  22. return 'PASSPORT';
  23. }
  24. $length = strlen($this->digits($document));
  25. return match ($length) {
  26. 11 => 'CPF',
  27. 14 => 'CNPJ',
  28. default => 'PASSPORT',
  29. };
  30. }
  31. protected function extractAddressParts(array $data): array
  32. {
  33. $addressLine = trim((string) ($data['address'] ?? ''));
  34. $segments = array_map('trim', explode(',', $addressLine));
  35. $streetSegment = $segments[0] ?? '';
  36. if (($data['number'] ?? null) === null) {
  37. preg_match('/^(\d+)/', $streetSegment, $matches);
  38. }
  39. return [
  40. 'street' => $streetSegment,
  41. 'street_number' => (string) ($data['number'] ?? $matches[1] ?? 'S/N'),
  42. 'neighborhood' => (string) ($data['district'] ?? $segments[1] ?? 'N/A'),
  43. 'reference_point' => (string) ($data['reference_point'] ?? 'N/A'),
  44. 'complementary' => (string) ($data['complement'] ?? 'N/A'),
  45. ];
  46. }
  47. protected function formatBirthdate(mixed $birthdate): ?string
  48. {
  49. if ($birthdate === null || $birthdate === '') {
  50. return null;
  51. }
  52. if ($birthdate instanceof \DateTimeInterface) {
  53. return Carbon::instance($birthdate)->format('d/m/Y');
  54. }
  55. $birthdate = trim((string) $birthdate);
  56. if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $birthdate) === 1) {
  57. return $birthdate;
  58. }
  59. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate) === 1) {
  60. return Carbon::createFromFormat('Y-m-d', $birthdate)->format('d/m/Y');
  61. }
  62. return Carbon::parse($birthdate)->format('d/m/Y');
  63. }
  64. protected function normalizeHolderName(string $holderName): string
  65. {
  66. $holderName = trim(preg_replace('/\s+/', ' ', $holderName) ?? '');
  67. if (Str::length($holderName) < 30) {
  68. return $holderName;
  69. }
  70. $parts = explode(' ', $holderName);
  71. if (count($parts) >= 3) {
  72. $firstName = array_shift($parts);
  73. $lastName = array_pop($parts);
  74. $initials = array_map(
  75. static fn (string $part): string => Str::upper(Str::substr($part, 0, 1)),
  76. $parts
  77. );
  78. $abbreviated = trim($firstName.' '.implode(' ', $initials).' '.$lastName);
  79. if (Str::length($abbreviated) < 30) {
  80. return $abbreviated;
  81. }
  82. $firstAndLast = trim($firstName.' '.$lastName);
  83. if (Str::length($firstAndLast) < 30) {
  84. return $firstAndLast;
  85. }
  86. }
  87. return Str::limit($holderName, 29, '');
  88. }
  89. }