EMongoIdValidator.php 825 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. /**
  3. * EMongoIdValidator
  4. *
  5. * This class is basically designed to be used to cast all fields sent into it as MongoIds.
  6. * This was created because at the time it was seen as the most flexible, yet easiest way, to accomplish
  7. * the casting of MongoIds automatically.
  8. */
  9. class EMongoIdValidator extends CValidator
  10. {
  11. public $allowEmpty = true;
  12. protected function validateAttribute($object,$attribute)
  13. {
  14. $value = $object->$attribute;
  15. if($this->allowEmpty && $this->isEmpty($value)){
  16. return;
  17. }
  18. if(is_array($value)){
  19. foreach($value as $key=>$attr){
  20. $value[$key] = $attr instanceof MongoId ? $attr : new MongoId($attr);
  21. }
  22. $object->$attribute = $value;
  23. }else{
  24. $object->$attribute = $object->$attribute instanceof MongoId ? $object->$attribute : new MongoId($object->$attribute);
  25. }
  26. }
  27. }