~earboxer/bare-mess-php

f4de640cb1a7faff065ac007413fcf90acb3b589 — Zach DeCook 3 years ago 05f838d
* Primitives: Make Boolean class which is a bool
2 files changed, 26 insertions(+), 4 deletions(-)

M src/Bare.php
A src/Boolean.php
M src/Bare.php => src/Bare.php +2 -4
@@ 8,9 8,7 @@ class Bare
    {
        switch(gettype($startingValue)) {
            case 'boolean': // bool
                $val = $mess[0] !== "\0";
                $mess = substr($mess, 1);
                return $val;
                return (new Boolean($mess))->get();
            case 'string': // string
                return (new Str($mess))->get();
            case 'integer': // int


@@ 26,7 24,7 @@ class Bare
                // Note: This isn't the void type.
                return chr(0);
            case 'boolean': // bool
                return chr($value);
                return Boolean::fromValue($value)->mess();
            case 'string': // string
                return Str::fromValue($value)->mess();
            case 'integer': // int

A src/Boolean.php => src/Boolean.php +24 -0
@@ 0,0 1,24 @@
<?php

namespace BareMess;

// We can't use "Bool" as the name of this class as it's reserved by PHP.
// Note: Bare::mess and Bare::construct will encode a PHP bool as a \BareMess\Boolean.
class Boolean
{
    use Mutatable;
    private bool $value = false;
    public function __construct(& $mess = null)
    {
        if ($mess === null){
            return;
        }
        $this->value = $mess[0] !== "\0";
        $mess = substr($mess, 1);
    }

    public function mess()
    {
        return $this->value ? chr(1) : chr(0);
    }
}