/** * Test find max */ public function testFindMax() { $dates = $this->dates(); $max = (new \DateTime('@' . strtotime('+1 day')))->format('Y-m-d'); $this->assertEquals($max, DateTime::max($dates)->format('Y-m-d')); $this->assertEquals($max, DateTime::max(new ArrayIterator($dates))->format('Y-m-d')); $this->assertEquals($max, call_user_func_array('Zork\\Stdlib\\DateTime::max', $dates)->format('Y-m-d')); }
/** * Input date * * @param string $date * @param string $format * @return \DateTime */ protected function inputDate($date, $format = null) { if (empty($date)) { $date = null; } if (!$date instanceof DateTime) { if ($date instanceof BaseDateTime) { $date = DateTime::createFromFormat(DateTime::ISO8601, $date->format(DateTime::ISO8601)); } if (is_int($date)) { $date = new DateTime('@' . $date); } else { if (empty($format)) { $date = new DateTime($date); } else { $date = DateTime::createFromFormat($format, $date); } } } return $date; }
/** * Create a DateTime object from various formats * * @param DateTime|\DateTime|string|int|null $dateTime * @param bool $nullOnEmpty * @return DateTime|null */ public static function create($dateTime, $nullOnEmpty = false) { if ($nullOnEmpty && empty($dateTime)) { return null; } else { if ($dateTime instanceof \DateTime) { return new static($dateTime->format(static::CONVERT)); } else { if (is_numeric($dateTime)) { return new static('@' . $dateTime); } } } return new static((string) $dateTime); }
/** * Preview a css */ public function previewAction() { /* @var $form \Zork\Form\Form */ /* @var $model \Grid\Customize\Model\Sheet\Model */ $params = $this->params(); $request = $this->getRequest(); $locator = $this->getServiceLocator(); $id = $params->fromRoute('id'); $rootId = is_numeric($id) ? (int) $id : null; $model = $locator->get('Grid\\Customize\\Model\\Sheet\\Model'); $form = $locator->get('Form')->get('Grid\\Customize\\Css'); $sheet = $model->createEmpty($rootId); $form->setHydrator($model->getMapper())->bind($sheet); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $this->messenger()->add('customize.preview.success', 'customize', Message::LEVEL_INFO); $id = $rootId === null ? 'global' : $rootId; $prefix = 'public'; $file = sprintf(static::PREVIEW_FILE, $id) . '.'; do { $suffix = new DateTime(); } while (file_exists($prefix . $file . $suffix->toHash() . static::PREVIEW_EXTENSION)); $url = $file . $suffix->toHash() . static::PREVIEW_EXTENSION; $path = $prefix . $url; $sheet->render($path); $locator->get('Grid\\Customize\\Service\\CssPreview')->setPreviewById($rootId, $url); if (null === $rootId) { return $this->redirect()->toUrl('/'); } else { return $this->redirect()->toRoute('Grid\\Paragraph\\Render\\Paragraph', array('locale' => (string) $this->locale(), 'paragraphId' => $rootId)); } } else { $this->messenger()->add('customize.preview.failed', 'customize', Message::LEVEL_ERROR); return $this->redirect()->toRoute('Grid\\Customize\\CssAdmin\\List', array('locale' => (string) $this->locale())); } } }
/** * Set updated * * @param null|int|string|\DateTime|\Zork\Stdlib\DateTime $updated * @return \Customize\Model\Rule\Structure */ public function setUpdated($updated) { $this->updated = DateTime::create($updated, true); return $this; }
/** * Get published date * * @return \DateTime */ public function getRenderedPublished() { $rendered = $this->getRenderedContent(); if ($rendered instanceof Content) { return DateTime::max($rendered->publishedFrom, $rendered->created); } return null; }
/** * Find updated times * * @param array|int $rootParagraphIds * @param bool|null $global * @return DateTime[] */ public function findUpdated($rootParagraphIds, $global = null) { if ($rootParagraphIds instanceof Traversable) { $rootParagraphIds = ArrayUtils::iteratorToArray($rootParagraphIds); } else { if (!is_array($rootParagraphIds)) { $rootParagraphIds = (array) $rootParagraphIds; } } if (null === $global && in_array(null, $rootParagraphIds)) { $global = true; } if (empty($rootParagraphIds)) { return array(); } $rootParagraphIds = array_filter($rootParagraphIds); if ($global) { if (empty($rootParagraphIds)) { $where = array(new Predicate\IsNull('rootParagraphId')); } else { $where = array(new Predicate\PredicateSet(array(new Predicate\IsNull('rootParagraphId'), new Predicate\In('rootParagraphId', $rootParagraphIds)), Predicate\PredicateSet::COMBINED_BY_OR)); } } else { $where = array('rootParagraphId' => $rootParagraphIds); } $select = $this->sql()->select()->columns(array('rootParagraphId', 'updated'))->where($where)->order(array(new Expression('COALESCE( ?, 0 ) ASC', array('rootParagraphId'), array(Expression::TYPE_IDENTIFIER)))); /* @var $result \Zend\Db\Adapter\Driver\ResultInterface */ $result = $this->sql()->prepareStatementForSqlObject($select)->execute(); $affected = $result->getAffectedRows(); if ($affected < 1) { return array(); } $updated = array(); foreach ($result as $row) { $updated[$row['rootParagraphId']] = DateTime::create($row['updated']); } return $updated; }
/** * Convert to date-time * * @param int|string|\DateTime|\Zork\Stdlib\DateTime $time * @return \Zork\Stdlib\DateTime */ protected function convertToDateTime($time) { if ($time instanceof DateTime) { return clone $time; } else { if ($time instanceof \DateTime) { return new DateTime($time->format(\DateTime::ISO8601)); } else { if (is_numeric($time)) { return new DateTime('@' . $time); } } } return new DateTime((string) $time); }