2017 © Pedro Peláez
 

library record

Immutable validatable record

image

prewk/record

Immutable validatable record

  • Wednesday, February 28, 2018
  • by prewk
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3,403 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 21 Versions
  • 4 % Grown

The README.md

Immutable Validatable Record Build Status Coverage Status

Validatable records with an API inspired by Immutable Record (but without the memory efficiency..), (*1)

Installation

composer require prewk/record, (*2)

Simple usage

  1. Extend \Prewk\Record
  2. Define fields by implementing getFields()
  3. Define (optional) defaults by implementing getDefaults()
  4. Construct a base record
  5. Create new records from that base record
<?php
class FooRecord extends \Prewk\Record
{
    /**
     * Get record fields
     * @return string[]
     */
    protected function getFields(): array
    {
        return ["foo", "bar", "baz"];
    }

    /**
     * Get defaults
     * @return array
     */
    protected function getDefaults(): array
    {
        return ["foo" => 123, "bar" => null, "baz" => 456];
    }
}

$fooRecord = new FooRecord;

// Create a FooRecord
$record1 = $fooRecord->make(["foo" => 777, "bar" => 888]);
print_r($record1->asArray());
// -> ["foo" => 777, "bar" => 888, "baz" => 456]

// Immutibility
$record1->set("foo", "This value will disappear into the void");
print_r($record1->asArray());
// -> ["foo" => 777, "bar" => 888, "baz" => 456]

$record2 = $record1->set("foo", "This value will end up in record2");
print_r($record2->asArray());
// -> ["foo" => "Yay", "bar" => 888, "baz" => 456]

Validation

  1. Implement a validator class that extends \Prewk\Record\ValidatorInterface
  2. Define rules on your record by implementing getRules()
  3. Every mutation on your record will be routed through your validator
class MyValidator implements \Prewk\Record\ValidatorInterface
{
   /**
     * Validates a value against a rule 
     * @param mixed $value
     * @param mixed $rule
     * @return bool
     */
    public function validate($value, $rule): bool
    {
        switch ($rule) {
            case "numeric":
                return is_numeric($value);
            default:
                throw new \Exception("Invalid rule!");
        }
    }

class FooRecord extends \Prewk\Record
{
    /**
     * Get record fields
     * @return string[]
     */
    protected function getFields(): array
    {
        return ["foo", "bar", "baz"];
    }

    /**
     * Get defaults
     * @return array
     */
    protected function getDefaults(): array
    {
        return ["foo" => 123, "bar" => null, "baz" => 456];
    }

    /**
     * Get rules
     * @return array
     */
    protected function getRules(): array
    {
        return ["foo" => "numeric"];
    }
}

$fooRecord = new FooRecord(new MyValidator);

$record1 = $fooRecord->make(["foo" => 100]);
print_r($record1->asArray());
// -> ["foo" => 777, "bar" => 888, "baz" => 456]

$record2 = $fooRecord->make(["foo" => "Will throw exception"]);
// -> throws exception "Field name foo didn't validate according to its rules"

Injectable Laravel validated record

<?php
class FooRecord extends \Prewk\Record\Laravel\Record
{
    protected function getFields(): array
    {
        return ["foo", "bar"];
    }

    protected function getRules(): array
    {
        return ["foo" => "in:1,2,3", "bar" => "numeric"];
    }
}

class FooController extends BaseController
{
    private $fooRecord;

    public function __construct(FooRecord $fooRecord)
    {
        $this->fooRecord = $fooRecord;
    }

    public function create(FooRequest $request)
    {
        $record = $this->fooRecord->make($request->all());
    }
}

API

// Make a new record from an existing record
$record->make(["foo" => "bar"]);

// Make a new record from setting
$newRecord = $record->set("foo", "bar");

// Check if a field has a value (if field has a default value this returns true)
$fooIsSet = $record->has("foo");

// Merge with an array
$newRecord = $record->merge(["baz" => "qux"]);
// ..or with an existing record
$newRecord = $record->merge($record2);

License

MIT, (*3)

PHP 5

Use 1.1 for PHP versions < 7.0., (*4)

The Versions

28/02 2018

dev-v1-backports

dev-v1-backports http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

28/02 2018

1.2.1

1.2.1.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

28/02 2018

dev-master

9999999-dev http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

immutable record

28/02 2018

2.1.1

2.1.1.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

immutable record

28/02 2018

1.2.0

1.2.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

28/02 2018

2.1.0

2.1.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

immutable record

16/01 2018

2.0.1

2.0.1.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

immutable record

16/01 2018

2.0.0

2.0.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

immutable record

14/12 2017

1.1.0

1.1.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

31/08 2017

1.0.0

1.0.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

19/07 2017

0.4.0

0.4.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

13/07 2017

0.3.1

0.3.1.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

13/07 2017

0.3.0

0.3.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

08/05 2017

0.2.0

0.2.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

28/06 2016

0.1.0

0.1.0.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

15/04 2016

0.0.7

0.0.7.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

15/04 2016

0.0.6

0.0.6.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

15/04 2016

0.0.5

0.0.5.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

07/04 2016

0.0.4

0.0.4.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

06/04 2016

0.0.3

0.0.3.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record

06/04 2016

0.0.2

0.0.2.0 http://github.com/prewk/record

Immutable validatable record

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

immutable record