Skip to content

Commit

Permalink
Fix bug in login functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Nov 26, 2023
1 parent a8dfd1c commit 34fb1b9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/Utils/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Kiwilan\Steward\Utils;

use Illuminate\Support\Facades\File;
use Kiwilan\HttpPool\Utils\PrintConsole;

class Json
{
protected ?string $path = null;

protected mixed $contents = null;

/**
* @param mixed $data Can be a file path, json string, or array
*/
public function __construct(
readonly protected mixed $data,
) {
if (is_file($this->data)) {
$this->path = $this->data;
$this->contents = file_get_contents($this->data);
} elseif (is_string($this->data)) {
$this->contents = json_decode($this->data, true);
} else {
$this->contents = $this->data;
}
}

/**
* @return string Pretty json string
*/
public function pretty(): string
{
return json_encode($this->contents, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}

/**
* @param string $saveTo Path to save the json file
* @param bool $console Print save path to console
*/
public function save(string $saveTo, bool $console = true): void
{
$pretty = $this->pretty();

if (! is_dir($saveTo)) {
mkdir($saveTo, recursive: true);
}

unlink($saveTo);
file_put_contents($saveTo, $pretty);

if ($console) {
$print = PrintConsole::make();
$print->print("Saved to `{$saveTo}`.");
}
}

public function toArray(bool $is_associative = true): array
{
return json_decode($this->contents, $is_associative);
}

public function toObject(): object
{
return json_decode(json_encode($this->contents, JSON_FORCE_OBJECT));
}

public function __toString()
{
return json_encode($this->contents);
}
}

0 comments on commit 34fb1b9

Please sign in to comment.