/** * Yaml parser * * __DIR__ support: (only available for Yaml files, of course!) * - '__DIR__' will behave as expected * - '___DIR___' (an extra underscore around) will be translated as a literal '__DIR__' * * @param mixed $yaml * @return self */ public static function parse($yaml) { ArgValidator::assert($yaml, 'string'); if (is_file($yaml) && is_readable($yaml)) { $dir = realpath(dirname($yaml)); $yaml = file_get_contents($yaml); $yaml = strtr($yaml, array('___DIR___' => '__DIR__', '__DIR__' => $dir)); } return SymfonyYaml::parse($yaml); }
/** * register a cron job * * @see Cron::trySchedule() for allowed cron expression syntax * * @param string $code the cron job code / identifier * @param string $frequency cron expression * @param callable $callback the actual cron job * @param array $args args to the cron job * @return self */ public static function register($code, $frequency, $callback, array $args = array()) { ArgValidator::assert($code, array('string', 'min' => 1)); ArgValidator::assert($callback, 'callable'); /* * validation of $frequency (cron expression): * will be done together when scheduling * (errors thrown if invalid cron expression) */ $instance = self::getInstance(); $cronRegistry = $instance->getCronRegistry(); $cronRegistry[$code] = array('frequency' => $frequency, 'callback' => $callback, 'args' => $args); $instance->setCronRegistry($cronRegistry); return $instance; }
public static function validateDateTime($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null) { foreach (array('year', 'month', 'day', 'hour', 'minute', 'second') as $var) { ArgValidator::assert(${$var}, array('string', 'int', 'null')); } $daysMap = array(1 => 31, 2 => 29, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31); if (!is_null($year) && !is_null($month) && !is_null($day) && !checkdate($month, $day, $year) || !is_null($year) && !is_null($month) && ($month < 1 || $month > 12) || !is_null($month) && !is_null($day) && ($month < 1 || $month > 12 || $day > $daysMap[(int) $month]) || !is_null($day) && ($day < 1 || $day > 31)) { throw new Exception\InvalidArgumentException(sprintf('invalid date: %s-%s-%s given', $year, $month, $day)); } if (!is_null($hour) && ($hour < 0 || $hour > 23) || !is_null($minute) && ($minute < 0 || $minute > 59) || !is_null($second) && ($second < 0 || $second > 59)) { throw new Exception\InvalidArgumentException(sprintf('invalid time: %s:%s:%s given', $hour, $minute, $second)); } return true; }
/** * normalize vcard source string after parsing * * @param string $vcardStr vcard source string * @return string */ public function normalizeSource($vcardStr) { ArgValidator::assert($vcardStr, 'string'); /* convert chars outside ASCII range to <U+xxxx> expression */ if (preg_match_all('/[^\\x{0000}-\\x{007F}]/u', $vcardStr, $matches)) { $conversion = array(); foreach (array_unique($matches[0]) as $char) { $conversion[$char] = '<U+' . strtoupper(dechex(Utf8::uord($char))) . '>'; } $vcardStr = strtr($vcardStr, $conversion); } /* workaround to enable comma-separated values */ $vcardStr = strtr($vcardStr, array(self::JOINCHAR => ',')); return $vcardStr; }
/** * single instance properties, with datetime / text values * * @param Property $property * @param string $entityClass * @return Entity\* */ public function importSingleDatetime(Property $property, $entityClass) { ArgValidator::assertClass($entityClass); $em = $this->getEm(); $vcard = $this->getVcard(); $dateTimeParser = $this->getDateTimeParser(); $entityArray = explode('\\', $entityClass); $entityName = end($entityArray); //get first instance foreach ($property as $property) { break; } $entity = new $entityClass(); $em->persist($entity); $dateTimeText = new Entity\DateTimeText(); $em->persist($dateTimeText); $entity->setValue($dateTimeText); if ($property['VALUE'] == 'text') { $dateTimeText->setFormat(Repository\DateTimeText::TEXT)->setValueText((string) $property); } else { $dt = $dateTimeParser->parseDateTime((string) $property); if ($timestamp = $dateTimeParser->createTimestamp($dt['year'], $dt['month'], $dt['day'], $dt['hour'], $dt['minute'], $dt['second'], $dt['timezone'])) { $dateTimeText->setFormat(Repository\DateTimeText::FULL)->setValue(\DateTime::createFromFormat('U', $timestamp)); } else { $dateTimeText->setFormat(Repository\DateTimeText::PARTIAL); } $dateTimeText->setYear($dt['year'])->setMonth($dt['month'])->setDay($dt['day'])->setHour($dt['hour'])->setMinute($dt['minute'])->setSecond($dt['second'])->setTimezone($dt['timezone']); } return $entity; }