get() public method

Get a key from the database.
public get ( string $key ) : mixed
$key string
return mixed
Exemplo n.º 1
0
 public function get($key, callable $callback = null, $default = null)
 {
     $value = $this->cache->get($key);
     if (!isset($value)) {
         $value = $default;
     }
     if (is_callable($callback)) {
         return call_user_func($callback, $value);
     }
     return $value;
 }
Exemplo n.º 2
0
 /**
  * Get reviews from country App Store
  *
  * @param  integer $appId
  * @param  string  $countryCode
  * @param  string  $countryName
  * @return array   list of reviews
  */
 public function getReviewsByCountry($appId, $countryCode, $countryName)
 {
     $reviews = [];
     $promises = [];
     for ($i = 1; $i <= $this->maxPages; $i++) {
         $promises['page' . $i] = $this->client->getAsync("https://itunes.apple.com/{$countryCode}/rss/customerreviews/page={$i}/id={$appId}/sortBy=mostRecent/json");
     }
     try {
         $responses = Promise\unwrap($promises);
         for ($page = 1; $page <= $this->maxPages; $page++) {
             $reviewsData = json_decode((string) $responses['page' . $page]->getBody(), true);
             if (!isset($reviewsData['feed']) || !isset($reviewsData['feed']['entry']) || count($reviewsData['feed']['entry']) == 0) {
                 // Received empty page
                 if ($this->logger) {
                     $this->logger->debug("#{$appId}: Received 0 entries for page {$page} in {$countryName}");
                 }
             } else {
                 if ($this->logger) {
                     $countEntries = count($reviewsData['feed']['entry']) - 1;
                     $this->logger->debug("#{$appId}: Received {$countEntries} entries for page {$page} in {$countryName}");
                 }
                 $applicationData = [];
                 foreach ($reviewsData['feed']['entry'] as $reviewEntry) {
                     if (isset($reviewEntry['im:name']) && isset($reviewEntry['im:image']) && isset($reviewEntry['link'])) {
                         // First element is always an app metadata
                         $applicationData = ['name' => $reviewEntry['im:name']['label'], 'image' => end($reviewEntry['im:image'])['label'], 'link' => $reviewEntry['link']['attributes']['href']];
                         continue;
                     }
                     $reviewId = intval($reviewEntry['id']['label']);
                     if ($this->storage->get("r{$reviewId}")) {
                         continue;
                     }
                     $review = ['id' => $reviewId, 'author' => ['uri' => $reviewEntry['author']['uri']['label'], 'name' => $reviewEntry['author']['name']['label']], 'title' => $reviewEntry['title']['label'], 'content' => $reviewEntry['content']['label'], 'rating' => intval($reviewEntry['im:rating']['label']), 'country' => $countryName, 'application' => array_merge($applicationData, ['version' => $reviewEntry['im:version']['label']])];
                     array_push($reviews, $review);
                 }
             }
         }
     } catch (Exception $e) {
         if ($this->logger) {
             $this->logger->error('Reviewer: exception while getting reviews', ['exception' => $e]);
         }
     }
     return $reviews;
 }
Exemplo n.º 3
0
 protected function runOperationsTests($config)
 {
     $db = new Flintstone('test', $config);
     $arr = array('foo' => "new\nline");
     $this->assertFalse($db->get('foo'));
     $db->set('foo', 1);
     $db->set('name', 'john');
     $db->set('arr', $arr);
     $this->assertEquals(1, $db->get('foo'));
     $this->assertEquals('john', $db->get('name'));
     $this->assertEquals($arr, $db->get('arr'));
     $db->set('foo', 2);
     $this->assertEquals(2, $db->get('foo'));
     $this->assertEquals('john', $db->get('name'));
     $this->assertEquals($arr, $db->get('arr'));
     $db->delete('name');
     $this->assertFalse($db->get('name'));
     $this->assertEquals($arr, $db->get('arr'));
     $keys = $db->getKeys();
     $this->assertEquals(2, count($keys));
     $this->assertEquals('foo', $keys[0]);
     $this->assertEquals('arr', $keys[1]);
     $data = $db->getAll();
     $this->assertEquals(2, count($data));
     $this->assertEquals(2, $data['foo']);
     $this->assertEquals($arr, $data['arr']);
     $db->flush();
     $this->assertFalse($db->get('foo'));
     $this->assertFalse($db->get('arr'));
     $this->assertEquals(0, count($db->getKeys()));
     $this->assertEquals(0, count($db->getAll()));
     unlink($db->getDatabase()->getPath());
 }