/** * Choose the better charset for $data. * * Return the charset letter (A, B or C). $part is the string * to be converted using returned charset and it is extracted * from $data. * * If anyone charset was choosed, returned a empty string. * * @param string $data * @param string $part * @param string $curCharset Current charset in use, if there was one * @return string */ protected function chooseCharset(&$data, &$part, $curCharset = null) { $tests = ['/^\\d{2,3}$/' => !$curCharset ? 'C' : $curCharset, '/^\\d{4,}/' => 'C', '/^\\d{0,3}[\\x20-\\x2f\\x40-\\xff]+/' => 'B', '/^\\d{0,3}[\\x00-\\x2f\\x40-\\x60]+/' => 'A', '/^\\d/' => $curCharset && $curCharset != 'C' ? $curCharset : 'B']; $charset = $match = null; $length = 0; foreach ($tests as $regex => $testCharset) { if (!\preg_match($regex, $data, $match)) { continue; } $n = \strlen($match[0]); if ($testCharset == 'C' && $n % 2 > 0) { $n--; if ($curCharset && $curCharset != 'C') { $charset = $curCharset; $length = 1; break; } } if ($n > $length) { $charset = $testCharset; $length = $n; if ($match[0] == $data) { break; } } } if ($charset) { $part = \substr_remove($data, 0, $length); } return $charset; }
/** * Encodes a width char (ex. NWNWW) to a binary string using * the defined wide and narrow width. * * Encode each char alternating bar and spaces, begin with bar from left * to right. * * Assuming a "N" as a narrow bar/space and "W" as a wide bar/space. * * @param string $nwString narrow/wide encode string * @return string */ protected function widthToBinary($nwString) { $encoded = ''; $bar = true; while (!empty($nwString)) { $nw = \substr_remove($nwString, 0, 1); $encoded .= $this->encodeWithWidth($nw == 'N', $bar); $bar = !$bar; } return $encoded; }
/** * Create a instance using a ISSN number. * * ISSN has a format like be XXXX-XXXX. Zero left padding will be * applied if necessary. * * If ISSN number is invalid, a exception will be thrown. * * @param string $issn * @return ISSN * @throws ISSNException */ public static function fromISSN($issn) { $issn = \preg_replace('/[^\\d]/', '', $issn); $check = \substr_remove($issn, -1); if (!empty($issn)) { $issn = self::zeroLeftPadding($issn, 7); $me = new self(self::SYSTEM . $issn . '00', false); if ($me->issn == $issn . \strtoupper($check)) { return $me; } } throw new ISSNException('Invalid ISSN checksum number!'); }
/** * @test */ public function substrRemoveTest() { $string = __FUNCTION__; $this->assertTrue(\substr_remove($string, 2) == 'bstrRemoveTest' && $string == 'su'); $string = __FUNCTION__; $this->assertTrue(\substr_remove($string, 0, 3) == 'sub' && $string == 'strRemoveTest'); $string = __FUNCTION__; $this->assertTrue(\substr_remove($string, 3, 4) == 'strR' && $string == 'subemoveTest'); $string = __FUNCTION__; $this->assertTrue(\substr_remove($string, -3) == 'est' && $string == 'substrRemoveT'); $string = __FUNCTION__; $this->assertTrue(\substr_remove($string, 5, -2) == 'rRemoveTe' && $string == 'substst'); $string = __FUNCTION__; $this->assertTrue(\substr_remove($string, -3, -2) == 'e' && $string == 'substrRemoveTst'); }
/** * * @param EncoderInterface $encoder * @param string $data */ protected function encodeData(EncoderInterface &$encoder, $data) { $encoder->addBinary('10110010'); while (!empty($data)) { $char = \substr_remove($data, 0, 1); $encoded = self::$encodingTable[$char] . '0'; $encoder->addBinary($encoded); } $encoder->addBinary('1011001'); }
/** * Extracts the checksum of data and return it. * * Using getCheckLength() and getCheckPosition() to extract it. * * @param string $data * @param string $cleanData Data without checksum * @return int */ protected function extractChecksum($data, &$cleanData) { $len = $this->getCheckLength(); $pos = $this->getCheckPosition(); $checksum = \substr_remove($data, $pos, $len); $cleanData = $data; return $checksum; }