2021-01-16 20:11:11 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-01-16 20:11:11 +01:00
|
|
|
namespace Rector\CodingStyle\NodeFactory;
|
|
|
|
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use Rector\Core\PhpParser\Node\NodeFactory;
|
|
|
|
/**
|
|
|
|
* Creates + adds
|
|
|
|
*
|
|
|
|
* $jsonData = ['...'];
|
|
|
|
* $json = Nette\Utils\Json::encode($jsonData);
|
|
|
|
*/
|
|
|
|
final class JsonEncodeStaticCallFactory
|
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\NodeFactory
|
2021-01-16 20:11:11 +01:00
|
|
|
*/
|
|
|
|
private $nodeFactory;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\Core\PhpParser\Node\NodeFactory $nodeFactory)
|
2021-01-16 20:11:11 +01:00
|
|
|
{
|
|
|
|
$this->nodeFactory = $nodeFactory;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Creates + adds
|
|
|
|
*
|
|
|
|
* $jsonData = ['...'];
|
|
|
|
* $json = Nette\Utils\Json::encode($jsonData);
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function createFromArray(\PhpParser\Node\Expr $assignExpr, \PhpParser\Node\Expr\Array_ $jsonArray) : \PhpParser\Node\Expr\Assign
|
2021-01-16 20:11:11 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$jsonDataAssign = new \PhpParser\Node\Expr\Assign($assignExpr, $jsonArray);
|
|
|
|
$jsonDataVariable = new \PhpParser\Node\Expr\Variable('jsonData');
|
2021-05-09 20:15:43 +00:00
|
|
|
$jsonDataAssign->expr = $this->nodeFactory->createStaticCall('Nette\\Utils\\Json', 'encode', [$jsonDataVariable]);
|
2021-01-16 20:11:11 +01:00
|
|
|
return $jsonDataAssign;
|
|
|
|
}
|
|
|
|
}
|