RockLobsterInc\Swv\Rules

FileRule::validate │ public │ CF7 1.0

Validates the form data according to the logic defined by this rule.

Метод класса: FileRule{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$FileRule = new FileRule();
$FileRule->validate( $form_data, $context );
$form_data(FormDataTree) (обязательный)
Form data.
$context(массив)
Optional context.
По умолчанию: []

Код FileRule::validate() CF7 6.2.4.1

public function validate(FormDataTree $form_data, array $context = [])
{
    $files = $form_data->getAllFiles($this->field);
    $files = array_flatten($files);

    if (empty($files)) {
        return true;
    }

    $acceptable_filetypes = [];

    foreach ($this->accept as $accept) {
        if (preg_match('/^\.[a-z0-9]+$/i', $accept)) {
            $acceptable_filetypes[] = $accept;
        } else {
            foreach (self::convertMimeToExt($accept) as $extension) {
                $acceptable_filetypes[] = sprintf(
                    ".%s",
                    trim($extension, " ."),
                );
            }
        }
    }

    $acceptable_filetypes = array_map("strtolower", $acceptable_filetypes);
    $acceptable_filetypes = array_unique($acceptable_filetypes);

    foreach ($files as $file) {
        $file_name = $file->name();

        $last_period_pos = strrpos($file_name, ".");

        if (false === $last_period_pos) {
            // No period.
            throw new Invalidity($this, ["cause" => $file]);
        }

        $suffix = strtolower(substr($file_name, $last_period_pos));

        if (!in_array($suffix, $acceptable_filetypes, true)) {
            throw new Invalidity($this, ["cause" => $file]);
        }
    }

    return true;
}