Skip to content

Commit

Permalink
Aggiunta integrazione Contatti-ContactLists
Browse files Browse the repository at this point in the history
  • Loading branch information
HighLiuk committed Oct 11, 2021
1 parent dd28e2d commit 2f8c1f9
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Contacts/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,60 @@ public function listAllLoop(array $query_params = [], int $contacts_per_page = 1
return $contacts;
}

/**
* List all contacts
*
* Li elenca tutti, iterando sulla paginazione, e aggiunge pure i contactLists
*/
public function listAllWithContactLists($debug = false, int $contacts_per_page = 100): array
{
// aggiungo ai parametri della query i contactLists
$query_params = [
'include' => 'contactLists',
];

// Risposta JSON dal server
$res = $this->listAll($query_params, $contacts_per_page);

// Converto la risposta in array
$res = json_decode($res, true);

// Calcolo le pagine, i.e. numero di richieste che devo fare in totale
$total = (int) $res['meta']['total'];
$pages = (int) ceil($total / $contacts_per_page);

// Estraggo le informazioni su contatti e contactLists
$contacts = $res['contacts'] ?? [];
$contactLists = $res['contactLists'] ?? [];

if ($debug) {
dump('Scaricata pagina 1 / ' . $pages);
}

// Loop sulle pagine
for ($page = 1; $page < $pages; $page++) {
$res = $this->listAll(
$query_params,
$contacts_per_page,
$page * $contacts_per_page
);
$res = json_decode($res, true);

// aggiungo i risultati
$contacts = array_merge($contacts, $res['contacts']);
$contactLists = array_merge($contactLists, $res['contactLists']);

if ($debug) {
dump('Scaricata pagina ' . ($page + 1) . ' / ' . $pages);
}
}

return [
'contacts' => $contacts,
'contactLists' => $contactLists,
];
}

/**
* Aggiorna l'account di un dato contatto. Serve però passare
* NON l'id del contatto
Expand Down

0 comments on commit 2f8c1f9

Please sign in to comment.