Skip to content

Commit

Permalink
Partially implement Field element functionality, introduce autoloading
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrepp committed Jul 24, 2018
1 parent 954c0c8 commit f37c257
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/coverage/
/vendor/
/coverage.json
/composer.phar
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"autoload": {
"psr-4": {
"Eno\\": "src/elements/"
}
},
"name": "eno-lang/enophp",
"description": "PHP implementation of the eno library specification",
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions spec/elements/field.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

use Eno\Field;

describe('Field', function() {
given('_context', function() { return []; });
given('instruction', function() { return [ 'name' => 'language', 'value' => 'eno' ]; });
given('instruction_named_valueless', function() { return [ 'name' => 'language', 'value' => null ]; });
given('instruction_unnamed_value', function() { return [ 'name' => null, 'value' => 'eno' ]; });
given('instruction_unnamed_long_value', function() { return [ 'name' => null, 'value' => 'The language is eno' ]; });
given('instruction_void', function() { return [ 'name' => null, 'value' => null ]; });
given('parent', function() { return []; });

beforeEach(function() {
$this->field = new Field($this->_context, $this->instruction, $this->parent);
});

it('is untouched after initialization', function() {
expect($this->field->touched)->toBe(false);
});

describe('isEmpty()', function() {
describe('when not empty', function() {
it('returns false', function() {
expect($this->field->isEmpty())->toBe(false);
});
});

describe('when empty', function() {
it('returns true', function() {
$named_empty_field = new Field($this->_context, $this->instruction_named_valueless, $this->parent);
expect($named_empty_field->isEmpty())->toBe(true);
});
});
});

describe('raw()', function() {
describe('with a name and a value', function() {
it('returns a native representation', function() {
expect($this->field->raw())->toEqual([ 'language' => 'eno' ]);
});
});

describe('with an unnamed value', function() {
it('returns a native representation', function() {
$unnamed_field = new Field($this->_context, $this->instruction_unnamed_value, $this->parent);
expect($unnamed_field->raw())->toEqual('eno');
});
});
});

describe('__toString()', function() {
describe('with a name and a value', function() {
it('returns a debug abstraction', function() {
expect((string)$this->field)->toEqual('[Field name="language" value="eno"]');
});
});

describe('with a name and no value', function() {
it('returns a debug abstraction', function() {
$named_empty_field = new Field($this->_context, $this->instruction_named_valueless, $this->parent);
expect((string)$named_empty_field)->toEqual('[Field name="language" value=null]');
});
});

describe('with an unnamed value', function() {
it('returns a debug abstraction', function() {
$unnamed_field = new Field($this->_context, $this->instruction_unnamed_value, $this->parent);
expect((string)$unnamed_field)->toEqual('[Field value="eno"]');
});
});

describe('with no name and value', function() {
it('returns a debug abstraction', function() {
$void_field = new Field($this->_context, $this->instruction_void, $this->parent);
expect((string)$void_field)->toEqual('[Field value=null]');
});
});

describe('with no name and a long value', function() {
it('returns a debug abstraction with a truncated value', function() {
$unnamed_long_value_field = new Field($this->_context, $this->instruction_unnamed_long_value, $this->parent);
expect((string)$unnamed_long_value_field)->toEqual('[Field value="The languag..."]');
});
});
});
});

?>
62 changes: 62 additions & 0 deletions src/elements/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Eno;

class Field {
public $touched;
private $value = null;

function __construct(&$context, &$instruction, &$parent, $from_empty = false) {
$this->context = $context;
$this->instruction = $instruction;
$this->name = $instruction['name'];
$this->parent = $parent;
$this->value = $instruction['value'];
$this->touched = false;

if($from_empty)
return;

$instruction['element'] = $this;

// ... TODO
}

public function __toString() {
$value = $this->value;

if($value === null) {
$value = 'null';
} else {
$value = str_replace("\n", '\n', $value);
if(strlen($value) > 14) {
$value = substr($value, 0, 11) . '...';
}
$value = "\"{$value}\"";
}

if($this->name === null) {
return "[Field value={$value}]";
} else {
return "[Field name=\"{$this->name}\" value={$value}]";
}
}

function isEmpty() {
return $this->value === null;
}

function raw() {
if($this->name === null) {
return $this->value;
} else {
return [ $this->name => $this->value ];
}
}

function touch() {
$this->touched = true;
}
}

?>

0 comments on commit f37c257

Please sign in to comment.