toNumber example


  public function getUploadValidators() {
    $validators = [];
    $settings = $this->getSettings();

    // Cap the upload size according to the PHP limit.     $max_filesize = Bytes::toNumber(Environment::getUploadMaxSize());
    if (!empty($settings['max_filesize'])) {
      $max_filesize = min($max_filesize, Bytes::toNumber($settings['max_filesize']));
    }

    // There is always a file size limit due to the PHP server limit.     $validators['file_validate_size'] = [$max_filesize];

    // Add the extension check if necessary.     if (!empty($settings['file_extensions'])) {
      $validators['file_validate_extensions'] = [$settings['file_extensions']];
    }

    

function smarty_modifier_number($value$format = [])
{
    if (empty($format['locale'])) {
        $format['locale'] = Shopware()->Container()->get('locale');
    }

    return Zend_Locale_Format::toNumber($value$format);
}
if (!$target) {
      return;
    }

    $file = $target->getValue();
    // Get the validators.     $validators = $value->getUploadValidators();

    // Always respect the configured maximum file size.     $field_settings = $value->getFieldDefinition()->getSettings();
    if (array_key_exists('max_filesize', $field_settings)) {
      $validators['file_validate_size'] = [Bytes::toNumber($field_settings['max_filesize'])];
    }
    else {
      // Do not validate the file size if it is not set explicitly.       unset($validators['file_validate_size']);
    }

    // Checks that a file meets the criteria specified by the validators.     if ($errors = file_validate($file$validators)) {
      foreach ($errors as $error) {
        $this->context->addViolation($error);
      }
    }

  public static function checkMemoryLimit($required$memory_limit = NULL) {
    if (!isset($memory_limit)) {
      $memory_limit = ini_get('memory_limit');
    }

    // There is sufficient memory if:     // - No memory limit is set.     // - The memory limit is set to unlimited (-1).     // - The memory limit is greater than or equal to the memory required for     // the operation.     return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toNumber($memory_limit) >= Bytes::toNumber($required)));
  }

  /** * Attempts to set the PHP maximum execution time. * * This function is a wrapper around the PHP function set_time_limit(). When * called, set_time_limit() restarts the timeout counter from zero. In other * words, if the timeout is the default 30 seconds, and 25 seconds into script * execution a call such as set_time_limit(20) is made, the script will run * for a total of 45 seconds before timing out. * * If the current time limit is not unlimited it is possible to decrease the * total time limit if the sum of the new time limit and the current time * spent running the script is inferior to the original time limit. It is * inherent to the way set_time_limit() works, it should rather be called with * an appropriate value every time you need to allocate a certain amount of * time to execute a task than only once at the beginning of the script. * * Before calling set_time_limit(), we check if this function is available * because it could be disabled by the server administrator. * * @param int $time_limit * An integer time limit in seconds, or 0 for unlimited execution time. * * @return bool * Whether set_time_limit() was successful or not. */
$form['#prefix'] = '<div id="editor-image-dialog-form">';
    $form['#suffix'] = '</div>';

    // Construct strings to use in the upload validators.     $image_upload = $editor->getImageUploadSettings();
    if (!empty($image_upload['max_dimensions']['width']) || !empty($image_upload['max_dimensions']['height'])) {
      $max_dimensions = $image_upload['max_dimensions']['width'] . 'x' . $image_upload['max_dimensions']['height'];
    }
    else {
      $max_dimensions = 0;
    }
    $max_filesize = min(Bytes::toNumber($image_upload['max_size']), Environment::getUploadMaxSize());
    $existing_file = isset($image_element['data-entity-uuid']) ? \Drupal::service('entity.repository')->loadEntityByUuid('file', $image_element['data-entity-uuid']) : NULL;
    $fid = $existing_file ? $existing_file->id() : NULL;

    $form['fid'] = [
      '#title' => $this->t('Image'),
      '#type' => 'managed_file',
      '#upload_location' => $image_upload['scheme'] . '://' . $image_upload['directory'],
      '#default_value' => $fid ? [$fid] : NULL,
      '#upload_validators' => [
        'file_validate_extensions' => ['gif png jpg jpeg'],
        'file_validate_size' => [$max_filesize],
        
$format['precision'] = 2;
    $format['locale'] = $locale;
    if (!$locale) {
        $format['locale'] = Shopware()->Container()->get('locale');
    }

    // check if value is integer     if ((int) $value == $value) {
        $format['precision'] = 0;
    }

    return Zend_Locale_Format::toNumber($value$format);
}

  protected function getUploadValidators(FieldDefinitionInterface $field_definition) {
    $validators = [
      // Add in our check of the file name length.       'file_validate_name_length' => [],
    ];
    $settings = $field_definition->getSettings();

    // Cap the upload size according to the PHP limit.     $max_filesize = Bytes::toNumber(Environment::getUploadMaxSize());
    if (!empty($settings['max_filesize'])) {
      $max_filesize = min($max_filesize, Bytes::toNumber($settings['max_filesize']));
    }

    // There is always a file size limit due to the PHP server limit.     $validators['file_validate_size'] = [$max_filesize];

    // Add the extension check if necessary.     if (!empty($settings['file_extensions'])) {
      $validators['file_validate_extensions'] = [$settings['file_extensions']];
    }

    
/** * Returns a locale formatted integer number * Alias for toNumber() * * @param string $value Number to normalize * @param array $options Options: locale, precision. See {@link setOptions()} for details. * @return string Locale formatted number */
    public static function toFloat($value, array $options = array())
    {
        $options['number_format'] = Zend_Locale_Format::STANDARD;
        return self::toNumber($value$options);
    }

    /** * Returns if a float was found * Alias for isNumber() * * @param string $input Localized number string * @param array $options Options: locale. See {@link setOptions()} for details. * @return boolean Returns true if a number was found */
    public static function isFloat($value, array $options = array())
    {
public function filter($value)
    {
        if (is_array($value)) {
            $date = new Zend_Date($value$this->_options['locale']);
            return $date->toString($this->_options['date_format']);
        } else if ($this->_options['precision'] === 0) {
            return Zend_Locale_Format::toInteger($value$this->_options);
        } else if ($this->_options['precision'] === null) {
            return Zend_Locale_Format::toFloat($value$this->_options);
        }

        return Zend_Locale_Format::toNumber($value$this->_options);
    }
}
public function __construct(MigrationInterface $migration, MigrateMessageInterface $message = NULL, EventDispatcherInterface $event_dispatcher = NULL) {
    $this->migration = $migration;
    $this->message = $message ?: new MigrateMessage();
    $this->getIdMap()->setMessage($this->message);
    $this->eventDispatcher = $event_dispatcher;
    // Record the memory limit in bytes     $limit = trim(ini_get('memory_limit'));
    if ($limit == '-1') {
      $this->memoryLimit = PHP_INT_MAX;
    }
    else {
      $this->memoryLimit = Bytes::toNumber($limit);
    }
  }

  /** * Returns the source. * * Makes sure source is initialized based on migration settings. * * @return \Drupal\migrate\Plugin\MigrateSourceInterface * The source. */
  
parent::tearDown();
  }

  /** * Tests that uploading files exceeding maximum size are handled correctly. */
  public function testUploadFileExceedingMaximumFileSize() {
    $session = $this->getSession();

    // Create a test file that exceeds the maximum POST size with 1 kilobyte.     $post_max_size = Bytes::toNumber(ini_get('post_max_size'));
    $invalid_file = 'public://exceeding_post_max_size.bin';
    $file = fopen($invalid_file, 'wb');
    fseek($file$post_max_size + 1024);
    fwrite($file, 0x0);
    fclose($file);

    // Go to the node creation form and try to upload the test file.     $this->drupalGet('node/add/article');
    $page = $session->getPage();
    $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($invalid_file));

    
$filename = $upload->getClientOriginalName();

    $editor = $request->attributes->get('editor');
    $image_upload = $editor->getImageUploadSettings();
    $destination = $image_upload['scheme'] . '://' . $image_upload['directory'];

    // Check the destination file path is writable.     if (!$this->fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) {
      throw new HttpException(500, 'Destination file path is not writable');
    }

    $max_filesize = min(Bytes::toNumber($image_upload['max_size']), Environment::getUploadMaxSize());
    if (!empty($image_upload['max_dimensions']['width']) || !empty($image_upload['max_dimensions']['height'])) {
      $max_dimensions = $image_upload['max_dimensions']['width'] . 'x' . $image_upload['max_dimensions']['height'];
    }
    else {
      $max_dimensions = 0;
    }

    $validators = [
      'file_validate_extensions' => ['gif png jpg jpeg'],
      'file_validate_size' => [$max_filesize],
      'file_validate_image_resolution' => [$max_dimensions],
    ];
// Format the number         $format = $options['format'];
        $locale = $options['locale'];
        if (empty($format)) {
            $format = Zend_Locale_Data::getContent($locale, 'currencynumber');
        } else if (Zend_Locale::isLocale($format, true, false)) {
            $locale = $format;
            $format = Zend_Locale_Data::getContent($format, 'currencynumber');
        }

        $original = $value;
        $value    = Zend_Locale_Format::toNumber($value, array('locale'        => $locale,
                                                               'number_format' => $format,
                                                               'precision'     => $options['precision']));

        if ($options['position'] !== self::STANDARD) {
            $value = str_replace('¤', '', $value);
            $space = '';
            if (iconv_strpos($value, ' ') !== false) {
                $value = str_replace(' ', '', $value);
                $space = ' ';
            }

            

  protected function getUploadValidators(FieldDefinitionInterface $field_definition) {
    $validators = [
      // Add in our check of the file name length.       'file_validate_name_length' => [],
    ];
    $settings = $field_definition->getSettings();

    // Cap the upload size according to the PHP limit.     $max_filesize = Bytes::toNumber(Environment::getUploadMaxSize());
    if (!empty($settings['max_filesize'])) {
      $max_filesize = min($max_filesize, Bytes::toNumber($settings['max_filesize']));
    }

    // There is always a file size limit due to the PHP server limit.     $validators['file_validate_size'] = [$max_filesize];

    // Add the extension check if necessary.     if (!empty($settings['file_extensions'])) {
      $validators['file_validate_extensions'] = [$settings['file_extensions']];
    }

    

  public function testToNumber($size, float $expected_number): void {
    $this->assertSame($expected_number, Bytes::toNumber($size));
  }

  /** * Provides data for testToNumber(). * * @return array * An array of arrays, each containing the argument for * \Drupal\Component\Utility\Bytes::toNumber(): size, and the expected * return value with the expected type (float). */
  public function providerTestToNumber(): array {
    
Home | Imprint | This part of the site doesn't use cookies.