PHP code example of beerline / php-custom-annotations

1. Go to this page and download the library: Download beerline/php-custom-annotations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

beerline / php-custom-annotations example snippets


    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;
      }
    }

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

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

    
    use Doctrine\Common\Annotations\AnnotationReader;
    use Beerline\PhpCustomAnnotations\Metadata\PropertyMetadataPicker;
    
    $product = new Product( 'iPhone', now(), 'Designed by Apple in California' );
    $propertyMetadataPicker = new PropertyMetadataPicker( new AnnotationReader() );
    
    $propertiesMetadata = $propertyMetadataPicker->findPropertyCertainMetadata(
        $product,
        Translatable::class
    );
    
    foreach ($propertiesMetadata as $property) {
         foreach ($property->getMetadataClass() as $metadataClass){
             if ( $metadataClass instanceof Translate) {
                 echo  $property->getPropertyName() . ': ' . $metadataClass->translatable . "\n";
             }
         }
    }
yaml
    services:
      // Your services here
      Beerline\PhpCustomAnnotations\Metadata\PropertyMetadataPicker:
        class: 'Beerline\PhpCustomAnnotations\Metadata\PropertyMetadataPicker'