laravel-6 support

This commit is contained in:
RafficMohammed
2023-01-08 01:17:22 +05:30
parent 1a5c16ae4b
commit 774eed8b0e
4962 changed files with 279380 additions and 297961 deletions

View File

@@ -20,14 +20,12 @@ use Symfony\Component\DomCrawler\Field\FormField;
*/
class FormFieldRegistry
{
private $fields = array();
private $fields = [];
private $base;
private $base = '';
/**
* Adds a field to the registry.
*
* @param FormField $field The field
*/
public function add(FormField $field)
{
@@ -35,8 +33,8 @@ class FormFieldRegistry
$target = &$this->fields;
while ($segments) {
if (!is_array($target)) {
$target = array();
if (!\is_array($target)) {
$target = [];
}
$path = array_shift($segments);
if ('' === $path) {
@@ -49,17 +47,15 @@ class FormFieldRegistry
}
/**
* Removes a field and its children from the registry.
*
* @param string $name The fully qualified name of the base field
* Removes a field based on the fully qualifed name and its children from the registry.
*/
public function remove($name)
public function remove(string $name)
{
$segments = $this->getSegments($name);
$target = &$this->fields;
while (count($segments) > 1) {
while (\count($segments) > 1) {
$path = array_shift($segments);
if (!array_key_exists($path, $target)) {
if (!\is_array($target) || !\array_key_exists($path, $target)) {
return;
}
$target = &$target[$path];
@@ -68,22 +64,20 @@ class FormFieldRegistry
}
/**
* Returns the value of the field and its children.
* Returns the value of the field based on the fully qualifed name and its children.
*
* @param string $name The fully qualified name of the field
*
* @return mixed The value of the field
* @return FormField|FormField[]|FormField[][] The value of the field
*
* @throws \InvalidArgumentException if the field does not exist
*/
public function &get($name)
public function &get(string $name)
{
$segments = $this->getSegments($name);
$target = &$this->fields;
while ($segments) {
$path = array_shift($segments);
if (!array_key_exists($path, $target)) {
throw new \InvalidArgumentException(sprintf('Unreachable field "%s"', $path));
if (!\is_array($target) || !\array_key_exists($path, $target)) {
throw new \InvalidArgumentException(sprintf('Unreachable field "%s".', $path));
}
$target = &$target[$path];
}
@@ -92,13 +86,11 @@ class FormFieldRegistry
}
/**
* Tests whether the form has the given field.
*
* @param string $name The fully qualified name of the field
* Tests whether the form has the given field based on the fully qualified name.
*
* @return bool Whether the form has the given field
*/
public function has($name)
public function has(string $name): bool
{
try {
$this->get($name);
@@ -110,21 +102,22 @@ class FormFieldRegistry
}
/**
* Set the value of a field and its children.
* Set the value of a field based on the fully qualified name and its children.
*
* @param string $name The fully qualified name of the field
* @param mixed $value The value
* @param mixed $value The value
*
* @throws \InvalidArgumentException if the field does not exist
*/
public function set($name, $value)
public function set(string $name, $value)
{
$target = &$this->get($name);
if ((!is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) {
if ((!\is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) {
$target->setValue($value);
} elseif (is_array($value)) {
$fields = self::create($name, $value);
foreach ($fields->all() as $k => $v) {
} elseif (\is_array($value)) {
$registry = new static();
$registry->base = $name;
$registry->fields = $value;
foreach ($registry->all() as $k => $v) {
$this->set($k, $v);
}
} else {
@@ -135,47 +128,21 @@ class FormFieldRegistry
/**
* Returns the list of field with their value.
*
* @return FormField[] The list of fields as array((string) Fully qualified name => (mixed) value)
* @return FormField[] The list of fields as [string] Fully qualified name => (mixed) value)
*/
public function all()
public function all(): array
{
return $this->walk($this->fields, $this->base);
}
/**
* Creates an instance of the class.
*
* This function is made private because it allows overriding the $base and
* the $values properties without any type checking.
*
* @param string $base The fully qualified name of the base field
* @param array $values The values of the fields
*
* @return static
*/
private static function create($base, array $values)
{
$registry = new static();
$registry->base = $base;
$registry->fields = $values;
return $registry;
}
/**
* Transforms a PHP array in a list of fully qualified name / value.
*
* @param array $array The PHP array
* @param string $base The name of the base field
* @param array $output The initial values
*
* @return array The list of fields as array((string) Fully qualified name => (mixed) value)
*/
private function walk(array $array, $base = '', array &$output = array())
private function walk(array $array, ?string $base = '', array &$output = []): array
{
foreach ($array as $k => $v) {
$path = empty($base) ? $k : sprintf('%s[%s]', $base, $k);
if (is_array($v)) {
if (\is_array($v)) {
$this->walk($v, $path, $output);
} else {
$output[$path] = $v;
@@ -188,18 +155,14 @@ class FormFieldRegistry
/**
* Splits a field name into segments as a web browser would do.
*
* <code>
* getSegments('base[foo][3][]') = array('base', 'foo, '3', '');
* </code>
*
* @param string $name The name of the field
* getSegments('base[foo][3][]') = ['base', 'foo, '3', ''];
*
* @return string[] The list of segments
*/
private function getSegments($name)
private function getSegments(string $name): array
{
if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) {
$segments = array($m['base']);
$segments = [$m['base']];
while (!empty($m['extra'])) {
$extra = $m['extra'];
if (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $extra, $m)) {
@@ -212,6 +175,6 @@ class FormFieldRegistry
return $segments;
}
return array($name);
return [$name];
}
}