Skip to content
forked from azjezz/typed

Typed variables for PHP 7.4+

License

Notifications You must be signed in to change notification settings

ryanwcare2/typed

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

Typed

This libaray allows you to create typed variables in PHP 7.4 +

example :

<?php declare(strict_types=1);

$name = &string('saif');
$age = &int(19);

try {

  $name = 'azjezz'; // works
  $name = 15; // fails
  $age = null; // fails

} catch(TypeError $error) {
   
}

// based on the passed variable type
$string = &typed('saif');
$traversable = &traversable(new ArrayObject());
$countable = &countable(new ArrayObject());
$func = &func('strtolower');
$object = &object((object) []);
$stdClass = &stdClass((object) []);
$iterable = &iterable([]);
$bool = &bool(false);
$bool = &boolean(true);
$set = &set(['a', 'b', 'b', 'a']); // ['a', 'b']
$vector = &vector(['a' => 1, 'b' => 2]); // [1, 2]
$array = &arr(['a' => 1 , 'b' => 2]); // ['a' => 1, 'b' => 2]
$keyset = &keyset(['a' => 1, 'b' => 2]); // ['a', 'b']
$int = &integer(5);
$int = &int(48);
$string = &string('f');
$float = &float(1.56);

callable => func, array => { arr, keyset, vector, set }

Since callable and array cant be used for function names, instead i have used func for callable and arr for array. keyset, vector and set are just helper to help you create an array of keys ( keyset = arr(array_keys($value)) ), an array of values ( vector = arr(array_values($value)) ) and an array of unique values ( set = arr(array_unique($value)) ).

typed

typed() is a function to help you create a typed variables based on the type of the passed variable, currently it supports string, int, float, bool, object and iterable. it was suggected by @mikesherov on twitter.


check this twitter thread.


Compoer installation :

soon

TODO :

  • add nullable* functions.

About

Typed variables for PHP 7.4+

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%