Exemple #1
0
/**
 * Создаёт объект-дату по строке вида «20131115153657».
 * @param string $timestamp
 * @param string|null $offsetType [optional]
 * @return ZD
 */
function df_date_from_timestamp_14($timestamp, $offsetType = null)
{
    df_assert(ctype_digit($timestamp));
    df_assert_eq(14, strlen($timestamp));
    // Почему-то new Zend_Date($timestamp, 'yMMddHHmmss') у меня не работает
    /** @var string $pattern */
    $pattern = '#(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})#';
    /** @var int[] $matches */
    $matches = [];
    /** @var int $r */
    $r = preg_match($pattern, $timestamp, $matches);
    df_assert_eq(1, $r);
    /** @var int $hour */
    $hour = df_nat0(dfa($matches, 4));
    if ($offsetType) {
        df_assert_in($offsetType, ['UTC', 'GMT']);
        /** @var int $offsetFromGMT */
        $offsetFromGMT = df_round(df_int(df_dts(ZD::now(), ZD::TIMEZONE_SECS)) / 3600);
        $hour += $offsetFromGMT;
        if ('UTC' === $offsetType) {
            $hour++;
        }
    }
    return new ZD(['year' => dfa($matches, 1), 'month' => dfa($matches, 2), 'day' => dfa($matches, 3), 'hour' => $hour, 'minute' => dfa($matches, 5), 'second' => dfa($matches, 6)]);
}
Exemple #2
0
/**
 * @param CX $e
 * @param string $name
 * @param bool $required [optional]
 * @return CX|null
 * @throws E
 */
function df_xml_child(CX $e, $name, $required = false)
{
    /** @var CX[] $childNodes */
    $childNodes = df_xml_children($e, $name, $required);
    /** @var CX|null $result */
    if (is_null($childNodes)) {
        $result = null;
    } else {
        /**
        * Обратите внимание, что если мы имеем структуру:
        			<dictionary>
        				<rule/>
        				<rule/>
        				<rule/>
        			</dictionary>
        * то $this->e()->{'rule'} вернёт не массив, а объект (!),
        * но при этом @see count() для этого объекта работает как для массива (!),
        * то есть реально возвращает количество детей типа rule.
        * Далее, оператор [] также работает, как для массива (!)
        * http://stackoverflow.com/a/16100099
        * Класс \SimpleXMLElement — вообще один из самых необычных классов PHP.
        */
        df_assert_eq(1, count($childNodes));
        $result = $childNodes[0];
        df_assert($result instanceof CX);
    }
    return $result;
}