Skip to content

Commit

Permalink
[TypeInfo] Add accepts method
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Jan 7, 2025
1 parent e4f8ff8 commit 951f461
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion components/type_info.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Advanced Usages
The TypeInfo component provides various methods to manipulate and check types,
depending on your needs.

Checking a **simple type**::
**Identify** a type::

// define a simple integer type
$type = Type::int();
Expand All @@ -141,6 +141,23 @@ Checking a **simple type**::
$type->isIdentifiedBy(DummyParent::class); // true
$type->isIdentifiedBy(DummyInterface::class); // true

Checking if a type **accepts a value**::

$type = Type::int();
// check if the type accepts a given value
$type->accepts(123); // true
$type->accepts('z'); // false

$type = Type::union(Type::string(), Type::int());
// now the second check is true because the union type accepts either a int and a string value
$type->accepts(123); // true
$type->accepts('z'); // true

.. versionadded:: 7.3

The :method:`Symfony\\Component\\TypeInfo\\Type::accepts`
method was introduced in Symfony 7.3.

Using callables for **complex checks**::

class Foo
Expand Down

0 comments on commit 951f461

Please sign in to comment.