public function __construct($name = null, $format = 'Y-m-d H:i:s') { $validator = new Validator\Date(array('format' => $format)); $validator->setMessages(array(Validator\Date::INVALID => "Неверный тип поля. Значение может быть числовым, строковым или датой", Validator\Date::INVALID_DATE => "Некорректное значение даты", Validator\Date::FALSEFORMAT => "Неверный формат даты")); $this->getValidatorChain()->attach($validator); parent::__construct($name); }
/** * Ensures that the validator can handle different manual dateformats * * @group ZF-2003 * @return void */ public function testUseManualFormat() { $this->assertTrue($this->validator->setFormat('d.m.Y')->isValid('10.01.2008'), var_export(date_get_last_errors(), 1)); $this->assertEquals('d.m.Y', $this->validator->getFormat()); $this->assertTrue($this->validator->setFormat('m Y')->isValid('01 2010')); $this->assertFalse($this->validator->setFormat('d/m/Y')->isValid('2008/10/22')); $this->assertTrue($this->validator->setFormat('d/m/Y')->isValid('22/10/08')); $this->assertFalse($this->validator->setFormat('d/m/Y')->isValid('22/10')); // Omitting the following assertion, as it varies from 5.3.3 to 5.3.11, // and there is no indication in the PHP changelog as to when or why it // may have changed. Leaving for posterity, to indicate original expectation. // $this->assertFalse($this->validator->setFormat('s')->isValid(0)); }
/** * {@inheritDoc} */ public function isValid($value) { if (!$value) { return true; } return parent::isValid($value); }
/** * {@inheritDoc} */ public function isValid($value) { if (null === $value && $this->isAllowNull()) { return true; } return parent::isValid($value); }
/** * @param $apartmentGroupId * @param $from * @param $to * @param int $roomCount * @param $sort * @param $roomType * @return array */ public function composeGroupAvailabilityForDateRange($apartmentGroupId, $from, $to, $roomCount = -1, $sort, $roomType) { /** * @var \DDD\Domain\Apartment\Inventory\GroupInventory[] $resultSet * @var \DDD\Dao\Booking\Booking $reservationDao */ $dateValidator = new Date(['format' => 'Y-m-d']); if ($dateValidator->isValid($from) && $dateValidator->isValid($to)) { $apartmentService = $this->getServiceLocator()->get('service_apartment_general'); $apartmentDao = new \DDD\Dao\Apartment\General($this->getServiceLocator(), 'DDD\\Domain\\Apartment\\Inventory\\GroupInventory'); $reservationDao = $this->getServiceLocator()->get('dao_booking_booking'); $overbookings = $reservationDao->getApartmentGroupOverbookingsForDateRange($apartmentGroupId, $from, $to, $roomCount, $roomType); $result = []; $dates = []; $existingDates = []; $resultSet = $apartmentDao->getGroupAvailabilityForDateRange($apartmentGroupId, $from, $to, $roomCount, $sort, $roomType); foreach ($resultSet as $row) { $apartmentId = $row->getId(); $date = $row->getDate(); if (!in_array($date, $existingDates)) { array_push($existingDates, $date); array_push($dates, ['raw' => $date, 'dayOfWeek' => date('D', strtotime($date)), 'day' => date('j', strtotime($date)), 'month' => date('n', strtotime($date))]); } if (isset($result[$apartmentId])) { $result[$apartmentId][$date] = ['av' => $row->getAvailability(), 'reservation_data' => $row->getReservationData()]; } else { $balcony = !is_null($row->getAmenityId()) ? ' (B)' : ''; $bedroom = $row->getBedroom_count() . $balcony; $result[$apartmentId] = ['id' => $row->getId(), 'name' => $row->getName(), 'floor' => $row->getFloor(), 'block' => $row->getBlock(), 'bedroom' => $bedroom, 'bathroom' => $row->getBathroom_count(), 'max_capacity' => $row->getMax_capacity(), 'unit_number' => $row->getUnit_number(), 'building_name' => $row->getBuildingName(), 'links' => $apartmentService->getWebsiteLink($row->getId()), $row->getDate() => ['av' => $row->getAvailability(), 'reservation_data' => $row->getReservationData()]]; } } if (!empty($result)) { $result = array_values($result); } sort($dates); return ['list' => $result, 'days' => $dates, 'overbookings' => $overbookings]; } else { return ['list' => [], 'days' => [], 'overbookings' => []]; } }
/** * Returns true if a date is within a valid step * * @param string|int|\DateTime $value * @return bool * @throws Exception\InvalidArgumentException */ public function isValid($value) { if (!parent::isValid($value)) { return false; } $valueDate = $this->convertToDateTime($value, false); // avoid duplicate errors $baseDate = $this->convertToDateTime($this->baseValue, false); $step = $this->getStep(); // Same date? if ($valueDate == $baseDate) { return true; } // Optimization for simple intervals. // Handle intervals of just one date or time unit. $intervalParts = explode('|', $step->format('%y|%m|%d|%h|%i|%s')); $partCounts = array_count_values($intervalParts); $unitKeys = array('years', 'months', 'days', 'hours', 'minutes', 'seconds'); $intervalParts = array_combine($unitKeys, $intervalParts); // Get absolute time difference $timeDiff = $valueDate->diff($baseDate, true); $diffParts = array_combine($unitKeys, explode('|', $timeDiff->format('%y|%m|%d|%h|%i|%s'))); if (5 === $partCounts["0"]) { // Find the unit with the non-zero interval $intervalUnit = null; $stepValue = null; foreach ($intervalParts as $key => $value) { if (0 != $value) { $intervalUnit = $key; $stepValue = (int) $value; break; } } // Check date units if (in_array($intervalUnit, array('years', 'months', 'days'))) { switch ($intervalUnit) { case 'years': if (0 == $diffParts['months'] && 0 == $diffParts['days'] && 0 == $diffParts['hours'] && 0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { if ($diffParts['years'] % $stepValue === 0) { return true; } } break; case 'months': if (0 == $diffParts['days'] && 0 == $diffParts['hours'] && 0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { $months = $diffParts['years'] * 12 + $diffParts['months']; if ($months % $stepValue === 0) { return true; } } break; case 'days': if (0 == $diffParts['hours'] && 0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { $days = $timeDiff->format('%a'); // Total days if ($days % $stepValue === 0) { return true; } } break; } $this->error(self::NOT_STEP); return false; } // Check time units if (in_array($intervalUnit, array('hours', 'minutes', 'seconds'))) { // Simple test if $stepValue is 1. if (1 == $stepValue) { if ('hours' === $intervalUnit && 0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { return true; } elseif ('minutes' === $intervalUnit && 0 == $diffParts['seconds']) { return true; } elseif ('seconds' === $intervalUnit) { return true; } $this->error(self::NOT_STEP); return false; } // Simple test for same day, when using default baseDate if ($baseDate->format('Y-m-d') == $valueDate->format('Y-m-d') && $baseDate->format('Y-m-d') == '1970-01-01') { switch ($intervalUnit) { case 'hours': if (0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { if ($diffParts['hours'] % $stepValue === 0) { return true; } } break; case 'minutes': if (0 == $diffParts['seconds']) { $minutes = $diffParts['hours'] * 60 + $diffParts['minutes']; if ($minutes % $stepValue === 0) { return true; } } break; case 'seconds': $seconds = $diffParts['hours'] * 60 * 60 + $diffParts['minutes'] * 60 + $diffParts['seconds']; if ($seconds % $stepValue === 0) { return true; } break; } $this->error(self::NOT_STEP); return false; } } } return $this->fallbackIncrementalIterationLogic($baseDate, $valueDate, $intervalParts, $diffParts, $step); }
/** * @ZF-6374 */ public function testUsingApplicationLocale() { \Zend\Registry::set('Zend_Locale', new \Zend\Locale\Locale('de')); $valid = new Validator\Date(); $this->assertTrue($valid->isValid('10.April.2008')); }
/** * Returns true if a date is within a valid step * * @param string|int|\DateTime $value * @return bool * @throws Exception\InvalidArgumentException */ public function isValid($value) { parent::isValid($value); $this->setValue($value); $baseDate = $this->convertToDateTime($this->getBaseValue()); $step = $this->getStep(); // Parse the date try { $valueDate = $this->convertToDateTime($value); } catch (Exception\InvalidArgumentException $ex) { return false; } // Same date? if ($valueDate == $baseDate) { return true; } // Optimization for simple intervals. // Handle intervals of just one date or time unit. $intervalParts = explode('|', $step->format('%y|%m|%d|%h|%i|%s')); $partCounts = array_count_values($intervalParts); if (5 === $partCounts["0"]) { // Find the unit with the non-zero interval $unitKeys = array('years', 'months', 'days', 'hours', 'minutes', 'seconds'); $intervalParts = array_combine($unitKeys, $intervalParts); $intervalUnit = null; $stepValue = null; foreach ($intervalParts as $key => $value) { if (0 != $value) { $intervalUnit = $key; $stepValue = (int) $value; break; } } // Get absolute time difference $timeDiff = $valueDate->diff($baseDate, true); $diffParts = explode('|', $timeDiff->format('%y|%m|%d|%h|%i|%s')); $diffParts = array_combine($unitKeys, $diffParts); // Check date units if (in_array($intervalUnit, array('years', 'months', 'days'))) { switch ($intervalUnit) { case 'years': if (0 == $diffParts['months'] && 0 == $diffParts['days'] && 0 == $diffParts['hours'] && 0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { if ($diffParts['years'] % $stepValue === 0) { return true; } } break; case 'months': if (0 == $diffParts['days'] && 0 == $diffParts['hours'] && 0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { $months = $diffParts['years'] * 12 + $diffParts['months']; if ($months % $stepValue === 0) { return true; } } break; case 'days': if (0 == $diffParts['hours'] && 0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { $days = $timeDiff->format('%a'); // Total days if ($days % $stepValue === 0) { return true; } } break; } $this->error(self::NOT_STEP); return false; } // Check time units if (in_array($intervalUnit, array('hours', 'minutes', 'seconds'))) { // Simple test if $stepValue is 1. if (1 == $stepValue) { if ('hours' === $intervalUnit && 0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { return true; } elseif ('minutes' === $intervalUnit && 0 == $diffParts['seconds']) { return true; } elseif ('seconds' === $intervalUnit) { return true; } } // Simple test for same day, when using default baseDate if ($baseDate->format('Y-m-d') == $valueDate->format('Y-m-d') && $baseDate->format('Y-m-d') == '1970-01-01') { switch ($intervalUnit) { case 'hours': if (0 == $diffParts['minutes'] && 0 == $diffParts['seconds']) { if ($diffParts['hours'] % $stepValue === 0) { return true; } } break; case 'minutes': if (0 == $diffParts['seconds']) { $minutes = $diffParts['hours'] * 60 + $diffParts['minutes']; if ($minutes % $stepValue === 0) { return true; } } break; case 'seconds': $seconds = $diffParts['hours'] * 60 + $diffParts['minutes'] * 60 + $diffParts['seconds']; if ($seconds % $stepValue === 0) { return true; } break; } $this->error(self::NOT_STEP); return false; } } } // Fall back to slower (but accurate) method for complex intervals. // Keep adding steps to the base date until a match is found // or until the value is exceeded. if ($baseDate < $valueDate) { while ($baseDate < $valueDate) { $baseDate->add($step); if ($baseDate == $valueDate) { return true; } } } else { while ($baseDate > $valueDate) { $baseDate->sub($step); if ($baseDate == $valueDate) { return true; } } } $this->error(self::NOT_STEP); return false; }
/** * Validate a value * @param string|array|int|DateTime $value * @return bool */ public function isValid($value) { /// valid date $parentTest = parent::isValid($value); if (!$parentTest) { $this->error(self::FALSEFORMAT); return false; } // test if is later // timezone if (!$this->timezone != "") { $timezone = new \DateTimeZone($this->timezone); $dateA = DateTime::createFromFormat($this->format, $this->max, $timezone); $dateB = DateTime::createFromFormat($this->format, $this->value, $timezone); } else { $dateA = DateTime::createFromFormat($this->format, $this->max); $dateB = DateTime::createFromFormat($this->format, $this->value); } if ($dateA > $dateB) { return true; } else { $this->error(self::DATE_NOT_EARLIER); return false; } }
public function getDateValidator() { if ($this->dateValidator === NULL) { $validator = new DateValidator(); $validator->setOptions($this->getDateConfig())->setMessages(array(DateValidator::INVALID => $this->getErrorMessage('DATE::INVALID'), DateValidator::INVALID_DATE => $this->getErrorMessage('DATE::INVALID_DATE'), DateValidator::FALSEFORMAT => $this->getErrorMessage('DATE::FALSEFORMAT'))); $this->setDateValidator($validator); } return $this->dateValidator; }
/** * @param string $format * @param string $message * @return Date */ public static function dateValidator($format, $message = null) { $validator = new Date(compact('format')); if ($message) { $validator->setMessage($message); } else { $validator->setMessage(sprintf('Format tanggal harus berupa %s. Contoh "%s"', $format, date($format))); } return $validator; }
/** * updates the Birthday * @param string $birthday * @return boolean */ public function updateBirthday($birthday) { $date = new \DateTime($birthday); $val = new Date(); $val->setFormat('d.m.Y'); $dbUser = $this->getUserMapper()->findUserDataById($this->getIdentity()->getUserId()); $dbUser->setBirth($date); $result = $this->getUserMapper()->saveData($dbUser); if ($result == null) { return true; } return false; }