Skip to content

Code annotation types

Yannick Warnier edited this page Jul 6, 2021 · 1 revision

Because Chamilo 2.0 only runs on PHP 8.0+ and uses Symfony and a series of exciting bundles, you will see many new annotation types appearing in the Chamilo classes definitions.

This page tries to report on them as a type of dictionary, so you can refer here if you don't know what an annotation is for.

Annotation will always appear in the following form:

#[Something]
or
#[Someotherthing(Some info)]

Assert

Annotations of the Assert type (attribute-level) are applied to class attributes, and will make sure Symfony casts a soft error if some code tries to assign a value that does not match whatever requirement is set. For example:

#[Assert\NotNull]

will ensure the attribute is never null (Symfony will fail with an explicit error message if trying to set it to null, or if not setting it at all).

UniqueEntity

The UniqueEntity annotation is a class-level annotation will also ensure, before database validation, that the object instanciating the entity has a combination of attributes that is unique. This is a bit complex for the system to check, as it needs a collection or repository to realize there is a duplicate.

Example:

#[UniqueEntity(
    fields: ['user', 'friend', 'relationType'],
    errorPath: 'User',
    message: 'User relation already exists',
)]

Groups

The groups annotation is an attribute-level annotation that will simply define some groups the attribute "responds to". This is usually linked to some other access definition like the ApiFilter annotation.

Example:

#[Groups(['user_rel_user:read', 'user_rel_user:write'])]

ApiResource

The ApiResource annotation is a class-level annotation related to API Platform (which auto-provides web services). It has a complex structure which defines how web services will be provided.

Example for the UserRelUser entity:

#[ApiResource(
    collectionOperations: [
        'get' => [
            //'security' => "is_granted('ROLE_ADMIN')",
        ],
        'post' => [
            'security_post_denormalize' => "is_granted('CREATE', object)",
        ],
    ],
    itemOperations: [
        'get' => [
            //'security' => "is_granted('ROLE_ADMIN')",
        ],
        'put' => [
            'security' => "is_granted('EDIT', object)",
        ],
        'delete' => [
            'security' => "is_granted('DELETE', object)",
        ],
    ],
    attributes: [
        'security' => 'is_granted("ROLE_USER")',
    ],
    denormalizationContext: [
        'groups' => ['user_rel_user:write'],
    ],
    normalizationContext: [
        'groups' => ['user_rel_user:read', 'timestampable_created:read'],
    ],
)]

ApiFilter

ApiFilter is a class-level annotation that defines the fields (attributes) by which the webservices can be filtered.

Example:

#[ApiFilter(SearchFilter::class, properties: [
    'user' => 'exact',
    'friend' => 'exact',
    'relationType' => 'exact',
])]
Clone this wiki locally