~earboxer/bare-mess-php

3c6550f0ecc7a9878128a73b3d7462b708a89a45 — Zach DeCook 4 years ago 5d734aa
* Struct: Add more features
4 files changed, 76 insertions(+), 3 deletions(-)

M README.md
A example/Employee.php
M src/Struct.php
M tests/StructTest.php
M README.md => README.md +11 -2
@@ 16,9 16,9 @@ Work in progress.
* `i8`, `i16`, `i32`, `i64`: **I8**, **I16**, **I32**, **I64**
* `f32`, `f64`: Not yet implemented.
* `bool`: **Boolean** (bool)
* `enum`: Not yet implemented, use **UInt**.
* `enum`: extend **Uint**, defining constants. (e.g. `example/Department.php`)
* `string`: **Str** (string)
* `data<length>`: extend **Data**, defining BYTES
* `data<length>`: extend **Data**, defining BYTES. (e.g. `example/PublicKey.php`)
* `data`: **Str**
* `void`: **Nothing**



@@ 37,3 37,12 @@ $mess = $day->mess();
$data2 = new Data2($mess);
$data2->get(); // Will be equivalent to $day->mess().
```

## Aggregates

* `optional<type>`: Not completely implemented.
* `[length]type`: Not yet implemented.
* `[]type`: Not completely implemented.
* `map[type A]type B`: Not completely implemented.
* `(type | type | ...)`: Not yet implemented.
* `struct`: WIP, extend **Struct**. (e.g. `example/Employee.php`)

A example/Employee.php => example/Employee.php +25 -0
@@ 0,0 1,25 @@
<?php

namespace BareMess\Example;

use BareMess\Struct;

class Employee extends Struct
{
    public string $name = '';
    public string $email = '';
    public Address $address;
    public Department $_department;
    public Time $_hireDate;
    public ?PublicKey $_publicKey = null;
    public array $metadata = ['string' => 'string'];

    public function __construct($mess = null)
    {
        $this->address = new Address();
        $this->_department = new Department();
        $this->_hireDate = new Time();
        $this->_publicKey = new PublicKey();
        parent::__construct($mess);
    }
}

M src/Struct.php => src/Struct.php +24 -0
@@ 4,6 4,15 @@ namespace BareMess;

class Struct
{
    public function __construct($mess = null)
    {
        if ($mess !== null) {
            $vars = get_class_vars(get_class($this));
            foreach ($vars as $attributeName => $startingValue) {
                $this->$attributeName = Bare::construct($mess, $startingValue);
            }
        }
    }
    public function mess()
    {
        $mess = '';


@@ 13,4 22,19 @@ class Struct
        }
        return $mess;
    }

    public function __get(string $name)
    {
        if (is_object($this->{"_$name"})) {
            return $this->{"_$name"}->get();
        }
        return $this->$name;
    }
    public function __set(string $name, $value)
    {
        if (is_object($this->{"_$name"})) {
            return $this->{"_$name"}->set($value);
        }
        return $this->$name = $value;
    }
}

M tests/StructTest.php => tests/StructTest.php +16 -1
@@ 3,18 3,33 @@
namespace BareMess\Tests;

use BareMess\Example\Address;
use BareMess\Example\Employee;
use BareMess\Struct;
use PHPUnit\Framework\TestCase;

class StructTest extends TestCase
{
    public function testAttributes()
    {
        $customer = new Employee();
        $customer->name = "Bobbin Penny";
        $customer->email = "take.early@retirement.example";
        $customer->address->address[0] = "123 Don't Be Messy Ave.";
        $customer->address->city = "Punta Gorda";
        $customer->address->state = "FL";
        $customer->address->country = "USA";
        $customer->hireDate = "";
        $customer->metadata["occupation"] = "shell collector";
        $expected = "\x0CBobbin Penny\x1Dtake.early@retirement.example";
    }

    public function testMess()
    {
        $emptyStruct = new Struct();
        $this->assertEquals('', $emptyStruct->mess());

        $address = new Address();
        $address->address[0] = ["123 Easy Street"];
        $address->address[0] = "123 Easy Street";
        $address->city = "Nashville";
        $address->state = "TN";
        $address->country = "USA";