Ok i've been trying to work out how to build up my static function like how its done in the PHPActiveRecord class which i have included below the classes i am trying to build for reference. It does it in such a neat way i'ld like to use these methods to build classes in my own projects.
Validator Class
<?php
namespace Cabbit;
class Validate
{
# Class handles the validations of form inputs
# Validators array
private $validators = array();
# Get all the diffrent types of validation types
private static $validationFunctions = array(
'validates_presence_of'
);
public function validates_presence_of($validationFunctions)
{
var_dump($this->validators);
}
}
index file that uses the class
<?php
require_once 'Validate.php';
class implementMethod extends Cabbit\Validate
{
# Implementation class, would be similer to a Model
static $validates_precense_of = array(
array("Title")
);
}
Snip of the Validator class form phpactiverecord
class Validations
{
private $model;
private $options = array();
private $validators = array();
private $record;
private static $VALIDATION_FUNCTIONS = array(
'validates_presence_of',
'validates_size_of',
'validates_length_of',
'validates_inclusion_of',
'validates_exclusion_of',
'validates_format_of',
'validates_numericality_of',
'validates_uniqueness_of'
);
private static $DEFAULT_VALIDATION_OPTIONS = array(
'on' => 'save',
'allow_null' => false,
'allow_blank' => false,
'message' => null,
);
private static $ALL_RANGE_OPTIONS = array(
'is' => null,
'within' => null,
'in' => null,
'minimum' => null,
'maximum' => null,
);
private static $ALL_NUMERICALITY_CHECKS = array(
'greater_than' => null,
'greater_than_or_equal_to' => null,
'equal_to' => null,
'less_than' => null,
'less_than_or_equal_to' => null,
'odd' => null,
'even' => null
);
/**
* Constructs a {@link Validations} object.
*
* @param Model $model The model to validate
* @return Validations
*/
public function __construct(Model $model)
{
$this->model = $model;
$this->record = new Errors($this->model);
}
/**
* Runs the validators.
* @return Errors the validation errors if any
*/
public function validate()
{
$reflection = Reflections::instance()->get(get_class($this->model));
// create array of validators to use from valid functions merged with the static properties
$this->validators = array_intersect(array_keys($reflection->getStaticProperties()), self::$VALIDATION_FUNCTIONS);
if (empty($this->validators))
return $this->record;
$inflector = Inflector :: instance();
foreach ($this->validators as $validate)
{
$func = $inflector->variablize($validate);
$this->$func($reflection->getStaticPropertyValue($validate));
}
return $this->record;
}
/**
* Validates a field is not null and not blank.
*
* <code>
* class Person extends ActiveRecord\Model {
* static $validates_presence_of = array(
* array('first_name'),
* array('last_name')
* );
* }
* </code>
*
* Available options:
*
* <ul>
* <li><b>message:</b> custom error message</li>
* </ul>
*
* @param array $attrs Validation definition
*/
public function validates_presence_of($attrs)
{
$configuration = array_merge(self::$DEFAULT_VALIDATION_OPTIONS ,array('message' => Errors::$DEFAULT_ERROR_MESSAGES['blank'], 'on' => 'save'));
foreach ($attrs as $attr)
{
$options = array_merge($configuration, $attr);
$this->record->add_on_blank($options[0], $options['message']);
}
}