b2pweb /
bdf-form
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Bdf\Form\Phone; |
||||
| 4 | |||||
| 5 | use Bdf\Form\Child\ChildBuilder; |
||||
| 6 | use Bdf\Form\ElementBuilderInterface; |
||||
| 7 | use Bdf\Form\Phone\Transformer\PhoneNumberToStringTransformer; |
||||
| 8 | use Bdf\Form\Registry\RegistryInterface; |
||||
| 9 | use libphonenumber\PhoneNumberFormat; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * @extends ChildBuilder<PhoneElementBuilder> |
||||
| 13 | */ |
||||
| 14 | class PhoneChildBuilder extends ChildBuilder |
||||
| 15 | { |
||||
| 16 | /** |
||||
| 17 | * @var PhoneNumberFormat::*|null |
||||
| 18 | */ |
||||
| 19 | private $saveFormat; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @var bool |
||||
| 23 | */ |
||||
| 24 | private $formatIfInvalid = false; |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * {@inheritdoc} |
||||
| 28 | * |
||||
| 29 | * @param PhoneElementBuilder $elementBuilder |
||||
| 30 | */ |
||||
| 31 | 15 | public function __construct(string $name, ElementBuilderInterface $elementBuilder, RegistryInterface $registry = null) |
|||
| 32 | { |
||||
| 33 | 15 | parent::__construct($name, $elementBuilder, $registry); |
|||
| 34 | |||||
| 35 | 15 | $this->addTransformerProvider([$this, 'provideModelTransformer']); |
|||
| 36 | 15 | } |
|||
| 37 | |||||
| 38 | /** |
||||
| 39 | * Format the phone number even if it's invalid |
||||
| 40 | * If not, the raw input value will be used instead of the formatted one |
||||
| 41 | * |
||||
| 42 | * Note: Works only if `saveAsString()` is called |
||||
| 43 | * |
||||
| 44 | * @param bool $formatIfInvalid |
||||
| 45 | * |
||||
| 46 | * @return $this |
||||
| 47 | * |
||||
| 48 | * @see PhoneChildBuilder::saveAsString() To enable string formating when filling the entity |
||||
| 49 | */ |
||||
| 50 | 1 | public function formatIfInvalid(bool $formatIfInvalid = true): self |
|||
| 51 | { |
||||
| 52 | 1 | $this->formatIfInvalid = $formatIfInvalid; |
|||
| 53 | |||||
| 54 | 1 | return $this; |
|||
| 55 | } |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * The model value of the input will be transformer to a formatted string |
||||
| 59 | * |
||||
| 60 | * <code> |
||||
| 61 | * // The entity : the phone is a simple string |
||||
| 62 | * class MyEntity { |
||||
| 63 | * public string $phone; |
||||
| 64 | * } |
||||
| 65 | * |
||||
| 66 | * // Build the element |
||||
| 67 | * $builder->phone('phone')->saveAsString()->getter()->setter(); |
||||
| 68 | * |
||||
| 69 | * $form->import(MyEntity::get($id)); |
||||
| 70 | * $form['phone']->element()->value(); // Value is an instance of PhoneNumber |
||||
| 71 | * |
||||
| 72 | * $entity = $form->value(); |
||||
| 73 | * $entity->phone; // phone is a string |
||||
| 74 | * </code> |
||||
| 75 | * |
||||
| 76 | * @param PhoneNumberFormat::*|null $format The phone number format. Must be one of the constant of PhoneNumberFormat. Set null to disable |
||||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
| 77 | * |
||||
| 78 | * @return $this |
||||
| 79 | * |
||||
| 80 | * @see PhoneNumberToStringTransformer |
||||
| 81 | */ |
||||
| 82 | 6 | public function saveAsString(?int $format = PhoneNumberFormat::E164): self |
|||
| 83 | { |
||||
| 84 | 6 | $this->saveFormat = $format; |
|||
|
0 ignored issues
–
show
It seems like
$format can also be of type integer. However, the property $saveFormat is declared as type libphonenumber\PhoneNumberFormat. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 85 | |||||
| 86 | 6 | return $this; |
|||
| 87 | } |
||||
| 88 | |||||
| 89 | /** |
||||
| 90 | * @return PhoneNumberToStringTransformer[] |
||||
| 91 | */ |
||||
| 92 | 14 | protected function provideModelTransformer(): array |
|||
| 93 | { |
||||
| 94 | 14 | if ($this->saveFormat === null) { |
|||
| 95 | 9 | return []; |
|||
| 96 | } |
||||
| 97 | |||||
| 98 | 5 | return [new PhoneNumberToStringTransformer($this->saveFormat, $this->formatIfInvalid)]; |
|||
|
0 ignored issues
–
show
$this->saveFormat of type libphonenumber\PhoneNumberFormat is incompatible with the type integer expected by parameter $format of Bdf\Form\Phone\Transform...nsformer::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 99 | } |
||||
| 100 | } |
||||
| 101 |