Example #1
0
 /**
  * @return Timestamp
  * Emulates PostgreSQL's date_trunc() function
  **/
 public function truncate(Date $time, $ceil = false)
 {
     $time = $time->toTimestamp();
     $function = $ceil ? 'ceil' : 'floor';
     if ($this->seconds) {
         if ($this->seconds < 1) {
             return $time->spawn();
         }
         $truncated = (int) ($function($time->toStamp() / $this->seconds) * $this->seconds);
         return Timestamp::create($truncated);
     } elseif ($this->days) {
         $epochStartTruncated = Date::create('1970-01-05');
         $truncatedDate = Date::create($time->toDate());
         if ($ceil && $truncatedDate->toStamp() < $time->toStamp()) {
             $truncatedDate->modify('+1 day');
         }
         $difference = Date::dayDifference($epochStartTruncated, $truncatedDate);
         $truncated = (int) ($function($difference / $this->days) * $this->days);
         return Timestamp::create($epochStartTruncated->spawn($truncated . ' days')->toStamp());
     } elseif ($this->months) {
         $monthsCount = $time->getYear() * 12 + ($time->getMonth() - 1);
         if ($ceil && $time->getDay() - 1 + $time->getHour() + $time->getMinute() + $time->getSecond() > 0) {
             $monthsCount += 0.1;
         }
         // delta
         $truncated = (int) ($function($monthsCount / $this->months) * $this->months);
         $months = $truncated % 12;
         $years = ($truncated - $months) / 12;
         Assert::isEqual($years, (int) $years);
         $years = (int) $years;
         $months = $months + 1;
         return Timestamp::create("{$years}-{$months}-01 00:00:00");
     }
     Assert::isUnreachable();
 }
Example #2
0
 public function makeItems(\SimpleXMLElement $xmlFeed)
 {
     $result = array();
     if (isset($xmlFeed->channel->item)) {
         foreach ($xmlFeed->channel->item as $item) {
             $feedItem = FeedItem::create((string) $item->title)->setContent(FeedItemContent::create()->setBody((string) $item->description))->setPublished(Timestamp::create(strtotime((string) $item->pubDate)))->setLink((string) $item->link);
             if (isset($item->guid)) {
                 $feedItem->setId($item->guid);
             }
             if (isset($item->category)) {
                 $feedItem->setCategory((string) $item->category);
             }
             $result[] = $feedItem;
         }
     }
     return $result;
 }
Example #3
0
 public function makeItems(\SimpleXMLElement $xmlFeed)
 {
     $result = [];
     foreach ($xmlFeed->entry as $entry) {
         $feedItem = FeedItem::create((string) $entry->title);
         if (isset($entry->content)) {
             $feedItem->setContent($this->makeFeedItemContent($entry->content));
         }
         if (isset($entry->summary)) {
             $feedItem->setSummary($this->makeFeedItemContent($entry->summary));
         }
         if (isset($entry->id)) {
             $feedItem->setId($entry->id);
         }
         $result[] = $feedItem->setPublished(Timestamp::create(strtotime((string) $entry->updated)))->setLink((string) $entry->link);
     }
     return $result;
 }
 public function makeItems(\SimpleXMLElement $xmlFeed)
 {
     $xmlFeed->registerXPathNamespace(YandexRssFeedFormat::YANDEX_NAMESPACE_PREFIX, YandexRssFeedFormat::YANDEX_NAMESPACE_URI);
     $fullTextList = $xmlFeed->xpath('//' . YandexRssFeedFormat::YANDEX_NAMESPACE_PREFIX . ':full-text');
     $result = array();
     $i = 0;
     if (isset($xmlFeed->channel->item)) {
         foreach ($xmlFeed->channel->item as $item) {
             $feedItem = YandexRssFeedItem::create((string) $item->title)->setContent(FeedItemContent::create()->setBody((string) $item->description))->setPublished(Timestamp::create(strtotime((string) $item->pubDate)))->setFullText((string) $fullTextList[$i++])->setLink((string) $item->link);
             if (isset($item->guid)) {
                 $feedItem->setId($item->guid);
             }
             if (isset($item->category)) {
                 $feedItem->setCategory((string) $item->category);
             }
             $result[] = $feedItem;
         }
     }
     return $result;
 }
Example #5
0
 /**
  * @return Message
  **/
 public function receive($uTimeout = null)
 {
     if (!$this->queue) {
         throw new WrongStateException('you must set the queue first');
     }
     if ($uTimeout && $this->getStream()->isEof()) {
         usleep($uTimeout);
     }
     $string = $this->getStream()->readString();
     if (!$string && $this->getStream()->isEof()) {
         return null;
     }
     $this->getQueue()->setOffset($this->getStream()->getOffset());
     $string = rtrim($string, PHP_EOL);
     $chunks = preg_split("/\t/", $string, 2);
     $time = isset($chunks[0]) ? $chunks[0] : null;
     $text = isset($chunks[1]) ? $chunks[1] : null;
     Assert::isNotNull($time);
     $result = TextMessage::create(Timestamp::create($time))->setText($text);
     return $result;
 }
Example #6
0
 public static function getDateAsText(Timestamp $date, $todayWordNeed = true)
 {
     $dayStart = Timestamp::makeToday();
     $tomorrowDayStart = $dayStart->spawn('+1 day');
     if (Timestamp::compare($date, $dayStart) == 1 && Timestamp::compare($date, $tomorrowDayStart) == -1) {
         return ($todayWordNeed === true ? 'сегодня ' : null) . 'в ' . date('G:i', $date->toStamp());
     }
     $yesterdayStart = $dayStart->spawn('-1 day');
     if (Timestamp::compare($date, $yesterdayStart) == 1 && Timestamp::compare($date, $dayStart) == -1) {
         return 'вчера в ' . date('G:i', $date->toStamp());
     }
     return date('j.m.Y в G:i', $date->toStamp());
 }
Example #7
0
 /**
  * @return DateRange
  **/
 public function safeSetEnd($end)
 {
     if (!$this->getStart() || Timestamp::compare($end, $this->getStart()) > 0) {
         $this->setEnd($end);
     } elseif ($this->getStart()) {
         $this->setEnd($this->getStart());
     }
     return $this;
 }
Example #8
0
 public function equals(Timestamp $timestamp)
 {
     return $this->toDateTime() === $timestamp->toDateTime();
 }
Example #9
0
 /**
  * "associate" mode request
  *
  * @param HttpUrl                          $server  to make association with (usually obtained from OpenIdCredentials)
  * @param OpenIdConsumerAssociationManager $manager - dao-like association manager
  *
  * @return OpenIdConsumerAssociation
  * @throws OpenIdException
  **/
 public function associate(HttpUrl $server, OpenIdConsumerAssociationManager $manager)
 {
     Assert::isTrue($server->isValid());
     if ($association = $manager->findByServer($server)) {
         return $association;
     }
     $dhParameters = new DiffieHellmanParameters($this->numberFactory->makeNumber(self::DIFFIE_HELLMAN_G), $this->numberFactory->makeNumber(self::DIFFIE_HELLMAN_P));
     $keyPair = DiffieHellmanKeyPair::generate($dhParameters, $this->randomSource);
     $request = HttpRequest::create()->setMethod(HttpMethod::post())->setUrl($server)->setPostVar('openid.ns', self::NAMESPACE_2_0)->setPostVar('openid.mode', 'associate')->setPostVar('openid.assoc_type', self::ASSOCIATION_TYPE)->setPostVar('openid.session_type', 'DH-SHA1')->setPostVar('openid.dh_modulus', base64_encode($dhParameters->getModulus()->toBinary()))->setPostVar('openid.dh_gen', base64_encode($dhParameters->getGen()->toBinary()))->setPostVar('openid.dh_consumer_public', base64_encode($keyPair->getPublic()->toBinary()));
     $response = $this->httpClient->setFollowLocation(true)->send($request);
     if ($response->getStatus()->getId() != HttpStatus::CODE_200) {
         throw new OpenIdException('bad response code from server');
     }
     $result = $this->parseKeyValueFormat($response->getBody());
     if (empty($result['assoc_handle'])) {
         throw new OpenIdException('can\\t live without handle');
     }
     if (!isset($result['assoc_type']) || $result['assoc_type'] !== self::ASSOCIATION_TYPE) {
         throw new OpenIdException('bad association type');
     }
     if (!isset($result['expires_in']) || !is_numeric($result['expires_in'])) {
         throw new OpenIdException('bad expires');
     }
     if (isset($result['session_type']) && $result['session_type'] == 'DH-SHA1' && isset($result['dh_server_public'])) {
         $secret = sha1($keyPair->makeSharedKey($this->numberFactory->makeFromBinary(base64_decode($result['dh_server_public'])))->toBinary(), true) ^ base64_decode($result['enc_mac_key']);
     } elseif (empty($result['session_type']) && isset($result['mac_key'])) {
         $secret = base64_decode($result['mac_key']);
     } else {
         throw new OpenIdException('no secret in answer');
     }
     return $manager->makeAndSave($result['assoc_handle'], $result['assoc_type'], $secret, Timestamp::makeNow()->modify('+ ' . $result['expires_in'] . ' seconds'), $server);
 }
Example #10
0
 /**
  * @return Timestamp
  **/
 public function toTimestamp()
 {
     return Timestamp::create($this->toStamp());
 }
Example #11
0
 /**
  * @param Model $model
  * @return $this
  */
 public function render(Model $model = null)
 {
     $csv = $this->toString($model);
     /** @var Model $model */
     if ($this->download) {
         if (is_string($this->download)) {
             $filename = $this->download;
         } else {
             $filename = 'csv_' . Timestamp::makeNow()->toFormatString('Y.m.d_H.i.s') . '.csv';
         }
         header('Content-Type: application/octet-stream; charset=' . $this->encoding);
         header('Content-Length: ' . strlen($csv));
         header('Content-Disposition: attachment;filename="' . $filename . '"');
         header('Content-Transfer-Encoding: binary');
         header('Expires: 0');
         header('Cache-Control: must-revalidate');
     } else {
         header('Content-Type: text/plain; charset=' . ($this->encoding ?: 'utf-8'));
     }
     echo $csv;
     return $this;
 }
Example #12
0
 public function __construct()
 {
     $this->date = Timestamp::makeNow();
 }
Example #13
0
 /**
  * @return Timestamp
  **/
 public static function alignToSeconds(Timestamp $stamp, $seconds)
 {
     $rawStamp = $stamp->toStamp();
     $align = floor($rawStamp / $seconds);
     return Timestamp::create($align * $seconds);
 }
Example #14
0
 /**
  * @return AMQPBaseMessage
  **/
 public function setTimestamp(Timestamp $datetime)
 {
     $this->timestamp = $datetime;
     $this->properties[self::TIMESTAMP] = $datetime->toStamp();
     return $this;
 }
Example #15
0
 public function __construct(Timestamp $timestamp = null)
 {
     $this->timestamp = $timestamp ?: Timestamp::makeNow();
 }
Example #16
0
 public function __clone()
 {
     $vars = get_object_vars($this);
     foreach ($vars as $key => $val) {
         if (is_object($val)) {
             $this->{$key} = clone $val;
         }
     }
     $this->createDate = Timestamp::makeNow();
     $this->cloned = true;
     $this->clonedFrom = $this->id ? $this->id : $this->clonedFrom;
     $this->setId(null);
     $this->removed = false;
 }