Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…flare-analytics into grouped
  • Loading branch information
murdercode committed Aug 5, 2024
2 parents 6928453 + f27264b commit c2db393
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/CloudflareAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace The3LabsTeam\PhpCloudflareAnalytics;

use Dotenv\Dotenv;
use DateTime;
use DateInterval;
use DateTime;
use Dotenv\Dotenv;

class CloudflareAnalytics
{
Expand Down Expand Up @@ -102,6 +102,7 @@ public function orderBy(string $context, string $field, string $direction = 'ASC
public function take($alias, $limit)
{
$this->takes[$alias] = $limit;

return $this;
}

Expand Down Expand Up @@ -159,34 +160,35 @@ private function formatFields(array $fields, $alias)
{
$formattedFields = [];
foreach ($fields as $field) {
$field = str_replace("$alias.", "", $field); // Rimuove il prefisso alias
$field = str_replace("$alias.", '', $field); // Rimuove il prefisso alias
$parts = explode('.', $field);
$current = &$formattedFields;
foreach ($parts as $part) {
if (!isset($current[$part])) {
if (! isset($current[$part])) {
$current[$part] = [];
}
$current = &$current[$part];
}
}

return $this->buildFieldString($formattedFields);
}

private function buildFieldString(array $fields, $indent = 2)
{
$result = '';
foreach ($fields as $key => $value) {
$result .= str_repeat(' ', $indent) . $key;
if (is_array($value) && !empty($value)) {
$result .= " {\n" . $this->buildFieldString($value, $indent + 2) . str_repeat(' ', $indent) . "}\n";
$result .= str_repeat(' ', $indent).$key;
if (is_array($value) && ! empty($value)) {
$result .= " {\n".$this->buildFieldString($value, $indent + 2).str_repeat(' ', $indent)."}\n";
} else {
$result .= "\n";
}
}

return $result;
}


private function formatOrderBy(array $orderBy)
{
return implode("\n", array_map(fn ($o) => $o, $orderBy));
Expand Down
1 change: 0 additions & 1 deletion tests/CloudflareAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,3 @@
$this->assertIsArray($results);
$this->assertGreaterThan(0, $results);
});

0 comments on commit c2db393

Please sign in to comment.