PHPackages                             beerline/php-custom-annotations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. beerline/php-custom-annotations

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

beerline/php-custom-annotations
===============================

Create and work with custom php annotation with pleasure.

0.3.2(5y ago)1291MITPHPPHP ^7.2

Since Oct 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/beerline/php-custom-annotations)[ Packagist](https://packagist.org/packages/beerline/php-custom-annotations)[ Docs](https://github.com/beerline/php-custom-annotations)[ RSS](/packages/beerline-php-custom-annotations/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (8)Dependencies (1)Versions (9)Used By (0)

Php custom annotation
=====================

[](#php-custom-annotation)

Create and work with custom php annotation with pleasure.

Installation
------------

[](#installation)

**Step 1: Download the Bundle**Open a command console, enter your project directory and execute the following command to download the latest stable release for this bundle: `$ composer require beerline/php-custom-annotations`

**Step 2: Enable the Bundle**For Symfony declare new service at `config/services.yaml`

```
    services:
      // Your services here
      Beerline\PhpCustomAnnotations\Metadata\PropertyMetadataPicker:
        class: 'Beerline\PhpCustomAnnotations\Metadata\PropertyMetadataPicker'
```

How to use
----------

[](#how-to-use)

To add metadata to property of your class:

1. Create metadata class.
2. Add annotation `@Annotation` and `@Annotation\Target("PROPERTY")` to metadata class.
3. Annotate property of your class by metadata class.
4. Use `Beerline\PhpCustomAnnotations\Metadata\PropertyMetadataPicker::findPropertiesAllMetadata( object $entity )` to get array of all annotation of all property of given class.
5. Use `Beerline\PhpCustomAnnotations\Metadata\PropertyMetadataPicker::findPropertyCertainMetadata( object $entity, string $metadataClassName  )` method to get only properties with specific metadata class. `$metadataClassName` is name of metadata class

Examples
--------

[](#examples)

Imagine we have Product class.

```
    class Product
    {
      /** @var int */
      private $id;

      /** @var \DateTime */
      private $dateProduction;

      /**
       * @var string
       * @Translate(translatable=true)
       */
      private $name;

      /**
       * @var string
       * @Translate(translatable=true)
       */
      private $description;

      public function __construct( string $name, DateTime $date, string $description)
      {
          $this->name = $name;
          $this->dateProduction = $date;
          $this->description = $description;
      }
    }
```

Some of it’s properties should be translated:

- name
- description

To specify which properties should be translated lets create metadata Class called `Translatable`

```
    /**
     * @Annotation
     * @Annotation\Target("PROPERTY")
     */
    class Translatable
    {
      /**
       * @Required
       * @var boolean
       */
      public $translatable;
    }
```

Now mark properies `name` and `description` by metadata class

```
    // ...
        /**
         * @var string
         * @Translatable(translatable=true)
         */
        private $name;

        /**
         * @var string
         * @Translatable(translatable=true)
         */
        private $description
    // ...
```

**That all.** All you need now it is use `PropertyMetadataPicker` to get this properties

```
