~earboxer/bare-mess-php

5d734aa979e3b38678bc8aa8f14a560f0891446b — Zach DeCook 4 years ago 00fce28
* Data: Test constructor for primitive
3 files changed, 20 insertions(+), 2 deletions(-)

M README.md
M src/Data.php
M tests/PrimitivesTest.php
M README.md => README.md +6 -2
@@ 18,8 18,8 @@ Work in progress.
* `bool`: **Boolean** (bool)
* `enum`: Not yet implemented, use **UInt**.
* `string`: **Str** (string)
* `data<length>`: Not yet implemented.
* `data`: **Data**
* `data<length>`: extend **Data**, defining BYTES
* `data`: **Str**
* `void`: **Nothing**

All primitive types provide the following methods, which can be used as follows.


@@ 32,4 32,8 @@ $dayNumber->set(365); // Mutator
$mess = $dayNumber->mess(); // Convert into BARE message format.
$day = new U16($mess); // Constructor mutates the $mess parameter
$day->get(); // Accessor -- returns 365
class Data2 extends \BareMess\Data {public const BYTES = 2;}
$mess = $day->mess();
$data2 = new Data2($mess);
$data2->get(); // Will be equivalent to $day->mess().
```

M src/Data.php => src/Data.php +9 -0
@@ 7,6 7,15 @@ abstract class Data
    use Mutatable;
    private string $value = '';

    public function __construct(string & $mess = null)
    {
        if ($mess === null){
            return;
        }
        $this->value = substr($mess, 0, get_called_class()::BYTES);
        $mess = substr($mess, get_called_class()::BYTES);
    }

    public function set(string $value)
    {
        return $this->value = substr($value, 0, get_called_class()::BYTES);

M tests/PrimitivesTest.php => tests/PrimitivesTest.php +5 -0
@@ 49,6 49,11 @@ class PrimitivesTest extends TestCase
        $this->assertEquals($value, $pk3->set($lies));
        $this->assertNotEquals($lies, $pk3->get());
        $this->assertEquals($value, $pk3->get());

        $mess = $pk3->mess();
        $pk4 = new PublicKey($mess);
        $this->assertEquals('', $mess);
        $this->assertEquals($pk3->get(), $pk4->get());
    }

    public function testEnum()