Debian Codegraph @

vendor/ipl/validator/src/DeferredInArrayValidator.php

raw
1 <?php
2
3 namespace ipl\Validator;
4
5 /**
6 * Validate whether a value exists in the haystack created by the callback
7 */
8 class DeferredInArrayValidator extends InArrayValidator
9 {
10 /** @var callable Callback to create the haystack array */
11 protected $callback;
12
13 /**
14 * Create a new DeferredInArrayValidator
15 *
16 * **Required parameter:**
17 *
18 * - `callback`: (`callable`) The callback to create the haystack
19 *
20 * **Optional parameter:**
21 *
22 * *options: (`array`) Following option can be defined:*
23 *
24 * * `strict`: (`bool`) Whether the types of the needle in the haystack should also match, default `false`
25 *
26 * @param callable $callback The callback to create the haystack
27 * @param array{haystack?: mixed[], strict?: bool} $options
28 */
29 public function __construct(callable $callback, array $options = [])
30 {
31 $this->callback = $callback;
32
33 parent::__construct($options);
34 }
35
36 /**
37 * Get the haystack, invoking the callback if no haystack has been set
38 *
39 * @return mixed[]
40 */
41 public function getHaystack(): array
42 {
43 return $this->haystack ?? call_user_func($this->callback);
44 }
45
46 /**
47 * Set the callback
48 *
49 * @param callable $callback
50 *
51 * @return $this
52 */
53 public function setCallback(callable $callback): static
54 {
55 $this->haystack = null;
56 $this->callback = $callback;
57
58 return $this;
59 }
60 }