MongoIdValidator.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\mongodb\validators;
  8. use MongoDB\BSON\ObjectID;
  9. use yii\base\InvalidConfigException;
  10. use yii\validators\Validator;
  11. use Yii;
  12. /**
  13. * MongoIdValidator verifies if the attribute is a valid Mongo ID.
  14. * Attribute will be considered as valid, if it is an instance of [[\MongoId]] or a its string value.
  15. *
  16. * Usage example:
  17. *
  18. * ```php
  19. * class Customer extends yii\mongodb\ActiveRecord
  20. * {
  21. * ...
  22. * public function rules()
  23. * {
  24. * return [
  25. * ['_id', 'yii\mongodb\validators\MongoIdValidator']
  26. * ];
  27. * }
  28. * }
  29. * ```
  30. *
  31. * This validator may also serve as a filter, allowing conversion of Mongo ID value either to the plain string
  32. * or to [[\MongoId]] instance. You can enable this feature via [[forceFormat]].
  33. *
  34. * @author Paul Klimov <klimov.paul@gmail.com>
  35. * @since 2.0.4
  36. */
  37. class MongoIdValidator extends Validator
  38. {
  39. /**
  40. * @var string|null specifies the format, which validated attribute value should be converted to
  41. * in case validation was successful.
  42. * valid values are:
  43. * - 'string' - enforce value converted to plain string.
  44. * - 'object' - enforce value converted to [[\MongoId]] instance.
  45. * If not set - no conversion will be performed, leaving attribute value intact.
  46. */
  47. public $forceFormat;
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function init()
  52. {
  53. parent::init();
  54. if ($this->message === null) {
  55. $this->message = Yii::t('yii', '{attribute} is invalid.');
  56. }
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function validateAttribute($model, $attribute)
  62. {
  63. $value = $model->$attribute;
  64. $mongoId = $this->parseMongoId($value);
  65. if (is_object($mongoId)) {
  66. if ($this->forceFormat !== null) {
  67. switch ($this->forceFormat) {
  68. case 'string' : {
  69. $model->$attribute = $mongoId->__toString();
  70. break;
  71. }
  72. case 'object' : {
  73. $model->$attribute = $mongoId;
  74. break;
  75. }
  76. default: {
  77. throw new InvalidConfigException("Unrecognized format '{$this->forceFormat}'");
  78. }
  79. }
  80. }
  81. } else {
  82. $this->addError($model, $attribute, $this->message, []);
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function validateValue($value)
  89. {
  90. return is_object($this->parseMongoId($value)) ? null : [$this->message, []];
  91. }
  92. /**
  93. * @param mixed $value
  94. * @return ObjectID|null
  95. */
  96. private function parseMongoId($value)
  97. {
  98. if ($value instanceof ObjectID) {
  99. return $value;
  100. }
  101. try {
  102. return new ObjectID($value);
  103. } catch (\Exception $e) {
  104. return null;
  105. }
  106. }
  107. }