FormatsPagarmeData.php 3.3 KB

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