本文整理汇总了PHP中is_scalar函数的典型用法代码示例。如果您正苦于以下问题:PHP is_scalar函数的具体用法?PHP is_scalar怎么用?PHP is_scalar使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_scalar函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: convert_uudecode
function convert_uudecode($string)
{
// Sanity check
if (!is_scalar($string)) {
user_error('convert_uuencode() expects parameter 1 to be string, ' . gettype($string) . ' given', E_USER_WARNING);
return false;
}
if (strlen($string) < 8) {
user_error('convert_uuencode() The given parameter is not a valid uuencoded string', E_USER_WARNING);
return false;
}
$decoded = '';
foreach (explode("\n", $string) as $line) {
$c = count($bytes = unpack('c*', substr(trim($line), 1)));
while ($c % 4) {
$bytes[++$c] = 0;
}
foreach (array_chunk($bytes, 4) as $b) {
$b0 = $b[0] == 0x60 ? 0 : $b[0] - 0x20;
$b1 = $b[1] == 0x60 ? 0 : $b[1] - 0x20;
$b2 = $b[2] == 0x60 ? 0 : $b[2] - 0x20;
$b3 = $b[3] == 0x60 ? 0 : $b[3] - 0x20;
$b0 <<= 2;
$b0 |= $b1 >> 4 & 0x3;
$b1 <<= 4;
$b1 |= $b2 >> 2 & 0xf;
$b2 <<= 6;
$b2 |= $b3 & 0x3f;
$decoded .= pack('c*', $b0, $b1, $b2);
}
}
return rtrim($decoded, "");
}
示例2: setValue
/**
* Sets selected items (by keys).
* @param array
* @return self
*/
public function setValue($values)
{
if (is_scalar($values) || $values === NULL) {
$values = (array) $values;
} elseif (!is_array($values)) {
throw new Nette\InvalidArgumentException(sprintf("Value must be array or NULL, %s given in field '%s'.", gettype($values), $this->name));
}
$flip = [];
foreach ($values as $value) {
if (!is_scalar($value) && !method_exists($value, '__toString')) {
throw new Nette\InvalidArgumentException(sprintf("Values must be scalar, %s given in field '%s'.", gettype($value), $this->name));
}
$flip[(string) $value] = TRUE;
}
$values = array_keys($flip);
if ($this->checkAllowedValues && ($diff = array_diff($values, array_keys($this->items)))) {
$set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) {
return var_export($s, TRUE);
}, array_keys($this->items))), 70, '...');
$vals = (count($diff) > 1 ? 's' : '') . " '" . implode("', '", $diff) . "'";
throw new Nette\InvalidArgumentException("Value{$vals} are out of allowed set [{$set}] in field '{$this->name}'.");
}
$this->value = $values;
return $this;
}
示例3: __construct
public function __construct($value)
{
if (!is_scalar($value) && null !== $value) {
throw new InvalidNativeArgumentException($value, ['string', 'int', 'bool', 'null']);
}
$this->value = $value;
}
示例4: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Length) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Length');
}
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$stringValue = (string) $value;
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
$length = grapheme_strlen($stringValue);
} elseif (function_exists('mb_strlen')) {
$length = mb_strlen($stringValue, $constraint->charset);
} else {
$length = strlen($stringValue);
}
if (null !== $constraint->max && $length > $constraint->max) {
$this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ limit }}', $constraint->max)->setInvalidValue($value)->setPlural((int) $constraint->max)->setCode(Length::TOO_LONG_ERROR)->addViolation();
return;
}
if (null !== $constraint->min && $length < $constraint->min) {
$this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ limit }}', $constraint->min)->setInvalidValue($value)->setPlural((int) $constraint->min)->setCode(Length::TOO_SHORT_ERROR)->addViolation();
}
}
示例5: validate
public function validate($input)
{
if (!is_scalar($input)) {
return false;
}
return (bool) preg_match($this->regex, $input);
}
示例6: convert_uuencode
function convert_uuencode($string)
{
// Sanity check
if (!is_scalar($string)) {
user_error('convert_uuencode() expects parameter 1 to be string, ' . gettype($string) . ' given', E_USER_WARNING);
return false;
}
$u = 0;
$encoded = '';
while ($c = count($bytes = unpack('c*', substr($string, $u, 45)))) {
$u += 45;
$encoded .= pack('c', $c + 0x20);
while ($c % 3) {
$bytes[++$c] = 0;
}
foreach (array_chunk($bytes, 3) as $b) {
$b0 = ($b[0] & 0xfc) >> 2;
$b1 = (($b[0] & 0x3) << 4) + (($b[1] & 0xf0) >> 4);
$b2 = (($b[1] & 0xf) << 2) + (($b[2] & 0xc0) >> 6);
$b3 = $b[2] & 0x3f;
$b0 = $b0 ? $b0 + 0x20 : 0x60;
$b1 = $b1 ? $b1 + 0x20 : 0x60;
$b2 = $b2 ? $b2 + 0x20 : 0x60;
$b3 = $b3 ? $b3 + 0x20 : 0x60;
$encoded .= pack('c*', $b0, $b1, $b2, $b3);
}
$encoded .= "\n";
}
// Add termination characters
$encoded .= "`\n";
return $encoded;
}
示例7: setValue
public function setValue($values)
{
if (is_scalar($values) || $values === NULL) {
$values = (array) $values;
} elseif (!is_array($values)) {
throw new Nette\InvalidArgumentException(sprintf("Value must be array or NULL, %s given in field '%s'.", gettype($values), $this->name));
}
$flip = array();
foreach ($values as $value) {
if (!is_scalar($value) && !method_exists($value, '__toString')) {
throw new Nette\InvalidArgumentException(sprintf("Values must be scalar, %s given in field '%s'.", gettype($value), $this->name));
}
$flip[(string) $value] = TRUE;
}
$values = array_keys($flip);
$items = $this->items;
$nestedKeys = array();
array_walk_recursive($items, function ($value, $key) use(&$nestedKeys) {
$nestedKeys[] = $key;
});
if ($diff = array_diff($values, $nestedKeys)) {
$range = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) {
return var_export($s, TRUE);
}, $nestedKeys)), 70, '...');
$vals = (count($diff) > 1 ? 's' : '') . " '" . implode("', '", $diff) . "'";
throw new Nette\InvalidArgumentException("Value{$vals} are out of allowed range [{$range}] in field '{$this->name}'.");
}
$this->value = $values;
return $this;
}
示例8: normalize
protected function normalize($data)
{
if (null === $data || is_scalar($data)) {
return $data;
}
if (is_array($data) || $data instanceof \Traversable) {
$normalized = array();
$count = 1;
foreach ($data as $key => $value) {
if ($count++ >= 1000) {
$normalized['...'] = 'Over 1000 items, aborting normalization';
break;
}
$normalized[$key] = $this->normalize($value);
}
return $normalized;
}
if ($data instanceof \DateTime) {
return $data->format($this->dateFormat);
}
if (is_object($data)) {
if ($data instanceof Exception) {
return $this->normalizeException($data);
}
return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true));
}
if (is_resource($data)) {
return '[resource]';
}
return '[unknown(' . gettype($data) . ')]';
}
示例9: bcinvert
function bcinvert($a, $n)
{
// Sanity check
if (!is_scalar($a)) {
user_error('bcinvert() expects parameter 1 to be string, ' . gettype($a) . ' given', E_USER_WARNING);
return false;
}
if (!is_scalar($n)) {
user_error('bcinvert() expects parameter 2 to be string, ' . gettype($n) . ' given', E_USER_WARNING);
return false;
}
$u1 = $v2 = '1';
$u2 = $v1 = '0';
$u3 = $n;
$v3 = $a;
while (bccomp($v3, '0')) {
$q0 = bcdiv($u3, $v3);
$t1 = bcsub($u1, bcmul($q0, $v1));
$t2 = bcsub($u2, bcmul($q0, $v2));
$t3 = bcsub($u3, bcmul($q0, $v3));
$u1 = $v1;
$u2 = $v2;
$u3 = $v3;
$v1 = $t1;
$v2 = $t2;
$v3 = $t3;
}
if (bccomp($u2, '0') < 0) {
return bcadd($u2, $n);
} else {
return bcmod($u2, $n);
}
}
示例10: addValue
/**
* Add a single value to the Parameter
* @param mixed $value
*/
public function addValue($value)
{
if (!is_scalar($value)) {
throw new Exception('Only scalar values permitted');
}
$this->value[] = $value;
}
示例11: format
/**
* {@inheritdoc}
*/
public function format(array $record)
{
$record = parent::format($record);
if (!isset($record['datetime'], $record['message'], $record['level'])) {
throw new \InvalidArgumentException('The record should at least contain datetime, message and level keys, ' . var_export($record, true) . ' given');
}
$message = new Message();
$message->setTimestamp($record['datetime'])->setShortMessage((string) $record['message'])->setHost($this->systemName)->setLevel($this->logLevels[$record['level']]);
if (isset($record['channel'])) {
$message->setFacility($record['channel']);
}
if (isset($record['extra']['line'])) {
$message->setLine($record['extra']['line']);
unset($record['extra']['line']);
}
if (isset($record['extra']['file'])) {
$message->setFile($record['extra']['file']);
unset($record['extra']['file']);
}
foreach ($record['extra'] as $key => $val) {
$message->setAdditional($this->extraPrefix . $key, is_scalar($val) ? $val : $this->toJson($val));
}
foreach ($record['context'] as $key => $val) {
$message->setAdditional($this->contextPrefix . $key, is_scalar($val) ? $val : $this->toJson($val));
}
if (null === $message->getFile() && isset($record['context']['exception']['file'])) {
if (preg_match("/^(.+):([0-9]+)\$/", $record['context']['exception']['file'], $matches)) {
$message->setFile($matches[1]);
$message->setLine($matches[2]);
}
}
return $message;
}
示例12: getDateTime
/**
* Converts a value to a DateTime.
* Supports microseconds
*
* @throws InvalidArgumentException if $value is invalid
* @param mixed $value \DateTime|\MongoDate|int|float
* @return \DateTime
*/
public static function getDateTime($value)
{
$datetime = false;
$exception = null;
if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
return $value;
} elseif ($value instanceof \MongoDate) {
$datetime = static::craftDateTime($value->sec, $value->usec);
} elseif (is_numeric($value)) {
$seconds = $value;
$microseconds = 0;
if (false !== strpos($value, '.')) {
list($seconds, $microseconds) = explode('.', $value);
$microseconds = (int) str_pad((int) $microseconds, 6, '0');
// ensure microseconds
}
$datetime = static::craftDateTime($seconds, $microseconds);
} elseif (is_string($value)) {
try {
$datetime = new \DateTime($value);
} catch (\Exception $e) {
$exception = $e;
}
}
if ($datetime === false) {
throw new \InvalidArgumentException(sprintf('Could not convert %s to a date value', is_scalar($value) ? '"' . $value . '"' : gettype($value)), 0, $exception);
}
return $datetime;
}
示例13: getScalar
/**
* @param string $key
* @param mixed$val
*
* @return mixed
*
* @throws InvalidArgumentException
*/
protected function getScalar($key, $val)
{
if (!is_scalar($val) && !(is_object($val) && method_exists($val, '__toString'))) {
throw new InvalidArgumentException(sprintf('%s must be a scalar or implement __toString got "%s".', $key, is_object($val) ? get_class($val) : gettype($val)));
}
return is_object($val) ? (string) $val : $val;
}
示例14: php_compat_bcpowmod
/**
* Replace bcpowmod()
*
* @category PHP
* @package PHP_Compat
* @license LGPL - http://www.gnu.org/licenses/lgpl.html
* @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
* @link http://php.net/function.bcpowmod
* @author Sara Golemon <pollita@php.net>
* @version $Revision: 1.1 $
* @since PHP 5.0.0
* @require PHP 4.0.0 (user_error)
*/
function php_compat_bcpowmod($x, $y, $modulus, $scale = 0)
{
// Sanity check
if (!is_scalar($x)) {
user_error('bcpowmod() expects parameter 1 to be string, ' . gettype($x) . ' given', E_USER_WARNING);
return false;
}
if (!is_scalar($y)) {
user_error('bcpowmod() expects parameter 2 to be string, ' . gettype($y) . ' given', E_USER_WARNING);
return false;
}
if (!is_scalar($modulus)) {
user_error('bcpowmod() expects parameter 3 to be string, ' . gettype($modulus) . ' given', E_USER_WARNING);
return false;
}
if (!is_scalar($scale)) {
user_error('bcpowmod() expects parameter 4 to be integer, ' . gettype($scale) . ' given', E_USER_WARNING);
return false;
}
$t = '1';
while (bccomp($y, '0')) {
if (bccomp(bcmod($y, '2'), '0')) {
$t = bcmod(bcmul($t, $x), $modulus);
$y = bcsub($y, '1');
}
$x = bcmod(bcmul($x, $x), $modulus);
$y = bcdiv($y, '2');
}
return $t;
}
示例15: variableName
/**
* Generate a variable name for a given object.
*
*
* * If $value is an object, the generated variable name
* will be [$object-class-short-name]_$occurence in lower case e.g. 'point_0',
* 'assessmenttest_3', ...
*
* * If $value is a PHP scalar value (not including the null value), the generated
* variable name will be [gettype($value)]_$occurence e.g. 'string_1', 'boolean_0', ...
*
* * If $value is an array, the generated variable name will be array_$occurence such as
* 'array_0', 'array_2', ...
*
* * If $value is the null value, the generated variable name will be nullvalue_$occurence
* such as 'nullvalue_3'.
*
* * Finally, if the $value cannot be handled by this method, an InvalidArgumentException
* is thrown.
*
* @param mixed $value A value.
* @param integer $occurence An occurence number.
* @return string A variable name.
* @throws InvalidArgumentException If $occurence is not a positive integer or if $value cannot be handled by this method.
*/
public static function variableName($value, $occurence = 0)
{
if (is_int($occurence) === false || $occurence < 0) {
$msg = "The 'occurence' argument must be a positive integer (>= 0).";
throw new InvalidArgumentException($msg);
}
if (is_object($value) === true) {
$object = new ReflectionObject($value);
$className = mb_strtolower($object->getShortName(), 'UTF-8');
return "{$className}_{$occurence}";
} else {
// Is it a PHP scalar value?
if (is_scalar($value) === true) {
return gettype($value) . '_' . $occurence;
} else {
if (is_array($value) === true) {
return 'array_' . $occurence;
} else {
if (is_null($value) === true) {
return 'nullvalue_' . $occurence;
} else {
$msg = "Cannot handle the given value.";
throw new InvalidArgumentException($msg);
}
}
}
}
}