/** * Translate a long text by Google, if it too long, will separate it.. * * @param string $text String to translate. * @param string $SourceLan Translate from this language, eg: 'zh-TW'. Empty will auto detect. * @param string $ResultLan Translate to this language, eg: 'en'. Empty will auto detect. * @param integer $separate Separate text by a number of words, batch translate them and recombine to return. * * @return string Translated text. */ public static function translate($text, $SourceLan = null, $ResultLan = null, $separate = 0) { // If text too big, separate it. if ($separate) { if (Utf8String::strlen($text) > $separate) { $text = Utf8String::str_split($text, $separate); } else { $text = array($text); } } else { $text = array($text); } $result = ''; // Do translate by google translate API. foreach ($text as $txt) { $result .= self::gTranslate($txt, $SourceLan, $ResultLan); } return $result; }
/** * normalizeBacktrace * * @param array $trace * * @return array */ public static function normalizeBacktrace(array $trace) { $trace = new Data($trace); $args = []; foreach ($trace['args'] as $arg) { if (is_array($arg)) { $arg = 'Array'; } elseif (is_object($arg)) { $arg = ReflectionHelper::getShortName($arg); } elseif (is_string($arg)) { if (Utf8String::strlen($arg) > 20) { $arg = Utf8String::substr($arg, 0, 20) . '...'; } $arg = StringHelper::quote($arg); } elseif (is_null($arg)) { $arg = 'NULL'; } elseif (is_bool($arg)) { $arg = $arg ? 'TRUE' : 'FALSE'; } $args[] = $arg; } return array('file' => $trace['file'] ? $trace['file'] . ' (' . $trace['line'] . ')' : null, 'function' => ($trace['class'] ? $trace['class'] . $trace['type'] : null) . $trace['function'] . sprintf('(%s)', implode(', ', $args))); }
/** * Test... * * @param string $string @todo * @param string $expect @todo * * @return array * * @covers Windwalker\String\Utf8String::strlen * @dataProvider seedTestStrlen * @since 1.0 */ public function testStrlen($string, $expect) { $actual = Utf8String::strlen($string); $this->assertEquals($expect, $actual); }
/** * Is home page? * * @return boolean */ public static function isHome() { $uri = \JUri::getInstance(); $root = $uri::root(true); // Get site route $route = Utf8String::substr($uri->getPath(), Utf8String::strlen($root)); // Remove index.php $route = str_replace('index.php', '', $route); if (!trim($route, '/') && !$uri->getVar('option')) { return true; } return false; }
/** * endsWith * * @param string $string * @param string $target * @param boolean $caseSensitive * * @return boolean */ public static function endsWith($string, $target, $caseSensitive = true) { $stringLength = Utf8String::strlen($string); $targetLength = Utf8String::strlen($target); if ($stringLength < $targetLength) { return false; } if (!$caseSensitive) { $string = strtolower($string); $target = strtolower($target); } $end = Utf8String::substr($string, -$targetLength); return $end === $target; }
/** * Pivot $origin['prefix_xxx'] to $target['prefix']['xxx']. * * @param string $prefix A prefix text. * @param array $origin Origin array to pivot. * @param array $target A target array to store pivoted value. * * @return array Pivoted array. */ public static function pivotFromPrefix($prefix, $origin, $target = null) { $target = is_object($target) ? (object) $target : (array) $target; foreach ((array) $origin as $key => $row) { if (strpos($key, $prefix) === 0) { $key2 = Utf8String::substr($key, Utf8String::strlen($prefix)); self::setValue($target, $key2, $row); } } return $target; }
/** * validate * * @param Data $data * * @return void * * @throws ValidFailException */ protected function validate($data) { if (!$data->tos) { // throw new ValidFailException('You must agree terms and policy'); } if (!$data->username) { throw new ValidFailException('Username empty'); } if (!(new EmailValidator())->validate($data->email)) { throw new ValidFailException('Email not valid.'); } if (!$data->password) { throw new ValidFailException('Password empty'); } if (Utf8String::strlen($data->password) < 4) { throw new ValidFailException('Password should longer than 4 characters.'); } if ($data->password != $data->password2) { throw new ValidFailException('Password not match.'); } }