Created
December 30, 2014 16:47
-
-
Save alexglue/8b27a1a2eb5b18b272ec to your computer and use it in GitHub Desktop.
Phalcon DateTimeFilter implementation (instance of PhalconUserFilter)
This file contains hidden or 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 | |
/** | |
* 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