public function set($section, $path = '.', $searchQuery = null, $value = null, $decodeValue = true) { $dataAll = $this->read(); $storeData = json_encode($dataAll); $store = new Xapp_Util_Json_Query($storeData); $value = is_string($value) ? json_decode($value) : $value; $stdQuery = null; $success = false; if ($searchQuery) { //1. convert string query into an query map $stdQuery = $this->toStdQuery(is_string($searchQuery) ? json_decode($searchQuery, true) : $searchQuery); //2. prepare sanity test $queryResult = $store->query($path, $stdQuery)->get(); //3. did find something ? if ($queryResult != null && count($queryResult) == 1) { //4. now construct the real store $store = Xapp_Util_Json_Store::create($storeData); //5. re-run the query $a = $store->query($path, $stdQuery); //merge new value into query result $newValue = $this->_merge($queryResult[0], $value); //update store $a->set(null, $newValue); $success = true; } else { return false; } } if ($success) { $result = $store->object(); $this->write(json_encode($result)); return $result; } return null; }
function xapp_object_find($object, $path, $query, $toArray = true) { error_reporting(E_ERROR); ini_set('display_errors', '0'); # don't show any errors... $queryObject = null; if (is_string($object)) { $queryObject = $object; } else { $queryObject = json_encode($object); } $q = new Xapp_Util_Json_Query($queryObject); $res = $q->query($path, $query); ini_set('display_errors', '1'); # revert error_reporting(E_ALL); return $res->get(); }
/** * query the object with path and query parameters from Xapp_Util_Json_Query::find method and use store * methods on search result, e.g. * <code> * $store = Xapp_Util_Json_Store::create($object) ->query('/firstElement', array("id=1000")) ->query('.', array('title=foo')) ->set(null, 1); * </code> * * the query method must be terminated with a store method! * * @see Xapp_Util_Std_Query::find * @error 17005 * @param string $path expects the optional search path as outlined above * @param array $query expects the optional query filter chain as explained above * @param null|string $result_flag expects optional result flag "first" or "last" * @return $this */ public function query($path, array $query = null, $result_flag = 'first') { $this->_result =& Xapp_Util_Json_Query::find($this->_object, $path, $query, $result_flag); return $this; }
public function set($section, $path = '.', $searchQuery = null, $value = null, $decodeValue = true) { $dataAll = $this->read(); $userData = null; if ($dataAll) { $primKey = xapp_get_option(self::PRIMARY_KEY, $this); if (!property_exists($dataAll, $primKey)) { $dataAll->{$primKey} = new stdClass(); } $userData = $dataAll->{$primKey}; } if (!property_exists($userData, $section)) { $userData->{$section} = array(); } $storeData = json_encode($userData); $store = new Xapp_Util_Json_Query($storeData); $value = is_string($value) ? json_decode($value) : $value; if ($decodeValue === false && (is_array($value) || is_object($value))) { $value = json_encode($value, true); } if (!$value) { return false; } $success = false; $stdQuery = null; if ($searchQuery) { $stdQuery = $this->toStdQuery(is_string($searchQuery) ? json_decode($searchQuery, true) : $searchQuery); $queryResult = $store->query($path, $stdQuery)->get(); if ($queryResult != null && count($queryResult) == 1) { $queryResult[0]->{xapp_get_option(self::DATA_PROPERTY, $this)} = $value; $success = true; } else { return false; } } else { if (is_array($userData->{$section})) { $found = null; $dataProp = xapp_get_option(self::DATA_PROPERTY, $this); foreach ($userData->{$section} as $item) { if (property_exists($item, xapp_get_option(self::ID_PROPERTY, $this))) { $found = $item; break; } } $value = (object) $value; if (is_string($value->{$dataProp})) { $value->{$dataProp} = json_decode($value->{$dataProp}); } if ($found) { $userData->{$section}[0]->{$dataProp} = $value->{$dataProp}; } else { $userData->{$section}[] = $value; //implicit } $dataAll->{$primKey} = $userData; $this->write(json_encode($dataAll)); } } if ($success) { $dataAll->{$primKey} = $store->getObject(); $this->write(json_encode($dataAll)); return true; } return false; }
public function __construct($json) { parent::__construct($json); }