Exemplo n.º 1
0
 /**
  * Count elements of an object
  *
  * Rewinding version of count 
  *
  * @return int
  */
 public function count()
 {
     if ($this->_iterator instanceof \Countable) {
         return $this->_iterator->count();
     }
     $count = iterator_count($this->_iterator);
     $this->_iterator->rewind();
     return $count;
 }
Exemplo n.º 2
0
 /**
  * lista wplat, uzywana takze do symulacji
  * 
  * @param Iterator $iterator
  * @return Payments
  */
 public function getPayments(Iterator $iterator)
 {
     $payments = new Payments();
     $amount = $this->transaction->getAmount();
     $overpayment = function ($amount) use($payments) {
         $payments->append(new Payment(array('amount' => $amount, 'type' => Payment::TYPE_OVERPAYMENT)));
     };
     if ($iterator->count() == 0) {
         /* jezeli brak dokumentow jedna wplata z nadp³at¹ */
         $overpayment($amount);
         return $payments;
     }
     /* rozlicza kwote transakcji na dokumenty ksiegowe */
     foreach ($iterator as $document) {
         /* @var $document SettlementInterface */
         if ($document instanceof Wrapper) {
             $due = $document->getPaymentAmount();
             /* kwota edytowana */
             $document = $document->getDocument();
             /* ograniczenia dla recznego rozliczenia */
             if ($due == 0 || $due > $document->getDocumentAmount()) {
                 $due = $document->getDocumentAmount();
             }
         } else {
             $due = $document->getDocumentAmount();
             /* kwota z dokumentu Do_zaplaty/kwota_raty... */
         }
         if ($amount == 0) {
             break;
         }
         $money = 0;
         if ($amount > floatval($due)) {
             $money = round(floatval($due), 2);
             $amount -= $money;
             $amount = round($amount, 2);
         } else {
             $money = $amount;
             $amount = 0;
         }
         $payments->append((new Payment(array('amount' => $amount, 'type' => Payment::TYPE_TRANSFER)))->setSettlementDocument($document));
     }
     if ($amount > 0) {
         /* jezeli jeszcze zostalo cos kasy (suma kwot faktur jest mniejsza niz kwota transakcji) to wstawia nadplate */
         $overpayment($amount);
     }
     return $payments;
 }
Exemplo n.º 3
0
 /**
  * @param \Iterator $it
  * @param mixed $first
  * @param mixed $last
  * @param int $count
  * @param callable $predicate
  *
  * @dataProvider dataProvider
  */
 public function testCount(\Iterator $it, $first, $last, $count, callable $predicate = null)
 {
     $this->assertEquals($count, $it->count($predicate));
 }
Exemplo n.º 4
0
 /**
  * The main method for calling the search method in the spotify web API.
  * @param string $query
  * @param array $options
  * @link https://developer.spotify.com/technologies/web-api/search/
  * 
  */
 function search($query, $options)
 {
     if ($query == '') {
         throw new \Exception();
     }
     if (!isset($options['type'])) {
         $options['type'] = 'track';
     }
     if (!array_key_exists($options['type'], self::$_SPOTIFY_SEARCH_METHODS)) {
         // TODO: error
     }
     // TODO ex:
     $this->base_uri = self::$_SPOTIFY_SEARCH_ENDPOINT . strtolower($options['type']) . '.json';
     // if page is set, add that as an additional parameter
     $this->query_string = '?q=' . json_encode($query) . (isset($options['page']) ? '&page=' . Constants::PAGER_MAX_PAGES * $options['page'] : '');
     $response = $this->_call_spotify_api();
     $data = new \cecilia\model\SpotifyResult($response);
     if ($data->cursor->has_next) {
         $threshold = $data->cursor->total < CECILIA_PAGER_MAX_ITEMS ? $data->cursor->total : CECILIA_PAGER_MAX_ITEMS;
         $offset = Constants::SPOTIFY_RESULTS_PER_PAGE;
         $i = 0;
         $tmp_array = array();
         while ($offset < $threshold) {
             if ($i == 0) {
                 $p = $data->cursor->page + 1;
                 $this->query_string = '?q=' . json_encode($query) . '&page=' . $p;
                 $offset = $offset * 2;
                 $p++;
             } else {
                 if ($offset < $threshold) {
                     $this->query_string = '?q=' . json_encode($query) . '&page=' . $p;
                     $offset = $p * Constants::SPOTIFY_RESULTS_PER_PAGE;
                     $p++;
                 }
             }
             $tmp = new \cecilia\model\SpotifyResult($this->_call_spotify_api());
             $tmp_data = $tmp->data;
             $tmp_array = array_merge($tmp_data, $tmp_array);
             $i++;
             if ($i > 4) {
                 die(var_dump($tmp_array));
             }
         }
         $pager = new Pager($tmp);
     } else {
         $pager = new Pager($data);
     }
     if (isset($tmp_array)) {
         $data->data = array_merge($data->data, $tmp_array);
     }
     // now, we have the first page of results.  from this we can move forward with putting together the aggregated result set for caching.
     $parsed = array();
     $class_name = '\\cecilia\\model\\' . ucfirst($data->type);
     foreach ($data->data as $k => $v) {
         try {
             $parsed[] = new $class_name($v);
         } catch (\Exception $e) {
         }
     }
     $it = new Iterator($parsed);
     // if the filter option is set, filter the result.
     $filtered = new Filter($it);
     return $it->count() > 0 ? new Response(1, $parsed, $pager) : new Response(0, $parsed, $pager);
 }
 /**
  * {@inheritdoc}
  */
 public function count()
 {
     $this->initialize();
     return $this->documents->count();
 }