Example #1
0
 public function pad($padchar, $padlen, $type = STR_PAD_LEFT)
 {
     if (self::$isMbstringLoaded) {
         $filllen = $padlen - $this->length();
         if ($filllen < 1) {
             return $this;
         } else {
             $padChar = new self($padchar);
             if ($type === STR_PAD_LEFT || $type === STR_PAD_RIGHT) {
                 if (($_len = $padChar->length()) === 1) {
                     $padChar->repeat($filllen);
                 } elseif ($_len !== $filllen) {
                     if ($filllen < $_len) {
                         $padChar->set($padChar->substring(0, $filllen));
                     } else {
                         $padChar->repeat(floor($filllen / $_len));
                         if (($rem = $filllen % $_len) !== 0) {
                             $padChar->append($padChar->substring(0, $rem));
                         }
                     }
                 }
                 if ($type === STR_PAD_LEFT) {
                     $this->string = $padChar . $this->string;
                 } else {
                     $this->string .= $padChar;
                 }
             } elseif ($type === STR_PAD_BOTH) {
                 $this->pad($padchar, (int) floor($filllen / 2) + $this->length(), STR_PAD_LEFT);
                 $this->pad($padchar, $padlen, STR_PAD_RIGHT);
             } else {
                 $message = __METHOD__ . "() invalid pad type. Use STR_PAD_LEFT or STR_PAD_RIGHT, STR_PAD_BOTH.";
                 throw new Sabel_Exception_InvalidArgument($message);
             }
         }
     } else {
         $this->string = str_pad($this->string, $padlen, $padchar, $type);
     }
     return $this;
 }