2017 © Pedro PelĂĄez
 

project scalar

Lightweight scalar-type container

image

granam/scalar

Lightweight scalar-type container

  • Tuesday, July 10, 2018
  • by jaroslavtyc
  • Repository
  • 1 Watchers
  • 1 Stars
  • 12,050 Installations
  • PHP
  • 11 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 18 Versions
  • 7 % Grown

The README.md

Wrapping object for scalar (or null) only

Build Status, (*1)

First of all, isn't sufficient PHP native scalar type hinting for you? It is available since PHP 7.0., (*2)

Sadly there is no native function able to cast or sanitize a value to scalar with warning on value lost., (*3)

For that reason, if we want to be sure about scalar type, a scalar converter and optionally a type-checking class are the only chance., (*4)

Warning: The converter and so the wrapper class do not cast null - null remains null., (*5)

<?php
namespace Granam\Scalar;

use Granam\Scalar\Tools\ToScalar;

$scalar = new Scalar('foo');

// foo
echo $scalar;

$nullScalar = new Scalar(null);
// false
echo is_scalar($nullScalar->getValue());
// true
echo $nullScalar->getValue() === null;

// NULL
var_dump(ToScalar::toScalar(null));

try {
  ToScalar::toScalar(null, true /* explicitly strict */);
} catch (Tools\Exceptions\WrongParameterType $scalarException) {
  // Something get wrong: Expected scalar or object with __toString method on strict mode, got NULL.
  die('Something get wrong: ' . $scalarException->getMessage());
}

The evil null

Why the NULL remains NULL by default?, (*6)

Because it does lesser harm. Forcing an unknown value to has a specific type causes loss of previous "I do not know what it is" information., (*7)

If you needs scalar only, without NULL, you will have to do it by yourself by native PHP casting:, (*8)

$variable = null;
$variable = (string)$variable;

So it is only up to you what type will the NULL become., (*9)

The Versions

10/07 2018

4.0.x-dev

4.0.9999999.9999999-dev https://github.com/jaroslavtyc/granam-scalar

Lightweight scalar-type container

  Sources   Download

MIT

The Requires

 

The Development Requires

php scalar

10/07 2018

3.0.x-dev

3.0.9999999.9999999-dev https://github.com/jaroslavtyc/granam-scalar

Lightweight scalar-type container

  Sources   Download

MIT

The Requires

 

The Development Requires

php scalar

15/02 2017
30/12 2016

1.1.x-dev

1.1.9999999.9999999-dev https://github.com/jaroslavtyc/granam-scalar

Lightweight scalar-type container

  Sources   Download

MIT

The Requires

 

The Development Requires

php scalar

30/12 2016
08/09 2015
02/09 2015
02/09 2015
17/07 2015

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/jaroslavtyc/granam-scalar

Lightweight scalar-type container

  Sources   Download

MIT

The Requires

 

The Development Requires

php scalar

17/07 2015
23/06 2015