Inheritance: implements Sokil\Mongo\ArrayableInterface, implements JsonSerializable
Beispiel #1
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     if (!isset($params['min'])) {
         throw new Exception('Minimum value of range not specified');
     }
     if (!isset($params['max'])) {
         throw new Exception('Maximum value of range not specified');
     }
     if ($value < $params['min']) {
         if (empty($params['minMessage'])) {
             $params['minMessage'] = 'Field "' . $fieldName . '" less than minimal value of range in ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['minMessage']);
     }
     if ($value > $params['max']) {
         if (empty($params['maxMessage'])) {
             $params['maxMessage'] = 'Field "' . $fieldName . '" less than minimal value of range in ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['maxMessage']);
     }
 }
Beispiel #2
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     if (empty($params[0])) {
         throw new Exception('Type not specified');
     }
     $requiredTypes = (array) $params[0];
     $allowedTypes = array('array', 'bool', 'callable', 'double', 'float', 'int', 'integer', 'long', 'null', 'numeric', 'object', 'real', 'resource', 'scalar', 'string');
     foreach ($requiredTypes as $type) {
         if (!in_array($type, $allowedTypes)) {
             throw new Exception('Type must be one of ' . implode(', ', $allowedTypes));
         }
         if (true === call_user_func('is_' . $type, $value)) {
             return;
         }
     }
     if (!isset($params['message'])) {
         if (count($requiredTypes) === 1) {
             $params['message'] = 'Field "' . $fieldName . '" must be of type ' . $requiredTypes[0] . ' in ' . get_called_class();
         } else {
             $params['message'] = 'Field "' . $fieldName . '" must be one of types ' . implode(', ', $requiredTypes) . ' in ' . get_called_class();
         }
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Beispiel #3
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     if ($document->get($fieldName)) {
         return;
     }
     $errorMessage = isset($params['message']) ? $params['message'] : 'Field "' . $fieldName . '" required in model ' . get_class($document);
     $document->addError($fieldName, $this->getName(), $errorMessage);
 }
Beispiel #4
0
 public function fieldValuesDataProvider()
 {
     $mongoId = new \MongoId();
     $stdClass = new \stdClass();
     $structure = new Structure();
     $structure->mergeUnmodified(array('param' => 'value'));
     return array('int' => array(1, 2, array(1, 2)), 'string' => array('string1', 'string2', array('string1', 'string2')), 'empty_stdclass' => array($stdClass, $stdClass, array(array(), array())), 'MongoId' => array($mongoId, $mongoId, array($mongoId, $mongoId)), 'list' => array(array(1), array(2), array(array(1), array(2))), 'list_of_list' => array(array(array(1)), array(array(2)), array(array(array(1)), array(array(2)))), 'subdocument' => array(array('subdoc' => 1), array('subdoc' => 2), array(array('subdoc' => 1), array('subdoc' => 2))), 'structure' => array($structure, $structure, array(array('param' => 'value'), array('param' => 'value'))));
 }
Beispiel #5
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if (preg_match($params['pattern'], $document->get($fieldName))) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" not match regexp ' . $params['pattern'] . ' in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
 public function validateField(Structure $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if (preg_match('/^\\w+$/', $document->get($fieldName))) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" not alpha-numeric in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Beispiel #7
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if (in_array($document->get($fieldName), $params['range'])) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" not in range of allowed values in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Beispiel #8
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if ($document->get($fieldName) !== $params['to']) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" must be equals to "' . $params['to'] . '" in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
 public function validateField(Structure $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if ($document->get($fieldName) === $params['to']) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Not equals';
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Beispiel #10
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     $carsNumber = $document->get($fieldName);
     if (is_numeric($carsNumber) && 0 === $this->getMod($carsNumber)) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Value of field "' . $fieldName . '" is not valid card number at ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Beispiel #11
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     if (!isset($params['than'])) {
         throw new Exception('Maximum value not specified');
     }
     if ($value <= $params['than']) {
         if (empty($params['message'])) {
             $params['message'] = 'Field "' . $fieldName . '" must be greater than specified value in ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['message']);
     }
 }
Beispiel #12
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     // check only if set
     if (!$value) {
         return;
     }
     // check if url valid
     if (false !== filter_var($value, FILTER_VALIDATE_IP)) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Value of field "' . $fieldName . '" is not valid IP address in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Beispiel #13
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     // check only if set
     if (!$value) {
         return;
     }
     // check if url valid
     $isValidUrl = (bool) filter_var($value, FILTER_VALIDATE_URL);
     if (!$isValidUrl) {
         if (!isset($params['message'])) {
             $params['message'] = 'Value of field "' . $fieldName . '" is not valid url in model ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['message']);
         return;
     }
     // ping not required - so url is valid
     if (empty($params['ping'])) {
         return;
     }
     // ping required
     $dnsRecordExists = dns_get_record(parse_url($value, PHP_URL_HOST));
     // network not allowed
     if ($dnsRecordExists === false) {
         throw new \RuntimeException('Error getting DNS record to validated url');
     }
     // empty array - host not found
     if (is_array($dnsRecordExists) && empty($dnsRecordExists)) {
         if (!isset($params['message'])) {
             $params['message'] = 'Value of field "' . $fieldName . '" is valid url but host is unreachable in model ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['message']);
         return;
     }
     if ($this->isUrlAccessible($value)) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Value of field "' . $fieldName . '" is valid url but page not found ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Beispiel #14
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     $isValidEmail = filter_var($value, FILTER_VALIDATE_EMAIL);
     $isValidMX = true;
     if ($isValidEmail && !empty($params['mx'])) {
         list(, $host) = explode('@', $value);
         $isValidMX = checkdnsrr($host, 'MX');
     }
     if ($isValidEmail && $isValidMX) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Value of field "' . $fieldName . '" is not email in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Beispiel #15
0
 public function validateField(Structure $document, $fieldName, array $params)
 {
     $value = $document->get($fieldName);
     if (!$value) {
         return;
     }
     $length = mb_strlen($value);
     // check if field is of specified length
     if (isset($params['is'])) {
         if ($length === $params['is']) {
             return;
         }
         if (!isset($params['message'])) {
             $params['message'] = 'Field "' . $fieldName . '" length not equal to ' . $params['is'] . ' in model ' . get_called_class();
         }
         $document->addError($fieldName, $this->getName(), $params['message']);
         return;
     }
     // check if fied is shorter than required
     if (isset($params['min'])) {
         if ($length < $params['min']) {
             if (!isset($params['messageTooShort'])) {
                 $params['messageTooShort'] = 'Field "' . $fieldName . '" length is shorter tnan ' . $params['min'] . ' in model ' . get_called_class();
             }
             $document->addError($fieldName, $this->getName(), $params['messageTooShort']);
             return;
         }
     }
     // check if fied is longer than required
     if (isset($params['max'])) {
         if ($length > $params['max']) {
             if (!isset($params['messageTooLong'])) {
                 $params['messageTooLong'] = 'Field "' . $fieldName . '" length is longer tnan ' . $params['max'] . ' in model ' . get_called_class();
             }
             $document->addError($fieldName, $this->getName(), $params['messageTooLong']);
             return;
         }
     }
 }
 public function pushEach($fieldName, array $values)
 {
     // value must be list, not dictionary
     $values = array_values($values);
     // prepasre to store
     $values = Structure::prepareToStore($values);
     // no $push operator found
     if (!isset($this->_operators['$push'])) {
         $this->_operators['$push'] = array();
     }
     // no field name found
     if (!isset($this->_operators['$push'][$fieldName])) {
         $this->_operators['$push'][$fieldName] = array('$each' => $values);
     } else {
         if (!is_array($this->_operators['$push'][$fieldName]) || !isset($this->_operators['$push'][$fieldName]['$each'])) {
             $oldValue = $this->_operators['$push'][$fieldName];
             $this->_operators['$push'][$fieldName] = array('$each' => array_merge(array($oldValue), $values));
         } else {
             $this->_operators['$push'][$fieldName]['$each'] = array_merge($this->_operators['$push'][$fieldName]['$each'], $values);
         }
     }
     return $this;
 }
Beispiel #17
0
 public function bitwiceXor($field, $value)
 {
     $oldFieldValue = (int) $this->get($field);
     $newValue = $oldFieldValue ^ $value;
     parent::set($field, $newValue);
     if ($this->getId()) {
         if (version_compare($this->getCollection()->getDatabase()->getClient()->getDbVersion(), '2.6', '>=')) {
             $this->operator->bitwiceXor($field, $value);
         } else {
             $this->operator->set($field, $newValue);
         }
     }
     return $this;
 }