2017 © Pedro PelΓ‘ez
 

library koda

Utility classes for analyse code and invoking callables with verification of arguments and type casting

image

koda/koda

Utility classes for analyse code and invoking callables with verification of arguments and type casting

  • Friday, December 1, 2017
  • by bzick
  • Repository
  • 2 Watchers
  • 0 Stars
  • 6,982 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 7 Versions
  • 8 % Grown

The README.md

Koda

Build Status Coverage Status, (*1)

Koda allows to analyse code entries, call functions, methods and objects with validation of arguments and type casting. This is similar to call_user_func and call_user_func_array, but more smarter. For example calculate hypotenuse using method:, (*2)

class Math {
    /**
     * @param float $leg1 (unsigned) first cathetus of triangle
     * @param float $leg2 (unsigned) second cathetus of triangle
     * @return float
     */
    public static function hypotenuse($leg1, $leg2, $round = 2) {
        // ...
    }
}

all arguments we gets from associative (or plain) array (e.g. $_GET)., (*3)

If use classic way with call_user_func:, (*4)

if(isset($_GET['leg1'])) {
    $leg1 = floatval($_GET['leg1']);
    if($leg1 < 0) {
        throw new LogicException("leg1 should be greater than 0");
    }
} else {
    throw new RuntimeException("No leg1 parameter");
}
if(isset($_GET['leg2'])) {
    $leg2 = floatval($_GET['leg2']);
    if($leg2 < 0) {
        throw new LogicException("leg2 should be greater than 0");
    }
} else {
    throw new RuntimeException("No leg2 parameter");
}

return call_user_func('Math::hypotenuse', $leg1, $leg2);

But if use Koda we get one line of code:, (*5)

return Koda::call('Math::hypotenuse', $_GET);

Koda find all arguments from associative (or plain) array $_GET, change type, validate and invoke method., (*6)

Method call

/**
 * Method description
 * @param int $arg1 some integer value
 * @param string $arg2 some string value
 * @param float[] $arg3 array of floating point values
 * @return bool method result
 **/
public function doSomething($arg1, $arg2, array $arg3 = array()) {
    // ...
}

ΠŸΠ΅Ρ€Π΅Π΄ Π²Ρ‹Π·ΠΎΠ²ΠΎΠΌ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° систСма ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ Π½Π° Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ всСх Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² Π² запросС ΠΈ ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Ρ‚ ΠΊ Π½ΡƒΠΆΠ½ΠΎΠΌΡƒ Ρ‚ΠΈΠΏΡƒ, согласно описанию ΠΌΠ΅Ρ‚ΠΎΠ΄Π°. Π‘Ρ‹Π²Π°Π΅Ρ‚ нСдостаточно простого привСдСния Ρ‚ΠΈΠΏΠ°, ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΡ‚Ρ€Π΅Π±ΠΎΠ²Π°Ρ‚ΡŒΡΡ привинтивная ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° самих Π΄Π°Π½Π½Ρ‹Ρ…. Π’ этом случаС Π² систСмС Π΅ΡΡ‚ΡŒ Π½Π°Π±ΠΎΡ€ Π³ΠΎΡ‚ΠΎΠ²Ρ‹Ρ… ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΎΠΊ. ВсС ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ΡΡ Ρ‚Π°ΠΊ ΠΆΠ΅ Π² doc-Π±Π»ΠΎΠΊΠ΅ Π² скобках, Ρ‡Π΅Ρ€Π΅Π· Π·Π°ΠΏΡΡ‚ΡƒΡŽ сразу послС названия Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π°:, (*7)

/**
 * Method description
 * @param int $arg1 (value 0..100) Π½Π΅ΠΊΠΈΠΉ числовой Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚, значСния ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ находится ΠΌΠ΅ΠΆΠ΄Ρƒ 0 ΠΈ 100 Π²ΠΊΠ»ΡŽΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ
 * @param string $arg2 (keyword, length < 100) Π½Π΅ΠΊΠΈΠΉ стоковый Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚, Π΄Π»ΠΈΠ½Π° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ мСньшС 100 символов ΠΈ состоит ΠΈΠ· `a-z0-9-_`
 * @param float[] $arg3 (count 0..10) массив чисСл с ΠΏΠ»Π°Π²ΡƒΡŽΡ‰Π΅ΠΉ Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ, массив ΠΌΠΎΠΆΠ΅Ρ‚ ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Ρ‚ΡŒ ΠΎΡ‚ 0 Π΄ΠΎ 10 элСмСнтов
 **/
public function doSomethingAction($arg1, $arg2, array $arg3) {
    // ...
}

List of verifications (class Koda\Filter):, (*8)

  • unsigned - value is unsigned integer
  • positive - integer value greater than zero
  • negative - integer value less than zero
  • smalltext - string value less than or equal to 256 bytes
  • text - string value less than or equal to 64KiB.
  • largetext - string value less than or equal to 2MiB.
  • date [FORMAT] - string value is a date. FORMAT - the format that the passed in string should be in. FORMAT - optional parameter, by default function strtotime parse string.
  • length RANGE - specifies the maximum number of bytes allowed in the string value. Maybe range length 1..6, equality length = 6 or inequality length <=100.
  • value RANGE - числовоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΈΠΌΠ΅Π΅Ρ‚ ограничСния ΠΏΠΎ Π·Π½Π°Ρ‡Π΅Π½ΠΈΡŽ. Π˜Π½Ρ‚Π΅Ρ€Π²Π°Π» задаСтся ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΠΊ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ 1..6 Ρ‚Π°ΠΊ ΠΈ Π½Π΅ равСством <=100. НапримСр, value <100, value 6..20
  • count RANGE - массив Π·Π°Π½Ρ‡Π΅Π½ΠΈΠΉ ΠΈΠΌΠ΅Π΅Ρ‚ ограничСния ΠΏΠΎ количСству элСмСнотов. Π˜Π½Ρ‚Π΅Ρ€Π²Π°Π» задаСтся ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΠΊ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ 1..6 Ρ‚Π°ΠΊ ΠΈ Π½Π΅ равСством <=100. НапримСр, count <100, count 6..20
  • file - checks that the given string is existing regular file.
  • dir - checks that the given string is existing directory.
  • email [extended] - string value is email address. Set parameter extended that would allow the email format James Bond <agent007@dev.null>.
  • domain - checks that the given string is domain.
  • custom CALLBACK - Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½ΠΎ Π² ΡƒΠΊΠ°Π·Π°Π½Π½ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ для ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ
  • url - string value is URL
  • ip - string value is IP address
  • hex - hex string, like md5 or sha1.
  • like PATTERN - match string value against a glob pattern. Example: like *.tgz
  • mask PATTERN - строковоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ удовлСтворяСт маскС символов. НапримСр, mask a-z0-9_
  • regexp PATTERN - строковоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ удовлСтворяСт рСгулярному Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ. НапримСр, regexp /^[a-z0-9_]+$/si
  • variants SOURCE - Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ являСтся ΠΎΠ΄Π½ΠΈΠΌ ΠΈΠ· Π²Π°Ρ€ΠΈΠ°Π½Ρ‚ΠΎΠΌ ΠΈΠ· списка SOURCE. SOURCE ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΠ°ΠΊ ΠΎΠ±Ρ‹Ρ‡Π½Ρ‹ΠΌ пСрСчислСниСм Ρ‡Π΅Ρ€Π΅Π· ΠΏΡ€ΠΎΠ±Π΅Π» Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Ρ… Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° Ρ‚Π°ΠΊ ΠΈ ΠΊΠΎΠ»Π»Π±Π΅ΠΊΠΎΠΌ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ массив допустимых ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ². НапримСр, variants: one two three four
  • options CALLBACK - ассоциативный массив Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Ρ… Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ. НСобходимоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ищСтся Π² ΠΊΠ»ΡŽΡ‡Π΅ массива.
  • is [SOMETHING] - Π±ΠΎΠ»Π΅Π΅ ΠΌΠ΅Ρ‚ΠΊΠ° Ρ‡Π΅ΠΌ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ°, сама ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π½ΠΈΡ‡Π΅Π³ΠΎ Π½Π΅ Π΄Π΅Π»Π°Π΅Ρ‚ ΠΈ всСгда Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true. Однако Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ для ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎΠΉ отрисовки ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π°.

Если указанная ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π½Π΅ сущСствуСт Π² Koda\Filter Ρ‚ΠΎ для Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΠΈ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Π²Π°Π½ ΠΌΠ΅Ρ‚ΠΎΠ΄ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π»Π΅Ρ€Π° с суффиксом Validator., (*9)

/**
 * Method description
 * @param int $author (user active)
 **/
public function addPost($author) {
    // ...
}

public function userValidator($user_id, $type) {
    // check $user_id
}

БистСма Ρ‚Π°ΠΊ ΠΆΠ΅ воспринимаСт Π·Π°ΠΊΠ°Ρ‡ΠΈΠ²Π°Π΅ΠΌΡ‹Π΅ Ρ„Π°ΠΉΠ»Ρ‹, Π² этом случаС Ρ‚ΠΈΠΏ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ splFileInfo:, (*10)

/**
 * ОписаниС ΠΌΠ΅Ρ‚ΠΎΠ΄Π°
 * @param splFileInfo $file Ρ„Π°ΠΉΠ»Ρ‹, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄ΡƒΡ‚ Π·Π°ΠΊΠ°Ρ‡ΠΈΠ½Ρ‹ Π½Π° сСрвСр
 **/
public function doSomethingAction(splFileInfo $file) {
    // ...
}

Create object

Cache

RPC example

The Versions

01/12 2017

dev-develop

dev-develop

Utility classes for analyse code and invoking callables with verification of arguments and type casting

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Ivan Shalganov

validation cast reflection call call_user_func invoker call_user_func_array analyse

30/10 2017

dev-master

9999999-dev

Utility classes for analyse code and invoking callables with verification of arguments and type casting

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Ivan Shalganov

validation cast reflection call call_user_func invoke call_user_func_array analyse

20/09 2017

2.0.2

2.0.2.0

Utility classes for analyse code and invoking callables with verification of arguments and type casting

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Ivan Shalganov

validation cast reflection call call_user_func invoke call_user_func_array analyse

14/09 2017

2.0.1

2.0.1.0

Utility classes for analyse code and invoking callables with verification of arguments and type casting

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Ivan Shalganov

validation cast reflection call call_user_func invoke call_user_func_array analyse

11/09 2017

2.0.0

2.0.0.0

Utility classes for analyse code and invoking callables with verification of arguments and type casting

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Ivan Shalganov

validation cast reflection call call_user_func invoke call_user_func_array analyse

21/06 2016

1.1.0

1.1.0.0

Utility classes for analyse code and invoking callables with verification of arguments and type casting

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Ivan Shalganov

validation cast reflection call call_user_func invoke call_user_func_array analyse

23/12 2015

1.0.0

1.0.0.0

Utility classes for analyse code and invoking callables with verification of arguments and type casting

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Ivan Shalganov

validation cast reflection call call_user_func invoke call_user_func_array analyse