Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
/ type Public archive
generated from spaceonfire/skeleton

Commit

Permalink
Merge pull request #3 from spaceonfire/scalar-types
Browse files Browse the repository at this point in the history
Scalar types
  • Loading branch information
hustlahusky authored Oct 4, 2020
2 parents 01d1f7f + cdf616c commit 245bde0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Nothing
-->

## [1.1.0] - 2020-10-04
### Added
- Support non strict mode for all scalar types (int, float, string and bool)
- Force return `null` when casting to null builtin type

## [1.0.1] - 2020-09-26
### Fixed
- Development config updates
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# type
# Type

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Total Downloads][ico-downloads]][link-downloads]
[![Code Coverage][ico-coverage]][link-actions]
[![Build Status][ico-build-status]][link-actions]

Collection of value objects provides ability of type checking in runtime.
Collection of objects that provides ability of checking value types.

## Install

Expand All @@ -26,8 +26,9 @@ $int = new BuiltinType(BuiltinType::INT);
Assert::true($int->check(1));
Assert::false($int->check('1'));

$intNoString = new BuiltinType(BuiltinType::INT, false);
Assert::true($int->check('1'));
$intNonStrict = new BuiltinType(BuiltinType::INT, false);
Assert::true($intNonStrict->check('1'));
Assert::same(1, $intNonStrict->cast('1'));
```

## Change log
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "spaceonfire/type",
"type": "library",
"description": "Collection of value objects provides ability of type checking in runtime",
"description": "Collection of objects that provides ability of checking value types",
"keywords": [
"spaceonfire",
"type"
Expand Down Expand Up @@ -45,7 +45,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "1.1-dev"
}
},
"config": {
Expand Down
23 changes: 17 additions & 6 deletions src/BuiltinType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ final class BuiltinType implements Type
public const CALLABLE = 'callable';
public const ITERABLE = 'iterable';

private const SUPPORT_NO_STRICT = [
self::INT => true,
self::FLOAT => true,
self::STRING => true,
public const SCALAR_TYPES = [
self::INT => self::INT,
self::FLOAT => self::FLOAT,
self::STRING => self::STRING,
self::BOOL => self::BOOL,
];

/**
Expand All @@ -48,7 +49,7 @@ public function __construct(string $type, bool $strict = true)

$this->type = self::prepareType($type);

if ($strict === false && !isset(self::SUPPORT_NO_STRICT[$this->type])) {
if ($strict === false && !isset(self::SCALAR_TYPES[$this->type])) {
$strict = true;
trigger_error(sprintf('Type "%s" cannot be non-strict. $strict argument overridden.', $this->type));
}
Expand Down Expand Up @@ -90,7 +91,11 @@ public function check($value): bool
break;

case self::BOOL:
Assert::boolean($value);
if ($this->strict) {
Assert::boolean($value);
} else {
Assert::scalar($value);
}
break;

case self::RESOURCE:
Expand Down Expand Up @@ -141,6 +146,12 @@ public function cast($value)
case self::STRING:
return (string)$value;

case self::BOOL:
return (bool)$value;

case self::NULL:
return null;

default:
return $value;
}
Expand Down
32 changes: 27 additions & 5 deletions tests/BuiltinTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ public function __toString(): string
}));
self::assertFalse($string->check(null));
self::assertFalse($string->check([]));

$bool = BuiltinType::create('bool', false);
self::assertTrue($bool->check(true));
self::assertTrue($bool->check(false));
self::assertTrue($bool->check(1));
self::assertTrue($bool->check(''));
self::assertFalse($bool->check([]));
self::assertFalse($bool->check((object)[]));
self::assertFalse($bool->check(null));
}

public function testNonStrictNotice(): void
Expand All @@ -124,26 +133,39 @@ public function testNonStrictNotice(): void
public function testCast(): void
{
$integer = BuiltinType::create('integer');
self::assertEquals(1, $integer->cast('1'));
self::assertSame(1, $integer->cast('1'));

$float = BuiltinType::create('float');
self::assertEquals(1.0, $float->cast('1.0'));
self::assertSame(1.0, $float->cast('1.0'));

$string = BuiltinType::create('string');
self::assertEquals('lorem ipsum', $string->cast(new class {
self::assertSame('lorem ipsum', $string->cast(new class {
public function __toString(): string
{
return 'lorem ipsum';
}
}));

$bool = BuiltinType::create('bool');
self::assertEquals('1', $bool->cast('1'));
self::assertTrue($bool->cast(true));
self::assertTrue($bool->cast('1'));
self::assertTrue($bool->cast(1));
self::assertTrue($bool->cast(-2));
self::assertFalse($bool->cast(false));
self::assertFalse($bool->cast(''));
self::assertFalse($bool->cast(0));
self::assertFalse($bool->cast(-0.0));

$null = BuiltinType::create('null');
self::assertNull($null->cast(1));

$noCast = BuiltinType::create('resource');
self::assertSame(1, $noCast->cast(1));
}

public function testStringify(): void
{
$type = BuiltinType::create('integer');
self::assertEquals('int', (string)$type);
self::assertSame('int', (string)$type);
}
}

0 comments on commit 245bde0

Please sign in to comment.