Skip to content

Commit

Permalink
PHP 8 support, minimal php version 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSuperStar committed Jul 4, 2023
1 parent dffadb8 commit 87b396d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
4 changes: 1 addition & 3 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,4 @@ Result
code count: 2
```

PS: about the use of memory, I can not say anything, like everything is transmitted by links, but I'm not sure.

Translated Google Translate
Translated by Google Translate
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,3 @@ echo 'code count: ',count($json->address->code);
}
code count: 2
```


PS: по поводу расходования памяти ничего сказать не могу, вроде все передается по ссылкам но я не уверен.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "alexsuperstar/jsonmaker",
"description": "PHP JSON Maker",
"type": "library",
"version": "1.3.0",
"version": "1.4.0",
"authors": [
{
"name": "Alexey Starikov",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0"
"php": ">=7.4.0"

},
"autoload": {
Expand Down
111 changes: 62 additions & 49 deletions src/alexstar/JsonMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ class JsonMaker implements \ArrayAccess, \IteratorAggregate, \Countable
public function __construct($str = null)
{
if ($str) {
if(is_string($str)){
if (is_string($str)) {
$data = json_decode($str);
if (json_last_error()) throw new \Exception(json_last_error_msg());
$this->parse($data);
}else{
}
else {
$this->parse(json_decode(json_encode($str)));
}
}
Expand All @@ -39,51 +40,58 @@ public function parse($data)
if (is_object($data)) {
if (is_scalar($v)) {
$this->$k = $v;
} else {
}
else {
$this->$k->parse($v);
}
} elseif (is_array($data)) {
}
elseif (is_array($data)) {
if (is_scalar($v)) {
$this[$k] = $v;
} else {
}
else {
$this[$k]->parse($v);
}
}
}
}
}

/*
* set/get value by path
* */
public function __invoke()
{
if(func_num_args()>0){
$path=func_get_arg(0);
if(func_num_args()>1) {
$newVal = func_get_arg(1);
}
$p = explode('/', trim($path,'/'));
$vars =& $this;
foreach ($p as $k => $v) {
if(is_numeric($v)){# array
if(!isset($vars[$v]) && !isset($newVal)) return null;
$vars=&$vars[$v];
}else{ # method
if(!isset($vars->{$v}) && !isset($newVal)) return null;
$vars =&$vars->{$v};
}
}
if(isset($newVal)) {
if (is_numeric($v)) {
$vars[$v] = is_scalar($newVal)?$newVal:new self($newVal);
} else {
$vars = is_scalar($newVal)?$newVal:new self($newVal);
}
}
return $vars;
}
return null;
}
public function __invoke()
{
if (func_num_args() > 0) {
$path = func_get_arg(0);
if (func_num_args() > 1) {
$newVal = func_get_arg(1);
}
$p = explode('/', trim($path, '/'));
$vars =& $this;
foreach ($p as $k => $v) {
if (is_numeric($v)) {# array
if (!isset($vars[$v]) && !isset($newVal)) return null;
$vars =& $vars[$v];
}
else { # method
if (!isset($vars->{$v}) && !isset($newVal)) return null;
$vars =& $vars->{$v};
}
}
if (isset($newVal)) {
if (is_numeric($v)) {
$vars[$v] = is_scalar($newVal) ? $newVal : new self($newVal);
}
else {
$vars = is_scalar($newVal) ? $newVal : new self($newVal);
}
}
return $vars;
}
return null;
}

# overloading
public function __set($name, $value)
{
Expand All @@ -95,7 +103,8 @@ public function &__get($name)
{
if (isset($this->$name)) {
return $this->$name;
} else {
}
else {
if (count($this->_data)) throw new \Exception('Property is array');
$this->$name = new self();
return $this->$name;
Expand All @@ -113,40 +122,42 @@ public function __unset($name)
}

# Countable
public function count()
public function count(): int
{
return count($this->_data);
}

# ArrayAccess
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
if (count(get_object_vars($this)) > 1) throw new \Exception('Property is not array');
if (is_null($offset)) {
$this->_data[] = $value;
} else {
}
else {
$this->_data[$offset] = $value;
}
}

public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->_data[$offset]);
}

public function offsetUnset($offset)
public function offsetUnset($offset): void
{
if (isset($this->_data[$offset])) {
unset($this->_data[$offset]);
}
}

public function &offsetGet($offset)
public function &offsetGet($offset): mixed
{
if (count(get_object_vars($this)) > 1) throw new \Exception('Property is not array');
if (isset($this->_data[$offset])) {
return $this->_data[$offset];
} else {
}
else {
$this->_data[$offset] = new self();
return $this->_data[$offset];
}
Expand All @@ -156,7 +167,8 @@ public function toArray()
{
if (count($this->_data)) {
return $this->processArray($this->_data);
} else {
}
else {
$data = get_object_vars($this);
unset($data['_data']);
return $this->processArray($data);
Expand All @@ -168,7 +180,8 @@ private function processArray($array)
foreach ($array as $key => $value) {
if (is_object($value)) {
$array[$key] = $value->toArray();
} else {
}
else {
$array[$key] = $value;
}
}
Expand All @@ -177,7 +190,7 @@ private function processArray($array)

public function toPrettyJson()
{
return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}

public function __toString()
Expand All @@ -186,13 +199,13 @@ public function __toString()
}

# IteratorAggregate
function getIterator()
function getIterator(): \Iterator
{
if (count($this->_data)) {
return new \ArrayIterator($this->_data);
} else {
}
else {
return new \ArrayIterator($this);
}
}
}
?>
}

0 comments on commit 87b396d

Please sign in to comment.