Issues (116)

src/Util/FieldFinderTrait.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Bdf\Form\Util;
4
5
use Bdf\Form\ElementInterface;
6
7
/**
8
 * Helper trait for adding field finding methods
9
 * Must be used on a ChildAggregateInterface implementation, like a subclass of CustomForm
10
 *
11
 * Usage:
12
 * <code>
13
 * class CredentialsForm extends CustomForm
14
 * {
15
 *     use FieldFinderTrait;
16
 *
17
 *     protected function configure(FormBuilderInterface $builder): void
18
 *     {
19
 *         $builder->string('username')->required();
20
 *         $builder->string('password')->required()->length(['min' => 6]);
21
 *         $builder->string('confirm')->depends('password')->satisfy(function ($value) {
22
 *             if ($value !== $this->findFieldValue('password')) {
23
 *                 return 'Invalid password confirmation';
24
 *             }
25
 *         });
26
 *     }
27
 * }
28
 * </code>
29
 *
30
 * @psalm-require-implements \Bdf\Form\ElementInterface
31
 */
32
trait FieldFinderTrait
33
{
34
    /**
35
     * Find a child field by a path
36
     *
37
     * Usage:
38
     * <code>
39
     * $this->findField('/foo/bar'); // Find field bar, under foo element, starting from the root
40
     * $this->findField('foo'); // Find field foo of the current form
41
     * $this->findField('../foo'); // Find field foo of the parent form (i.e. the current form is embedded)
42
     * </code>
43
     *
44
     * @param string $path The field path
45
     *
46
     * @return ElementInterface|null The element, or null if not found
47
     *
48
     * @see FieldPath::parse() For the path syntax
49
     */
50 2
    public function findField(string $path): ?ElementInterface
51
    {
52 2
        return $this->fieldPath($path)->resolve($this);
0 ignored issues
show
$this of type Bdf\Form\Util\FieldFinderTrait is incompatible with the type Bdf\Form\Child\ChildInte...f\Form\ElementInterface expected by parameter $currentElement of Bdf\Form\Util\FieldPath::resolve(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        return $this->fieldPath($path)->resolve(/** @scrutinizer ignore-type */ $this);
Loading history...
53
    }
54
55
    /**
56
     * Get a child field value by a path
57
     *
58
     * Usage:
59
     * <code>
60
     * $this->findField('/foo/bar'); // Get value of field bar, under foo element, starting from the root
61
     * $this->findField('foo'); // Get value of field foo on the current form
62
     * $this->findField('../foo'); // Get value of field foo on the parent form (i.e. the current form is embedded)
63
     * </code>
64
     *
65
     * @param string $path The field path
66
     *
67
     * @return mixed The field value, or null if the field is not found
68
     *
69
     * @see FieldPath::parse() For the path syntax
70
     */
71 2
    public function findFieldValue(string $path)
72
    {
73 2
        return $this->fieldPath($path)->value($this);
0 ignored issues
show
$this of type Bdf\Form\Util\FieldFinderTrait is incompatible with the type Bdf\Form\Child\ChildInte...f\Form\ElementInterface expected by parameter $currentElement of Bdf\Form\Util\FieldPath::value(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        return $this->fieldPath($path)->value(/** @scrutinizer ignore-type */ $this);
Loading history...
74
    }
75
76
    /**
77
     * Parse the field path
78
     *
79
     * @param string $path
80
     *
81
     * @return FieldPath
82
     */
83 4
    private function fieldPath(string $path): FieldPath
84
    {
85 4
        if ($path[0] !== '.' && $path[0] !== '/') {
86 4
            $path = './'.$path;
87
        }
88
89 4
        return FieldPath::parse($path);
90
    }
91
}
92