/** * {@inheritdoc} */ public function getErrorMessage() : string { $opts = \sndsgd\Arr::implode(", ", $this->getWrappedOptions(), _("or") . " "); if ($this->errorMessage) { return sprintf($this->errorMessage, $opts); } return sprintf(_("must be %s"), $opts); }
/** * Add multiple headers * * @param array<string,string> $headers */ public function addMultiple(array $headers) : HeaderCollection { foreach ($headers as $key => $value) { $key = $this->getKey($key, true); \sndsgd\Arr::addValue($this->headers, $key, $value); } return $this; }
/** * {@inheritdoc} */ public function getErrorMessage() : string { $template = _("value must be a child of %s"); if (count($this->parentPaths) === 1) { return sprintf($template, $this->parentPaths[0]); } $parents = \sndsgd\Arr::implode(", ", $this->parentPaths, _("or") . " "); return sprintf($template, $parents); }
/** * {@inheritdoc} */ public function getErrorMessage() : string { if (count($this->extensions) === 1) { $template = _("must be a file with a %s extension"); $value = $this->extensions[0]; } else { $template = _("must be a file with one of the following extensions: %s"); $value = \sndsgd\Arr::implode(", ", $this->extensions, _("or") . " "); } return sprintf($template, $value); }
/** * Parse a string * * @param string $header */ public function parseString($header) { $parts = explode("\r\n", $header, 2); if (count($parts) !== 2) { throw new InvalidArgumentException("invalid value provided for 'header'; expecting a string that " . "utilizes \\r\\n line breaks"); } list($info, $fields) = $parts; list($protocol, $code, $message) = preg_split("/\\s+/", $info, 3); $this->protocol = $protocol; $this->statusCode = intval($code); $lines = explode("\r\n", trim($fields)); foreach ($lines as $line) { list($key, $value) = explode(":", $line, 2); $key = strtolower($key); $value = trim($value); Arr::addvalue($this->fields, $key, $value); } }
/** * Process the $_POST superglobal to spoof the results of a decoder * * @return array */ protected function parsePost() { $ret = $_POST ?: []; if (isset($_FILES)) { foreach ($_FILES as $name => $info) { if (is_array($info["name"])) { $len = count($info["name"]); for ($i = 0; $i < $len; $i++) { $file = $this->createUploadedFile(["name" => $info["name"][$i] ?? "", "type" => $info["type"][$i] ?? "", "tmp_name" => $info["tmp_name"][$i] ?? "", "error" => $info["error"][$i] ?? 0, "size" => $info["size"][$i] ?? 0]); \sndsgd\Arr::addValue($ret, $name, $file); } } else { $file = $this->createUploadedFile($info); \sndsgd\Arr::addValue($ret, $name, $file); } } } return $ret; }
/** * {@inheritdoc} */ public function getErrorMessage() : string { if ($this->errorMessage) { return $this->errorMessage; } $testTypes = array_values($this->getTestTypes()); switch (count($testTypes)) { case 0: return _("must be a file"); case 1: $desc = $testTypes[0]; break; case 2: $desc = $testTypes[0] . " " . _("and") . " " . $testTypes[1]; break; default: $desc = \sndsgd\Arr::implode(", ", $testTypes, _("and") . " "); } return sprintf(_("must be a %s file"), $desc); }
/** * Export the current value(s) * * note: when exporting from a collection (collection->getValues()), this * method is not called if `$this->exportHandler === Field::EXPORT_SKIP` * @param ?integer $exportHandler A handler to override the * @return array<number|string>|number|string */ public function exportValue($exportHandler = null) { if ($exportHandler === null) { $exportHandler = $this->exportHandler; } if ($exportHandler === self::EXPORT_NORMAL || $exportHandler === self::EXPORT_SKIP) { $values = $this->getValuesAsArray(); return count($values) === 1 ? $values[0] : $values; } else { if ($exportHandler === self::EXPORT_ARRAY) { if ($this->value === null) { return $this->defaultValue === null ? [] : $this->defaultValue; } return Arr::cast($this->value); } else { return call_user_func($exportHandler, $this->getValuesAsArray()); } } }
/** * Add more data to the query * * @param string|array $data * @return \sndsgd\Url */ public function addQueryData($data) { if (is_string($data)) { $data = self::decodeQueryString($data); } else { if (is_object($data)) { $data = (array) $data; } else { if (!is_array($data)) { throw new InvalidArgumentException("invalid value provided for 'query'; " . "expecting a string, array, or object"); } } } foreach ($data as $k => $v) { Arr::addValue($this->query, $k, $v); } return $this; }
/** * Add a value that has a nested key * * @param string $key * @param mixed $value * @param integer $pos The position of the first open bracket */ protected function addNestedValue(string $key, $value, int $pos) { # convert the levels into an array of strings $levels = strpos($key, "][") !== false ? explode("][", substr($key, $pos + 1, -1)) : [substr($key, $pos + 1, -1)]; array_unshift($levels, substr($key, 0, $pos)); # ensure the nesting doesn't exceed `max_nesting_levels` $levelLen = count($levels); if ($levelLen > $this->maxNestingLevels) { throw new DecodeException("max_input_nesting_level exceeded"); } $lastKey = array_pop($levels); $values =& $this->values; foreach ($levels as $level) { if ($level === "") { $level = count($values); } if (!array_key_exists($level, $values)) { $values[$level] = []; } $values =& $values[$level]; } if ($lastKey === "") { $lastKey = count($values); } \sndsgd\Arr::addValue($values, $lastKey, $value); }
/** * Add values to the fields in the collection * * @param array.<string,mixed> $fieldValues */ public function addValues(array $fieldValues) { foreach ($fieldValues as $fieldName => $values) { $values = Arr::cast($values); if (null === ($field = $this->getField($fieldName))) { $message = "unknown field"; $len = count($values); if ($len !== 1) { $message .= " (encountered {$len} values)"; } $error = new Error($message); $error->setName($fieldName); $this->addError($error); } else { foreach ($values as $index => $value) { $field->addValue($value, $index); } } } }
/** * Verify a node type is valid * * @param string $nodeType The value to verify * @return string The verified node type * @throws \InvalidArgumentException If the node type is not valid */ protected function validateNodeType(string $nodeType) : string { if (!in_array($nodeType, static::$validNodeTypes)) { throw new \InvalidArgumentException("invalid value node type '{$nodeType}'; expecting " . \sndsgd\Arr::implode(", ", static::$validNodeTypes, "or ")); } return $nodeType; }