MongoId::isValid PHP Method

isValid() public static method

Check if a value is a valid ObjectId
public static isValid ( mixed $value ) : boolean
$value mixed The value to check for validity.
return boolean
    public static function isValid($value)
    {
        if ($value instanceof ObjectID || $value instanceof MongoId) {
            return true;
        } elseif (!is_string($value)) {
            return false;
        }
        return (bool) preg_match('#^[a-f0-9]{24}$#i', $value);
    }

Usage Example

コード例 #1
0
 public function gen_value($input)
 {
     //如果为空,返回空
     $input = trim($input);
     if ($input == "" || $input == "-") {
         return "";
     }
     //先检查是不是已经是 mongoid
     if (strlen($input) == 24 && MongoId::isValid($input)) {
         return $input;
     }
     $this->db->select(array($this->valueField, $this->showField))->where(array($this->showField => $input));
     if (!isset($this->whereOrgId)) {
         $this->whereOrgId = $this->CI->myOrgId;
     }
     $this->db->where(array('orgId' => $this->whereOrgId));
     $query = $this->db->get($this->tableName);
     if ($query->num_rows() > 0) {
         $result = $query->row_array();
         $real_id = $result['_id']->{'$id'};
     } else {
         $new_id = $this->plusCreate($input);
         $real_id = $new_id->{'$id'};
     }
     return $real_id;
 }
All Usage Examples Of MongoId::isValid