Converts a language code, e.g. 'de' to the KlarnaLanguage constant.

@param string $val language code

@return int|null
5944
public static function fromCode($val)
    {
        $val = strtoupper($val);
        if (array_key_exists($val, self::$_languages)) {
            return self::$_languages[$val];
        }
        return null;
    }
1064761
private static function iniGetBytes($val)
    {
        $val = trim(ini_get($val));
        if ($val != '') {
            $last = strtolower(
                $val{strlen($val) - 1}
            );
        } else {
            $last = '';
        }
        $val = (int) $val;
        switch ($last) {
            // The 'G' modifier is available since PHP 5.1.0
            case 'g':
                $val *= 1024;
            case 'm':
                $val *= 1024;
            case 'k':
                $val *= 1024;
        }

        return $val;
    }
367963
protected static function binaryStrlen($val)
    {
        for ($i=0; $i<strlen($val); ++$i) {
            if (ord($val{$i}) >= 128) {
                $val = self::convertCharset($val, 'UTF-8', 'UTF-16BE');
                return strlen($val);
            }
        }

        return strlen($val);
    }
334252
public function getLanguageCode($lang = null)
    {
        $parts = $this->_callInternalLocale('parseLocale', null, $lang, false);
        return isset($parts['language']) ? $parts['language'] : null;
    }
214874
public function get($val)
    {
        if (array_key_exists($val, $this->commands)) {
            return $this->commands[$val];
        }

        return null;
    }
1186816
public static function getCode($val, $alpha3 = false)
    {
        if (self::$_countryFlip === array()) {
            self::$_countryFlip = array_flip(self::$_countries);
        }
        if (!array_key_exists($val, self::$_countryFlip)) {
            return null;
        }
        $result = self::$_countryFlip[$val];
        if ($alpha3) {
            return self::$_tlcMap[$result];
        }
        return $result;
    }
79434
private function encodeValue($val, $base)
    {
        $this->preOpMethodParamsCheck(array($val, $base));

        $digits = $this->baseCheck($base);

        try {

            $new = '';

            while ($this->math->comp($val, '0') > 0) {
                $qq  = $this->math->div($val, $base);
                $rem = $this->math->mod($val, $base);
                $val = $qq;
                $new = $new . $digits[$rem];
            }

            return $new;

        } catch (\Exception $e) {
            throw $e;
        }
    }
327637
public function language($code, $locale = null)
    {
        $codes = explode('_', $code);

        $name = Intl::getLanguageBundle()->getLanguageName(
            $codes[0],
            $codes[1] ?? null,
            $locale ?: $this->localeDetector->getLocale()
        );

        return $name ? $this->fixCharset($name) : '';
    }
949615
private static function parseValue($val)
    {
        $parsedVal = null;

        // Cleanup
        $val = trim($val);

        if($val === '')
        {
            throw new Exception('Empty value not allowed');
        }

        // Boolean
        if($val == 'true' || $val == 'false')
        {
            $parsedVal = ($val == 'true');
        }
        // Literal multi-line string
        elseif(substr($val, 0, 3) == "'''" && substr($val, -3) == "'''")
        {
            $parsedVal = substr($val, 3, -3);

            // Trim first newline on multi-line string definition
            if($parsedVal[0] == "\n")
            {
                $parsedVal = substr($parsedVal, 1);
            }
        }
        // Literal string
        elseif($val[0] == "'" && substr($val, -1) == "'")
        {
            // No newlines allowed
            if(strpos($val, "\n") !== false)
            {
                throw new Exception('New lines not allowed on single line string literals.');
            }

            $parsedVal = substr($val, 1, -1);
        }
        // Multi-line string
        elseif(substr($val, 0, 3) == '"""' && substr($val, -3) == '"""')
        {
            $parsedVal = substr($val, 3, -3);

            // Trim first newline on multi-line string definition
            if($parsedVal[0] == "\n")
            {
                $parsedVal = substr($parsedVal, 1);
            }

            // Use json_decode to finally parse the string.
            $parsedVal = json_decode('"' . str_replace("\n", '\n', $parsedVal) . '"');
        }
        // String
        elseif($val[0] == '"' && substr($val, -1) == '"')
        {
            // TOML's specification says it's the same as for JSON format... so
            $parsedVal = json_decode($val);
        }
        // Numbers
        elseif(is_numeric(str_replace('_', '', $val)))
        {
            $val = str_replace('_', '', $val);

            $intVal = filter_var($val, FILTER_VALIDATE_INT);
            if($intVal !== false)
            {
                $parsedVal = $intVal;
            }
            else
            {
                $parsedVal = (float) $val;
            }
        }
        // Datetime. Parsed to UNIX time value.
        elseif(self::isISODate($val))
        {
            $parsedVal = new DateTime($val);
        }
        // Single line array (normalized)
        elseif($val[0] == '[' && substr($val, -1) == ']')
        {
            $parsedVal = self::parseArray($val);
        }
        // Inline table (normalized)
        elseif($val[0] == '{' && substr($val, -1) == '}')
        {
            $parsedVal = self::parseInlineTable($val);
        }
        else
        {
            throw new Exception('Unknown value type: ' . $val);
        }

        return $parsedVal;
    }
244578
private function jsEncode($val)
	{
		if (is_null($val)) {
			return 'null';
		}
		if ($val === false) {
			return 'false';
		}
		if ($val === true) {
			return 'true';
		}
		if (is_scalar($val))
		{
			if (is_float($val))
			{
				// Always use "." for floats.
				$val = str_replace(",", ".", strval($val));
			}

			// Use @@ to not use quotes when outputting string value
			if (strpos($val, '@@') === 0) {
				return substr($val, 2);
			}
			else {
				// All scalars are converted to strings to avoid indeterminism.
				// PHP's "1" and 1 are equal for all PHP operators, but
				// JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend,
				// we should get the same result in the JS frontend (string).
				// Character replacements for JSON.
				static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'),
				array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));

				$val = str_replace($jsonReplaces[0], $jsonReplaces[1], $val);
				if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.') {
					return $val;
				}

				return '"' . $val . '"';
			}
		}
		$isList = true;
		for ($i = 0, reset($val); $i < count($val); $i++, next($val))
		{
			if (key($val) !== $i)
			{
				$isList = false;
				break;
			}
		}
		$result = array();
		if ($isList)
		{
			foreach ($val as $v) $result[] = $this->jsEncode($v);
			return '[ ' . join(', ', $result) . ' ]';
		}
		else
		{
			foreach ($val as $k => $v) $result[] = $this->jsEncode($k).': '.$this->jsEncode($v);
			return '{ ' . join(', ', $result) . ' }';
		}
	}
1169307
public static function getLuhnKey($val = null)
    {
        if (is_null($val)) {
            return null;
        }
        $val = $val.'0';
        $length = strlen($val);
        $checksum = 0;
        for ($i=($length - 1); $i>=0; $i--) { 
            $digit = $val[$i];
            if ((($length - $i) % 2) == 0) {
                $digit = $digit * 2;
                if ($digit > 9) {
                    $digit = $digit - 9;
                }
            }
            $checksum += $digit;
        }
        $luhn_key = 10 - ( $checksum % 10 );
        if ($luhn_key == 10) {
            $luhn_key = 0;
        }
        return $luhn_key;
    }
995342
protected function validateCastValue($val)
    {
        if (isset($this->descriptor()->bareNumber) && $this->descriptor()->bareNumber === false) {
            return mb_ereg_replace('((^\D*)|(\D*$))', '', $val);
        } elseif (!is_numeric($val)) {
            throw $this->getValidationException('value must be numeric', $val);
        } else {
            $intVal = (int) $val;
            if ($intVal != (float) $val) {
                throw $this->getValidationException('value must be an integer', $val);
            } else {
                return $intVal;
            }
        }
    }
550290
public function getLanguageByCode($code)
    {
        $returnValue = null;


        $langClass = new core_kernel_classes_Class(static::CLASS_URI_LANGUAGES);
	    $langs = $langClass->searchInstances(array(
            OntologyRdf::RDF_VALUE => $code
	    ), array(
	    	'like' => false
	    ));
	    if (count($langs) == 1) {
	    	$returnValue = current($langs);
	    } else {
	    	common_Logger::w('Could not find language with code '.$code);
	    }


        return $returnValue;
    }
91050
public function parseOne($val)
    {
        if ($val === null || $val === '') {
            if ($this->allowNull()) {
                return null;
            } else {
                throw new InvalidArgumentException(
                    'Value can not be NULL (not allowed)'
                );
            }
        }

        if (!is_array($val)) {
            $val = json_decode($val, true);
        }

        return $val;
    }
1110693
protected function valType($type)
    {
        $type = IriFactory::create($type);

        return strlen($type->name) ? $type : null;
    }
148890
public function getCode128CheckNum($val)
    {
        $sum = 103;
        for ($i = 0; $i < strlen($val); $i++) {
            $sum += ($i + 1) * $this->ChrToC128(ord(substr($val, $i, 1)));
        }

        $ch_num = $sum % 103;

        return $ch_num;
    }
925043
private static function parseInlineTable($val)
    {
        // Check valid inline table
        if($val[0] == '{' && substr($val, -1) == '}')
        {
            $val = substr($val, 1, -1);
        }
        else
        {
            throw new Exception('Invalid inline table definition: ' . $val);
        }

        $result = new stdClass();
        $openString = false;
        $openLString = false;
        $buffer = '';

        $strLen = strlen($val);
        for($i = 0; $i < $strLen; $i++)
        {
            // Handle strings
            if($val[$i] == '"' && $val[$i - 1] != "\\")
            {
                $openString = !$openString;
            }
            elseif($val[$i] == "'") {
                $openLString = !$openLString;
            }

            if( $val[$i] == ',' && !$openString && !$openLString )
            {
                // Parse buffer
                $kv = explode('=', $buffer, 2);
                $result->{trim($kv[0])} = self::parseValue( trim($kv[1]) );

                // Clear buffer
                $buffer = '';
            }
            else
            {
                $buffer .= $val[$i];
            }
        }

        // Parse last buffer
        $kv = explode('=', $buffer, 2);
        $result->{trim($kv[0])} = self::parseValue( trim($kv[1]) );

        return $result;
    }