2017 © Pedro Peláez
 

library simple-annotations

Simple annotation parser

image

zonuexe/simple-annotations

Simple annotation parser

  • Friday, August 19, 2016
  • by zonuexe
  • Repository
  • 1 Watchers
  • 1 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 19 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

PHP Simple Annotations

Installation

Get composer and learn to use it., (*1)

Library is on packagist., (*2)

If you refuse to use composer then instead of include_once "vendor/autoload.php" use include_once "src/DocBlockReader/Reader.php"., (*3)

Test

You need PHPUnit. After you get it run:, (*4)

> git clone https://github.com/jan-swiecki/php-simple-annotations
> cd php-simple-annotations
> composer install
> phpunit

API

  • $reader = new \DocBlockReader\Reader(String $className), (*5)

    Initialize DocBlock Reader on class $className, (*6)

  • $reader = new \DocBlockReader\Reader(String $className, String $methodName), (*7)

    Initialize DocBlock Reader on method $className::$methodName, (*8)

  • $reader->getParameter(String $key), (*9)

    Returns DocBlock value of parameter $key. E.g., (*10)

    <?php
    class MyClass
    {
     /**
      * @awesomeVariable "I am a string"
      */
     public function fn()
     {
    
     }
    }
    

    then $reader->getParameter("awesomeVariable") will return string I am a string (without quotes)., (*11)

  • $reader->getParameters(), (*12)

    returns array of all parameters (see examples below)., (*13)

  • $reader->getVariableDeclarations(), (*14)

    See last example., (*15)

Examples

Examples based on ReaderTest.php., (*16)

Note: DocBlock Reader converts type of values basing on the context (see below)., (*17)

Type conversion example

<?php

include_once "vendor/autoload.php";

class MyClass
{
    /**
     * @var0 1.5
     * @var1 1
     * @var2 "123"
     * @var3 abc
     * @var4 ["a", "b"]
     * @var5 {"x": "y"}
     * @var6 {"x": {"y": "z"}}
     * @var7 {"x": {"y": ["z", "p"]}}
     *
     * @var8
     * @var9 null
     *
     * @var10 true
     * @var11 tRuE
     * @var12 false
     * @var13 null
     * 
     */
    private function MyMethod()
    {
    }
};

$reader = new DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

will print, (*18)

array (size=14)
  'var0' => float 1.5
  'var1' => int 1
  'var2' => string '123' (length=3)
  'var3' => string 'abc' (length=3)
  'var4' => 
    array (size=2)
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
  'var5' => 
    array (size=1)
      'x' => string 'y' (length=1)
  'var6' => 
    array (size=1)
      'x' => 
        array (size=1)
          'y' => string 'z' (length=1)
  'var7' => 
    array (size=1)
      'x' => 
        array (size=1)
          'y' => 
            array (size=2)
              0 => string 'z' (length=1)
              1 => string 'p' (length=1)
  'var8' => boolean true
  'var9' => null
  'var10' => boolean true
  'var11' => string 'tRuE' (length=4)
  'var12' => boolean false
  'var13' => null

Multi value example

<?php

include_once "vendor/autoload.php";

class MyClass
{
    /**
     * @var x
     * @var2 1024
     * @param string x
     * @param integer y
     * @param array z
     */
    private function MyMethod()
    {
    }
};

$reader = new DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

will print, (*19)

array (size=3)
  'var' => string 'x' (length=1)
  'var2' => int 1024
  'param' => 
    array (size=3)
      0 => string 'string x' (length=8)
      1 => string 'integer y' (length=9)
      2 => string 'array z' (length=7)

Variables on the same line

<?php

include_once "vendor/autoload.php";

class MyClass
{
    /**
     * @get @post
     * @ajax
     * @postParam x @postParam y
     * @postParam z
     */
    private function MyMethod()
    {
    }
};

$reader = new DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

will print, (*20)

array (size=4)
  'get' => boolean true
  'post' => boolean true
  'ajax' => boolean true
  'postParam' => 
    array (size=3)
      0 => string 'x' (length=1)
      1 => string 'y' (length=1)
      2 => string 'z' (length=1)

Variable declarations functionality example

I found below functionality useful for filtering $_GET/$_POST data in CodeIgniter. Hopefully I will soon release my CodeIgniter's modification., (*21)

<?php

include_once "vendor/autoload.php";

class MyClass
{
    /**
     * @param string var1
     * @param integer var2
     */
    private function MyMethod()
    {
    }
};

$reader = new DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getVariableDeclarations("param"));

will print, (*22)

array (size=2)
  0 => 
    array (size=2)
      'type' => string 'string' (length=6)
      'name' => string 'var1' (length=4)
  1 => 
    array (size=2)
      'type' => string 'integer' (length=7)
      'name' => string 'var2' (length=4)

The Versions

19/08 2016

dev-master

9999999-dev https://github.com/zonuexe/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >= 5.4.0

 

The Development Requires

by Jan Święcki

parser docblock annotations annotation

19/08 2016

0.3.1

0.3.1.0 https://github.com/zonuexe/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >= 5.4.0

 

The Development Requires

by Jan Święcki

parser docblock annotations annotation

26/05 2015

0.3.0

0.3.0.0 https://github.com/zonuexe/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >= 5.4.0

 

The Development Requires

by Jan Święcki

parser docblock annotations annotation

26/05 2015

dev-move-namespace-psr4

dev-move-namespace-psr4 https://github.com/jan-swiecki/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >= 5.3.0

 

The Development Requires

by Jan Święcki

parser docblock annotations annotation

15/02 2014

0.2.1

0.2.1.0 https://github.com/jan-swiecki/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Jan Święcki

parser docblock annotations annotation

15/02 2014

0.2.0

0.2.0.0 https://github.com/jan-swiecki/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Jan Święcki

parser docblock annotations annotation

13/04 2013

0.1.8

0.1.8.0 https://github.com/jan-swiecki/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Jan Święcki

parser docblock annotation

13/04 2013

0.1.7

0.1.7.0 https://github.com/jan-swiecki/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Jan Święcki

parser docblock annotation

13/04 2013

0.1.6

0.1.6.0 https://github.com/jan-swiecki/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Jan Święcki

parser docblock annotation

13/04 2013

0.1.5

0.1.5.0 https://github.com/jan-swiecki/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Jan Święcki

parser docblock annotation

13/04 2013

0.1.4

0.1.4.0 https://github.com/jan-swiecki/php-simple-annotations

Simple annotation parser

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Jan Święcki

parser docblock annotation

13/04 2013

0.1.1

0.1.1.0

  Sources   Download

The Requires

  • php >=5.3.0