Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AbstractUnicodeString.php

function AbstractUnicodeString::pad

3 calls to AbstractUnicodeString::pad()
AbstractUnicodeString::padBoth in vendor/symfony/string/AbstractUnicodeString.php
AbstractUnicodeString::padEnd in vendor/symfony/string/AbstractUnicodeString.php
AbstractUnicodeString::padStart in vendor/symfony/string/AbstractUnicodeString.php

File

vendor/symfony/string/AbstractUnicodeString.php, line 530

Class

AbstractUnicodeString
Represents a string of abstract Unicode characters.

Namespace

Symfony\Component\String

Code

private function pad(int $len, self $pad, int $type) : static {
    $sLen = $this->length();
    if ($len <= $sLen) {
        return clone $this;
    }
    $padLen = $pad->length();
    $freeLen = $len - $sLen;
    $len = $freeLen % $padLen;
    switch ($type) {
        case \STR_PAD_RIGHT:
            return $this->append(str_repeat($pad->string, intdiv($freeLen, $padLen)) . ($len ? $pad->slice(0, $len) : ''));
        case \STR_PAD_LEFT:
            return $this->prepend(str_repeat($pad->string, intdiv($freeLen, $padLen)) . ($len ? $pad->slice(0, $len) : ''));
        case \STR_PAD_BOTH:
            $freeLen /= 2;
            $rightLen = ceil($freeLen);
            $len = $rightLen % $padLen;
            $str = $this->append(str_repeat($pad->string, intdiv($rightLen, $padLen)) . ($len ? $pad->slice(0, $len) : ''));
            $leftLen = floor($freeLen);
            $len = $leftLen % $padLen;
            return $str->prepend(str_repeat($pad->string, intdiv($leftLen, $padLen)) . ($len ? $pad->slice(0, $len) : ''));
        default:
            throw new InvalidArgumentException('Invalid padding type.');
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal