/** * Create a signal collection from JSON string. * * @param string $json * @return GraphCommons\Graph\SignalCollection * @throws GraphCommons\Util\JsonException, \InvalidArgumentException */ public static final function fromJson($json) { $json = new Json($json); if ($json->hasError()) { $jsonError = $json->getError(); throw new JsonException(sprintf('JSON error: code(%d) message(%s)', $jsonError['code'], $jsonError['message']), $jsonError['code']); } $data = $json->decode(true); if (!isset($data['signals'])) { throw new \InvalidArgumentException("'signals' field is required!"); } $array = array(); foreach ($data['signals'] as $i => $signal) { if (!isset($signal['action'])) { throw new \InvalidArgumentException("Signal 'action' and 'parameters' fields are required!"); } $array[$i]['action'] = Signal::detectAction(Util::arrayPick($signal, 'action')); foreach ($signal as $key => $value) { $array[$i]['parameters'][$key] = $value; } } return self::fromArray($array); }
/** * Fill a lib's graph object. * * @param GraphCommons\Graph\Graph $graph * @param array|object $g * @return GraphCommons\Graph\Graph */ private final function fillGraph(Graph $graph, $g) { // force input being an object $g = Util::toObject($g); if (isset($g->id)) { $graph->setId($g->id)->setName($g->name)->setDescription($g->description)->setSubtitle($g->subtitle)->setStatus($g->status)->setCreatedAt($g->created_at); // add signals if exists if (isset($g->signals)) { $array = array(); foreach ($g->signals as $i => $signal) { $action = $signal->action; unset($signal->action); $array[$i]['action'] = Signal::detectAction($action); $array[$i]['parameters'] = Util::toArray($signal); } $graph->setSignals(SignalCollection::fromArray($array)); } } return $graph; }