Skip to content

Commit

Permalink
Merge pull request #107 from bakaphp/0.7-custom-fields-redis-json-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken authored Mar 20, 2021
2 parents 1a3aad9 + 29e307a commit 0394922
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/Contracts/CustomFields/CustomFieldsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Baka\Database\CustomFields\CustomFields;
use Baka\Database\CustomFields\CustomFieldsModules;
use Baka\Database\Model;
use function Baka\isJson;
use Baka\Support\Str;
use Phalcon\Di;
use Phalcon\Mvc\ModelInterface;
use Phalcon\Utils\Slug;
Expand Down Expand Up @@ -99,7 +99,7 @@ public function getAll() : array
$listOfCustomFields = [];

while ($row = $result->fetch()) {
$listOfCustomFields[$row['name']] = !isJson($row['value']) ? $row['value'] : json_decode($row['value'], true);
$listOfCustomFields[$row['name']] = Str::jsonToArray($row['value']);
}

return $listOfCustomFields;
Expand All @@ -116,9 +116,15 @@ public function getAllFromRedis() : array
if (Di::getDefault()->has('redis')) {
$redis = Di::getDefault()->get('redis');

return $redis->hGetAll(
$fields = $redis->hGetAll(
$this->getCustomFieldPrimaryKey(),
);

foreach ($fields as $key => $value) {
$fields[$key] = Str::jsonToArray($value);
}

return $fields;
}

return [];
Expand All @@ -138,7 +144,7 @@ public function get(string $name)
}

if ($field = $this->getCustomField($name)) {
return is_string($field->value) && isJson($field->value) ? json_decode($field->value, true) : $field->value;
return Str::jsonToArray($field->value);
}

return ;
Expand Down Expand Up @@ -207,7 +213,7 @@ protected function getFromRedis(string $name)
$name
);

return is_string($value) && isJson($value) ? json_decode($value, true) : $value;
return Str::jsonToArray($value);
}

return false;
Expand Down
3 changes: 1 addition & 2 deletions src/Elasticsearch/Models/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Baka\Elasticsearch\Models;

use Baka\Contracts\CustomFields\CustomFieldsTrait;
use Baka\Contracts\Database\ElasticModelInterface;
use Baka\Elasticsearch\Client;
use Baka\Elasticsearch\IndexBuilder;
Expand All @@ -27,7 +26,7 @@ public static function add(ElasticModelInterface $model, int $maxDepth = 3) : ar
$document = $model->toArray();

//merge custom fields
if (in_array(CustomFieldsTrait::class, class_uses(get_class($model)))) {
if (method_exists($model, 'getAll')) {
$document = array_merge($document, $model->getAll());
}

Expand Down
14 changes: 14 additions & 0 deletions src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Baka\Support;

use function Baka\isJson;

class Str
{
/**
Expand Down Expand Up @@ -232,4 +234,16 @@ public static function slug(string $string) : string
{
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
}

/**
* Given a json string decode it into array.
*
* @param mixed $string
*
* @return array|?string|mixed
*/
public static function jsonToArray($string)
{
return is_string($string) && isJson($string) ? json_decode($string, true) : $string;
}
}
19 changes: 19 additions & 0 deletions tests/unit/Support/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,23 @@ public function testLetterPlusNumber()
$this->assertSame('D', Str::letterPlusNumber('A', 3));
$this->assertSame('D', Str::letterPlusNumber('D', 0));
}

public function testJsonToArray()
{
$json = '[{"id":"57","name":"Clientes","description":"e","scope":0,"companies_id":1,"apps_id":15,"created_at":"2019-06-11 20:34:05","updated_at":"2021-01-24 01:44:03","is_default":1,"is_active":1,"is_deleted":0}]';
$jsonTwo = '{"settingsmenu":{"app-settings":0,"companies-manager":0,"companies-settings":0}}';
$array = Str::jsonToArray($json);
$arrayTwo = Str::jsonToArray($jsonTwo);

$this->assertIsArray($array);
$this->assertIsArray($arrayTwo);
}

public function testEmptyJsonNotArray()
{
$json = null;
$array = Str::jsonToArray($json);

$this->assertIsNotArray($array);
}
}

0 comments on commit 0394922

Please sign in to comment.