/** * Stubbable method for creating a decoder instance * * @param string $stream * @param string $contentType * @param int $contentLength * @param \sndsgd\http\data\DecoderOptions $options * @return \sndsgd\http\data\decoder\DecoderInterface * @throws \sndsgd\http\exception\BadRequestException */ protected function getDecoder(string $stream, string $contentType, int $contentLength, decoder\DecoderOptions $options) { $type = \sndsgd\Str::before($contentType, ";"); if (!isset(static::$decoders[$type])) { throw new exception\BadRequestException("failed to decode request body; " . "unknown content-type '{$contentType}'"); } $class = static::$decoders[$type]; return new $class($stream, $contentType, $contentLength, $options); }
/** * {@inheritdoc} */ public function validate() { if (is_bool($this->value)) { return true; } else { if ((is_string($this->value) || is_int($this->value)) && ($newValue = Str::toBoolean($this->value)) !== null) { $this->value = $newValue; return true; } } return false; }
/** * {@inheritdoc} */ public function validate() { if (is_string($this->value)) { if (preg_match('~^([0-9\\.-]+)$~', $this->value)) { $this->value = floatval(Str::toNumber($this->value)); return true; } } else { if (is_bool($this->value) === false && ($newValue = filter_var($this->value, FILTER_VALIDATE_FLOAT)) !== false) { $this->value = $newValue; return true; } } return false; }
/** * {@inheritdoc} */ public function validate(&$value, \sndsgd\form\Validator $validator = null) : bool { if (is_bool($value)) { return true; } elseif (is_int($value)) { if ($value === 0) { $value = false; return true; } elseif ($value === 1) { $value = true; return true; } return false; } elseif (is_string($value)) { if (($newValue = \sndsgd\Str::toBoolean($value)) !== null) { $value = $newValue; return true; } return false; } return false; }
/** * Create a temp file * * @param string $prefix A prefix for the filename * @return \sndsgd\fs\File */ public static function createFile(string $prefix, int $maxAttempts = 10) : entity\FileEntity { $tmpdir = static::getDir(); $prefix = \sndsgd\Fs::sanitizeName($prefix); $attempts = 1; do { if ($attempts > $maxAttempts) { throw new \RuntimeException("failed to create temp file; " . "reached max number ({$maxAttempts}) of attempts"); } $rand = \sndsgd\Str::random(10); $path = "{$tmpdir}/{$prefix}-{$rand}"; $attempts++; } while (file_exists($path)); touch($path); $file = new entity\FileEntity($path); static::registerEntity($file); return $file; }
/** * Set the port portion of the url * * @param string|null $port */ public function setPort($port = null) { if (is_null($port) || is_int($port)) { $this->port = $port; } else { if (is_string($port) && preg_match('/^[0-9]+$/', $port) !== 0) { $this->port = Str::toNumber($port); } else { throw new InvalidArgumentException("invalid value provided for 'port'; " . " expecting an integer, string of integers, or null"); } } return $this; }
/** * Write a message * * @param string $message * @return void */ public function write($message) { $map = $this->extractStyles($message); $message = $this->applyStyles($map, $message); if (Str::endsWith($message, PHP_EOL) === false) { $message .= PHP_EOL; } echo $message; }
/** * Create a message that specifies the number of characters required * * @return string */ protected function getMessage() { return Str::replace($this->message, ['{{len}}' => $this->min, '{{what}}' => $this->min === 1 ? 'character' : 'characters']); }
/** * {@inheritdoc} */ public function getMessage() { return Str::replace($this->message, ['{{len}}' => $this->min, '{{what}}' => $this->min === 1 ? 'value' : 'values']); }
/** * {@inheritdoc} */ public function getContentType() : string { if ($this->contentType === null) { $header = strtolower($this->getHeader("content-type", "")); $this->contentType = \sndsgd\Str::before($header, ";"); } return $this->contentType; }