Last active
August 29, 2015 14:01
-
-
Save Gisleburt/46cce27abf7cde67d1c0 to your computer and use it in GitHub Desktop.
Checks each property (eg $property_name) of an object for a validatePropertyName($value) function, and runs it. If it's invalid it adds it to an array of invalid properties and returns it.
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
<?php | |
class SomeClass { | |
const FIELD_MISSING = 'Missing'; | |
const FIELD_INVALID = 'Invalid'; | |
/** | |
* Checks each field for a validate function for each field of the object and runs it | |
* Override to change this functionality | |
* @param array $attributes | |
* @return array | |
*/ | |
public function getInvalidProperties() { | |
$invalidFields = array(); | |
foreach(get_object_vars($this) as $property => $value) { | |
$validateFunction = 'validate'.str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); | |
if(method_exists($this, $validateFunction)) { | |
if(!$this->$validateFunction($value)) { | |
if($value) { | |
$invalidFields[$property] = static::FIELD_INVALID; | |
} | |
else { | |
$invalidFields[$property] = static::FIELD_MISSING; | |
} | |
} | |
} | |
} | |
return $invalidFields; | |
} | |
} |
Author
Gisleburt
commented
May 13, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment