Skip to content

Commit

Permalink
remove Extant entirely, go with ValidateSpec::isNotBlank() instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul M. Jones committed Feb 26, 2016
1 parent ef919b7 commit c1100b4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 87 deletions.
8 changes: 0 additions & 8 deletions docs/validate.md
Expand Up @@ -98,14 +98,6 @@ Validates the value as loosely equal (`==') to a specified value.
$filter->validate('field')->is('equalToValue', $other_value);
```

## extant

Validates thhat the field exists in the subject, even if it is null or blank.

```php
$filter->validate('field')->is('extant');
```

## float

Validates the value as representing a float.
Expand Down
1 change: 0 additions & 1 deletion src/Locator/ValidateLocator.php
Expand Up @@ -42,7 +42,6 @@ protected function initFactories(array $factories)
'email' => function () { return new Validate\Email(); },
'equalToField' => function () { return new Validate\EqualToField(); },
'equalToValue' => function () { return new Validate\EqualToValue(); },
'extant' => function () { return new Validate\Extant(); },
'float' => function () { return new Validate\Double(); },
'inKeys' => function () { return new Validate\InKeys(); },
'int' => function () { return new Validate\Integer(); },
Expand Down
37 changes: 0 additions & 37 deletions src/Rule/Validate/Extant.php

This file was deleted.

58 changes: 18 additions & 40 deletions src/Spec/ValidateSpec.php
Expand Up @@ -94,6 +94,20 @@ public function isNot($rule)
return $this->init(func_get_args());
}

/**
*
* Validate the field is not blank.
*
* @return self
*
*/
public function isNotBlank()
{
$this->allow_blank = false;
$this->reverse = true;
return $this->init(array());
}

/**
*
* Validate the field does not match this rule (blank allowed).
Expand Down Expand Up @@ -124,7 +138,9 @@ protected function getDefaultMessage()
$message = $this->field . ' should';

if (! $this->rule) {
return $message . ' have been blank';
return $message
. (($this->reverse) ? ' not' : '')
. ' have been blank';
}

if ($this->allow_blank) {
Expand Down Expand Up @@ -153,7 +169,7 @@ protected function applyRule($subject)
return false;
}

if (! $this->fieldExists($subject)) {
if (! isset($subject->{$this->field})) {
return false;
}

Expand All @@ -163,42 +179,4 @@ protected function applyRule($subject)

return parent::applyRule($subject);
}

/**
*
* Does the field exist in the subject, even if it is null?
*
* @param mixed $subject The filter subject.
*
* @return bool
*
*/
protected function fieldExists($subject)
{
$field = $this->field;
if (isset($subject->$field)) {
return true;
}

// still, the property might exist and be null. using property_exists()
// presumes that we have a non-magic-method object, which may not be the
// case, so we have this hackish approach.

// first, turn off error reporting entirely.
$level = error_reporting(0);

// now put error_get_last() into known state by addressing a nonexistent
// variable with an unlikely name.
$fake = __FILE__ . ':' . __CLASS__;
$value = $$fake;

// now get the value of the field and turn error reporting back on
$value = $subject->$field;
error_reporting($level);

// if the last error was on $field, then $field is nonexistent.
$error = error_get_last();
$property = substr($error['message'], -1 * strlen($field) - 1);
return $property !== "\$$field";
}
}
3 changes: 2 additions & 1 deletion tests/SubjectFilterTest.php
Expand Up @@ -99,7 +99,6 @@ public function testApply_missingField()
$this->filter->validate('foo1')->is('strlenMin', 6)->asSoftRule();
$this->filter->validate('foo2')->is('alnum');
$this->filter->validate('foo2')->is('strlenMin', 6);
$this->filter->validate('foo3')->is('extant');

$subject = (object) array('foo1' => '!@#', 'foo3' => null);
$result = $this->filter->apply($subject);
Expand All @@ -120,6 +119,7 @@ public function testApply_missingField()

public function testUseFieldMessage()
{
$this->filter->validate('foo')->isNotBlank()->asSoftRule();
$this->filter->validate('foo')->is('alnum')->asSoftRule();
$this->filter->validate('foo')->is('strlenMin', 6)->asSoftRule();

Expand All @@ -128,6 +128,7 @@ public function testUseFieldMessage()
$this->assertFalse($result);
$expect = array(
'foo' => array(
'foo should not have been blank',
'foo should have validated as alnum',
'foo should have validated as strlenMin(6)',
),
Expand Down

0 comments on commit c1100b4

Please sign in to comment.