-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partially implement Field element functionality, introduce autoloading
- Loading branch information
Showing
5 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/coverage/ | ||
/vendor/ | ||
/coverage.json | ||
/composer.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..."]'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
?> |