Skip to content

Instantly share code, notes, and snippets.

@alexglue
Created December 30, 2014 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexglue/8b27a1a2eb5b18b272ec to your computer and use it in GitHub Desktop.
Save alexglue/8b27a1a2eb5b18b272ec to your computer and use it in GitHub Desktop.
Phalcon DateTimeFilter implementation (instance of PhalconUserFilter)
<?php
/**
* DatetimeFilter
*
* @author glue
* @date 30.12.14
* @example usage
*
* //In Controller: (ControllerBase etc)
* public function initialize(){
*
* $this->filter->add( 'date', new DateTimeFilter() );
* }
* //...
* {
* $model->created_at = $this->request->getPost( "sendAt", 'date' );
* }
* @subpackage
*/
class DateTimeFilter implements \Phalcon\Filter\UserFilterInterface {
const DefaultFormat = 'm-d-Y';
/**
* @var string format
*/
protected $format = DateTime::ISO8601;
/**
* @param $format
*/
function __construct( $format = self::DefaultFormat ) {
$this->format = $format;
}
/**
* Filters a value
*
* @param mixed $value
*
* @return mixed
*/
public function filter( $value ) {
$date = date_parse_from_format( $this->format, $value );
return $date['error_count']? null : $value ;
}
/**
* @param $value
*
* @return callable
*/
public function __invoke( $value ){
return $this->filter( $value );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment