Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed Array to string conversion for Config textarea value #145

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Playbloom/Satisfy/Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public function indexAction(Request $request): Response

$manager = $this->get(Manager::class);
$config = $manager->getConfig();
$config->convertConfigToJsonString();

$form = $this->createForm(ConfigurationType::class, $config);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$config->convertConfigToArray();
$manager->flush();
}

Expand Down
39 changes: 37 additions & 2 deletions src/Playbloom/Satisfy/Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Configuration
private $abandoned;

/**
* @var array|null
* @var array|null|string
*/
private $config;

Expand Down Expand Up @@ -355,7 +355,10 @@ public function setTwigTemplate(string $twigTemplate = null): void
$this->twigTemplate = $twigTemplate;
}

public function getConfig(): ?array
/**
* @return array|string|null
*/
public function getConfig()
{
return $this->config;
}
Expand Down Expand Up @@ -392,4 +395,36 @@ public function setPrettyPrint(bool $prettyPrint): self

return $this;
}

private function isJson($string)
{
if (!is_string($string)) {
return false;
}
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}

public function convertConfigToJsonString()
{
if ($this->isJson($this->config)) {
return;
}
if (is_array($this->config)) {
$this->config = json_encode($this->config, JSON_PRETTY_PRINT);
return;
}
$this->config = null;
}

public function convertConfigToArray()
{
if (is_string($this->config) && ($config = json_decode($this->config, true))) {
$this->config = $config;
return;
}
if (!is_array($this->config)) {
$this->config = null;
}
}
}