Skip to content

Instantly share code, notes, and snippets.

@kelunik
Created May 3, 2018 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelunik/18b2edbd3b22b613cae7123ae9a5c256 to your computer and use it in GitHub Desktop.
Save kelunik/18b2edbd3b22b613cae7123ae9a5c256 to your computer and use it in GitHub Desktop.
<?php
namespace Amp\Http\Server\FormParser;
final class Field {
/** @var string */
private $name;
/** @var string */
private $value;
/** @var FieldAttributes */
private $attributes;
public function __construct(string $name, string $value = "", FieldAttributes $attributes = null) {
$this->name = $name;
$this->value = $value;
$this->attributes = $attributes ?? new FieldAttributes;
}
public function getName(): string {
return $this->name;
}
public function getValue(): string {
return $this->value;
}
public function hasValue(): bool {
return $this->value !== "";
}
public function getAttributes(): FieldAttributes {
return $this->attributes;
}
}
<?php
namespace Amp\Http\Server\FormParser;
final class Form {
/** @var Field[][] */
private $fields;
/**
* @param Field[] $fields
*/
public function __construct(array $fields) {
$this->fields = $fields;
}
/**
* Get first field value with a given name or null, if no such field exists.
*
* @param string $name
*
* @return string|null
*/
public function getValue(string $name) {
if (!isset($this->fields[$name][0])) {
return null;
}
return $this->fields[$name][0]->getValue();
}
/**
* Get all field values with a given name.
*
* @param string $name
*
* @return string[]
*/
public function getValueArray(string $name): array {
$values = [];
foreach ($this->fields[$name] ?? [] as $field) {
$values[] = $field->getValue();
}
return $values;
}
/**
* Get first field with a given name or null, if no such field exists.
*
* @param string $name
*
* @return Field|null
*/
public function getField(string $name) {
return $this->fields[$name][0] ?? null;
}
/**
* Get all fields with a given name.
*
* @param string $name
*
* @return Field[]
*/
public function getFieldArray(string $name): array {
return $this->fields[$name] ?? [];
}
/**
* Returns the names of the passed fields.
*
* @return string[]
*/
public function getNames(): array {
return \array_keys($this->fields);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment