frostbane/php-simple-annotations

Simple annotation parser

0.5.2 2019-11-15 06:42 UTC

This package is not auto-updated.

Last update: 2024-04-23 02:27:19 UTC


README

Installation

Get composer and learn to use it.

Library is on packagist.

If you refuse to use composer then instead of include_once "vendor/autoload.php" use include_once "src/DocBlockReader/Reader.php".

Test

You need PHPUnit. After you get it run:

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

Introduction

This library gives you the ability to extract and auto-parse DocBlock comment blocks.

Example:

    class TestClass {
      /**
       * @x 1
       * @y yes!
       */
      private $myVar;
    }

    $reader = new \frostbane\DocBlockReader\Reader('TestClass', 'myVar', 'property');
    $x = $reader->getParameter("x"); // 1 (with number type)
    $y = $reader->getParameter("y"); // "yes!" (with string type)

So as you can see to do this you need to construct Reader object and target it at something. Then you extract data.

You can point at classes, class methods and class properties.

  • Targeting class: $reader = new \frostbane\DocBlockReader\Reader(String $className)
  • Targeting method or property: $reader = new \frostbane\DocBlockReader\Reader(String $className, String $name [, String $type = 'method'])

This will initialize DocBlock Reader on method $className::$name or property $className::$name.

To choose method use only two arguments or provide third argument as method string value. To get property value put property string value in third argument.

To extract parsed properties you have two methods:

  • $reader->getParameter(String $key)

Returns DocBlock value of parameter $key. E.g.

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

     }
 }

then

 $reader = new \frostbane\DocBlockReader\Reader('MyClass', 'fn');
 $reader->getParameter("awesomeVariable")

will return string I am a string (without quotes).

  • $reader->getParameters()

returns array of all parameters (see examples below).

API

  • Constructor $reader = new \frostbane\DocBlockReader\Reader(String $className [, String $name [, String $type = 'method'] ])

    Creates Reader pointing at class, class method or class property - based on provided arguments (see Introduction).

  • $reader->getParameter(String $key)

Returns value of parameter $key extracted from DocBlock.

  • $reader->getParameters()

returns array of all parameters (see examples below).

  • $reader->getVariableDeclarations() - See last example below.

Examples

Examples based on ReaderTest.php.

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

Type conversion example

<?php

include_once "../vendor/autoload.php";

class MyClass
{
    /**
     * @float_0-0         0.0
     * @float_1-5         1.5
     * @int_1             1
     * @int_0             0
     * @string_2-3 "2.3"
     * @string_1   "1"
     * @string_0   "0"
     * @string_0-0 "0.0"
     * @string_123 "123"
     * @string_4-5 "4.5"
     *
     * @string_abc        abc
     * @string_def  "def"
     *
     * @array1 ["a", "b"]
     * @obj1 {"x": "y"}
     * @obj2 {"x": {"y": "z"}}
     * @obj_array1 {"x": {"y": ["z", "p"]}}
     *
     * @empty1
     * @null1             null
     * @string_null "null"
     *
     * @bool_true         true
     * @bool_false        false
     *
     * @string_tRuE       tRuE
     * @string_fAlSe      fAlSe
     * @string_true  "true"
     * @string_false "false"
     *
     */
    private function MyMethod()
    {
    }
}

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

var_dump($reader->getParameters());

will print

array (size=25)
  'float_0-0' =>  0
  'float_1-5' =>  1.5
  'int_1' =>  1
  'int_0' =>  0
  'string_2-3' =>  '2.3' (length=3)
  'string_1' =>  '1' (length=1)
  'string_0' =>  '0' (length=1)
  'string_0-0' =>  '0.0' (length=3)
  'string_123' =>  '123' (length=3)
  'string_4-5' =>  '4.5' (length=3)
  'string_abc' =>  'abc' (length=3)
  'string_def' =>  'def' (length=3)
  'array1' =>
    array (size=2)
      0 =>  'a' (length=1)
      1 =>  'b' (length=1)
  'obj1' =>
    array (size=1)
      'x' =>  'y' (length=1)
  'obj2' =>
    array (size=1)
      'x' =>
        array (size=1)
          'y' =>  'z' (length=1)
  'obj_array1' =>
    array (size=1)
      'x' =>
        array (size=1)
          'y' =>
            array (size=2)
              ...
  'empty1' =>  true
  'null1' => null
  'string_null' =>  'null' (length=4)
  'bool_true' =>  true
  'bool_false' =>  false
  'string_tRuE' =>  'tRuE' (length=4)
  'string_fAlSe' =>  'fAlSe' (length=5)
  'string_true' =>  'true' (length=4)
  'string_false' =>  'false' (length=5)

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 \frostbane\DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

will print

array (size=3)
  'var' =>  'x' (length=1)
  'var2' =>  1024
  'param' =>
    array (size=3)
      0 =>  'string x' (length=8)
      1 =>  'integer y' (length=9)
      2 =>  '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 \frostbane\DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

will print

array (size=4)
  'get' =>  true
  'post' =>  true
  'ajax' =>  true
  'postParam' =>
    array (size=3)
      0 =>  'x' (length=1)
      1 =>  'y' (length=1)
      2 =>  '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.

<?php

include_once "vendor/autoload.php";

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

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

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

will print

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