Beispiel #1
0
 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;
 }