Skip to content

Commit

Permalink
Fix Model/Property Validation (#7)
Browse files Browse the repository at this point in the history
Fix Model/Property Validation
  • Loading branch information
mcaskill authored Nov 4, 2019
2 parents 4992196 + a4e67bb commit b31cf49
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 27 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "0.4.x-dev"
"dev-master": "0.5.x-dev"
}
},
"require": {
Expand All @@ -32,7 +32,7 @@
"locomotivemtl/charcoal-cache": "~0.1",
"locomotivemtl/charcoal-config": "~0.9",
"locomotivemtl/charcoal-factory": "~0.4",
"locomotivemtl/charcoal-property": "~0.7",
"locomotivemtl/charcoal-property": "~0.9",
"locomotivemtl/charcoal-view": "~0.3"
},
"require-dev": {
Expand Down
25 changes: 15 additions & 10 deletions src/Charcoal/Model/ModelValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

// From 'charcoal-core'
use Charcoal\Validator\AbstractValidator;
use Charcoal\Validator\ValidatableInterface;

/**
*
* Model Validator
*/
class ModelValidator extends AbstractValidator
{
Expand All @@ -15,23 +16,27 @@ class ModelValidator extends AbstractValidator
*/
public function validate()
{
$model = $this->model;
$result = true;

$model = $this->model;
$props = $model->properties();

$ret = true;
foreach ($props as $ident => $p) {
if (!$p || !$p->active()) {
foreach ($props as $ident => $prop) {
if (!($prop instanceof ValidatableInterface) || !$prop['active']) {
continue;
}
$valid = $p->validate();
if ($valid === false) {
$validator = $p->validator();

$value = $model->propertyValue($ident);
$isValid = $prop->setVal($value)->validate();
$prop->clearVal();

if ($isValid === false) {
$validator = $prop->validator();
$this->merge($validator, $ident);
$ret = false;
$result = false;
}
}

return $ret;
return $result;
}
}
53 changes: 40 additions & 13 deletions src/Charcoal/Validator/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
*/
abstract class AbstractValidator implements ValidatorInterface
{
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';

/**
* @var ValidatableInterface
*/
Expand All @@ -30,6 +26,13 @@ abstract class AbstractValidator implements ValidatorInterface
*/
private $results = [];

/**
* Holds a list of all camelized strings.
*
* @var string[]
*/
protected static $camelCache = [];

/**
* @param ValidatableInterface $model The object to validate.
*/
Expand Down Expand Up @@ -150,24 +153,48 @@ public function noticeResults()
}

/**
* @param ValidatorInterface $v The validator to merge.
* @param string $ident_prefix Optional identigfier prefix.
* @param ValidatorInterface $v The validator to merge.
* @param string|null $prefix Optional key to prefix onto identifiers.
* @return self
*/
public function merge(ValidatorInterface $v, $ident_prefix = null)
public function merge(ValidatorInterface $v, $prefix = null)
{
$results = $v->results();
foreach ($results as $level => $levelResults) {
foreach ($levelResults as $r) {
if ($ident_prefix !== null) {
$r->set_ident($ident_prefix.'.'.$r->ident());
$allResults = $v->results();

foreach ($allResults as $level => $resultset) {
foreach ($resultset as $result) {
if ($prefix !== null) {
$result->setIdent($prefix.'.'.$result->ident());
}
$this->addResult($r);
$this->addResult($result);
}
}
return $this;
}

/**
* Transform a string from "snake_case" to "camelCase".
*
* @param string $value The string to camelize.
* @return string The camelized string.
*/
final protected function camelize($value)
{
$key = $value;

if (isset(static::$camelCache[$key])) {
return static::$camelCache[$key];
}

if (strpos($value, '_') !== false) {
$value = implode('', array_map('ucfirst', explode('_', $value)));
}

static::$camelCache[$key] = lcfirst($value);

return static::$camelCache[$key];
}

/**
* @return boolean
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Charcoal/Validator/ValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*/
interface ValidatorInterface
{
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';

/**
* @param string $msg The error message.
* @return self
Expand Down
10 changes: 8 additions & 2 deletions tests/Charcoal/Model/ModelValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ public function testValidateModel()
]
]);

$obj = new ModelValidator($model);
$this->assertTrue($obj->validate());
$validator = new ModelValidator($model);
$this->assertFalse($validator->validate());

$model['foo'] = 'qux';
$this->assertFalse($validator->validate());

$model['foo'] = 'xyzzy';
$this->assertTrue($validator->validate());
}
}

0 comments on commit b31cf49

Please sign in to comment.