Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rikudousage/scalar-objects


https://github.com/rikudousage/scalar-objects

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

# Scalar types as objects for php

This php library requires php extension from .

## Installation

Run `composer require rikudou/scalar-objects`.

## Usage

The handlers are automatically registered when you include composer
autoload. That means you can use it immediately with no configuration.

Since IDEs don't understand the syntax, there are typehint classes
available to help you.

## Examples
```php
isInt()); // bool(true)
var_dump($num2->isString()); // bool(true)
var_dump($num2->isNumeric()); // bool(true)
var_dump($num2->isNumber()); // bool(false) - isNumber() returns true only for int and float

var_dump($num1->toString()->length()); // int(1)

/** @var StringTypehint $string */
$string = "This is a test string";

var_dump($string->length()); // int(21);
var_dump($string->capitalize()); // string(21) "This Is A Test String"
var_dump($string->caseInsensitiveCompare("this Is a TESt STRInG")); // int(0)
var_dump($string->toUpper()); // string(21) "THIS IS A TEST STRING"
var_dump($string->toLower()); //string(21) "this is a test string"

```

As you can see, you can use the scalar values as objects.

But you can still use them as regular scalar types.

```php
abs()); // int(15)
var_dump($int1 + $int2); // int(-5)
var_dump($int1 + $int2 + $int3->toInt()->abs()); // int(10)

```