mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 23:23:51 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Serializer\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use function array_keys;
|
||||
use function is_array;
|
||||
|
||||
class DocGenEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function encode($data, string $format, array $context = [])
|
||||
{
|
||||
if (!$this->isAssociative($data)) {
|
||||
throw new UnexpectedValueException("Only associative arrays are allowed; lists are not allowed");
|
||||
throw new UnexpectedValueException('Only associative arrays are allowed; lists are not allowed');
|
||||
}
|
||||
|
||||
$result = [];
|
||||
@@ -22,11 +27,28 @@ class DocGenEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInte
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function supportsEncoding(string $format)
|
||||
{
|
||||
return 'docgen' === $format;
|
||||
}
|
||||
|
||||
private function canonicalizeKey(string $path, string $key): string
|
||||
{
|
||||
return '' === $path ? $key : $path . '_' . $key;
|
||||
}
|
||||
|
||||
private function isAssociative(array $data)
|
||||
{
|
||||
$keys = array_keys($data);
|
||||
|
||||
return array_keys($keys) !== $keys;
|
||||
}
|
||||
|
||||
private function recusiveEncoding(array $data, array &$result, $path)
|
||||
{
|
||||
if ($this->isAssociative($data)) {
|
||||
foreach ($data as $key => $value) {
|
||||
if (\is_array($value)) {
|
||||
if (is_array($value)) {
|
||||
$this->recusiveEncoding($value, $result, $this->canonicalizeKey($path, $key));
|
||||
} else {
|
||||
$result[$this->canonicalizeKey($path, $key)] = $value;
|
||||
@@ -34,9 +56,8 @@ class DocGenEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInte
|
||||
}
|
||||
} else {
|
||||
foreach ($data as $elem) {
|
||||
|
||||
if (!$this->isAssociative($elem)) {
|
||||
throw new UnexpectedValueException(sprintf("Embedded loops are not allowed. See data under %s path", $path));
|
||||
throw new UnexpectedValueException(sprintf('Embedded loops are not allowed. See data under %s path', $path));
|
||||
}
|
||||
|
||||
$sub = [];
|
||||
@@ -45,25 +66,4 @@ class DocGenEncoder implements \Symfony\Component\Serializer\Encoder\EncoderInte
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function canonicalizeKey(string $path, string $key): string
|
||||
{
|
||||
return $path === '' ? $key : $path.'_'.$key;
|
||||
}
|
||||
|
||||
private function isAssociative(array $data)
|
||||
{
|
||||
$keys = \array_keys($data);
|
||||
|
||||
return $keys !== \array_keys($keys);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function supportsEncoding(string $format)
|
||||
{
|
||||
return $format === 'docgen';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user