Skip to content

Commit

Permalink
Merge pull request #11 from keboola/webrouse-COM-596-update-jsonparse…
Browse files Browse the repository at this point in the history
…r-packages

Update packages
  • Loading branch information
michaljurecko authored Jan 14, 2021
2 parents ab60c10 + dccae5b commit a8b9356
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 321 deletions.
19 changes: 12 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
],
"require": {
"php": ">=7.4",
"keboola/php-csvtable": "^0.1",
"keboola/php-temp": "^1.0",
"keboola/php-utils": "^2.2",
"monolog/monolog": "^1.23"
"ext-json": "*",
"keboola/php-csvtable": "^1.1.1",
"keboola/php-temp": "^2.0",
"keboola/php-utils": "^4.1",
"monolog/monolog": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^6.3",
"codeclimate/php-test-reporter": "^0.4",
"squizlabs/php_codesniffer": "^3.0"
"phpunit/phpunit": "^9.0",
"phpstan/phpstan": "^0.12.14",
"php-parallel-lint/php-parallel-lint": "^1.2",
"keboola/coding-standard": ">=9.0"
},
"autoload": {
"psr-4": {
Expand All @@ -42,6 +44,9 @@
"phpcbf": "phpcbf -n --ignore=vendor --extensions=php .",
"phplint": "parallel-lint -j 10 --exclude vendor .",
"build": [
"@phplint",
"@phpcs",
"@phpstan",
"@tests"
],
"ci": [
Expand Down
79 changes: 17 additions & 62 deletions src/Analyzer.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
<?php

declare(strict_types=1);

namespace Keboola\Json;

use Keboola\Json\Exception\JsonParserException;
use Psr\Log\LoggerInterface;

class Analyzer
{
/**
* @var bool
*/
protected $strict;
protected bool $strict;

/**
* @var bool
*/
protected $nestedArrayAsJson;
protected bool $nestedArrayAsJson;

/**
* @var LoggerInterface
*/
protected $log;
protected LoggerInterface $log;

/**
* @var Structure
*/
private $structure;
private Structure $structure;

/**
* Analyzer constructor.
* @param LoggerInterface $logger
* @param Structure $structure
* @param bool $nestedArraysAsJson
* @param bool $strict
*/
public function __construct(
LoggerInterface $logger,
Structure $structure,
Expand All @@ -46,27 +29,17 @@ public function __construct(
$this->structure = $structure;
}

/**
* @return Structure
*/
public function getStructure()
public function getStructure(): Structure
{
return $this->structure;
}

/**
* @return LoggerInterface
*/
public function getLogger()
public function getLogger(): LoggerInterface
{
return $this->log;
}

/**
* @param array $data
* @param string $rootType
*/
public function analyzeData(array $data, string $rootType)
public function analyzeData(array $data, string $rootType): void
{
if (empty($data)) {
return;
Expand All @@ -77,12 +50,9 @@ public function analyzeData(array $data, string $rootType)
}

/**
* @param $item
* @param NodePath $nodePath
* @return string
* @throws JsonParserException
* @param mixed $item
*/
private function analyzeItem($item, NodePath $nodePath) : string
private function analyzeItem($item, NodePath $nodePath): string
{
if (is_scalar($item)) {
if ($this->strict) {
Expand Down Expand Up @@ -115,11 +85,7 @@ private function analyzeItem($item, NodePath $nodePath) : string
return $nodeType;
}

/**
* @param array $array
* @param NodePath $nodePath
*/
private function analyzeArray(array $array, NodePath $nodePath)
private function analyzeArray(array $array, NodePath $nodePath): void
{
$oldType = null;
$nodePath = $nodePath->addChild(Structure::ARRAY_NAME);
Expand All @@ -133,26 +99,18 @@ private function analyzeArray(array $array, NodePath $nodePath)
}
}

/**
* @param $object
* @param NodePath $nodePath
*/
private function analyzeObject($object, NodePath $nodePath)
private function analyzeObject(object $object, NodePath $nodePath): void
{
foreach ($object as $key => $field) {
$this->analyzeItem($field, $nodePath->addChild($key));
foreach (get_object_vars($object) as $key => $field) {
$this->analyzeItem($field, $nodePath->addChild((string) $key));
}
}

/**
* Check that two types same or compatible.
* @param $oldType
* @param $newType
* @param NodePath $nodePath
* @return string
* @throws JsonParserException
*/
private function checkType($oldType, $newType, NodePath $nodePath) : string
private function checkType(?string $oldType, string $newType, NodePath $nodePath): string
{
if (!is_null($oldType) && ($newType !== $oldType) && ($newType !== 'null') && ($oldType !== 'null')) {
throw new JsonParserException(
Expand All @@ -162,10 +120,7 @@ private function checkType($oldType, $newType, NodePath $nodePath) : string
return $newType;
}

/**
* @return bool
*/
public function getNestedArrayAsJson() : bool
public function getNestedArrayAsJson(): bool
{
return $this->nestedArrayAsJson;
}
Expand Down
25 changes: 13 additions & 12 deletions src/Cache.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
<?php

declare(strict_types=1);

namespace Keboola\Json;

class Cache
{
protected $data = [];
protected array $data = [];

/**
* PHP temp://temp/
* @var resource
*/
protected $temp;

protected $readPosition = 0;
protected int $readPosition = 0;

protected $memoryLimit = null;
protected ?int $memoryLimit = null;

public function store($data)
public function store(array $data): void
{
// TODO ensure at least X MB is left free (X should be possible to change -> Parser::getCache()->setMemLimit(X))
// either to stop using memory once X mem is used or once X is left from PHP limit
if (ini_get('memory_limit') != "-1"
if (ini_get('memory_limit') !== '-1'
&& memory_get_usage() > (\Keboola\Utils\returnBytes(ini_get('memory_limit')) * 0.25)
|| ($this->memoryLimit !== null && memory_get_usage() > $this->memoryLimit)
) {
// cache
if (empty($this->temp)) {
// TODO use /maxmemory ?
$this->temp = fopen("php://temp/", 'w+');
/** @var resource $temp */
$temp = fopen('php://temp/', 'w+');
$this->temp = $temp;
}

fseek($this->temp, 0, SEEK_END);
Expand All @@ -37,14 +41,14 @@ public function store($data)
}
}

public function getNext()
public function getNext(): ?array
{
if (!empty($this->temp) && !feof($this->temp)) {
// keep the file position in case the file's been written to
fseek($this->temp, $this->readPosition);
$data = fgets($this->temp);
/** @var string $data */
$this->readPosition += strlen($data);

return unserialize(base64_decode($data));
} elseif (!empty($this->temp) && feof($this->temp)) {
fclose($this->temp);
Expand All @@ -54,10 +58,7 @@ public function getNext()
return array_shift($this->data);
}

/**
* @param int $limit
*/
public function setMemoryLimit(int $limit)
public function setMemoryLimit(int $limit): void
{
$this->memoryLimit = $limit;
}
Expand Down
22 changes: 11 additions & 11 deletions src/CsvRow.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
<?php

declare(strict_types=1);

namespace Keboola\Json;

use Keboola\Json\Exception\JsonParserException;

class CsvRow
{
/**
* @var array
*/
protected $data = [];
protected array $data = [];

public function __construct(array $columns)
{
$this->data = array_fill_keys($columns, null);
}

public function setValue($column, $value)
/**
* @param mixed $value
* @throws JsonParserException
*/
public function setValue(string $column, $value): void
{
if (!array_key_exists($column, $this->data)) {
throw new JsonParserException(
"Error assigning '{$value}' to a non-existing column '{$column}'!",
[
'columns' => array_keys($this->data)
'columns' => array_keys($this->data),
]
);
}
Expand All @@ -32,18 +35,15 @@ public function setValue($column, $value)
"Error assigning value to '{$column}': The value's not scalar!",
[
'type' => gettype($value),
'value' => json_encode($value)
'value' => json_encode($value),
]
);
}

$this->data[$column] = $value;
}

/**
* @return array
*/
public function getRow()
public function getRow(): array
{
return $this->data;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/InconsistentValueException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Keboola\Json\Exception;

class InconsistentValueException extends \Exception
Expand Down
5 changes: 4 additions & 1 deletion src/Exception/JsonParserException.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

declare(strict_types=1);

namespace Keboola\Json\Exception;

use Keboola\Utils\Exception;

class JsonParserException extends Exception
{
public function __construct($message = "", array $data = [], $code = 0, $previous = null)
public function __construct(string $message = '', array $data = [], int $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->setData($data);
Expand Down
3 changes: 3 additions & 0 deletions src/Exception/NoDataException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Keboola\Json\Exception;

class NoDataException extends JsonParserException
Expand Down
Loading

0 comments on commit a8b9356

Please sign in to comment.