Author: Dmytro Sokil (dmytro.sokil@gmail.com)
Inheritance: extends Structure
 public function validateField(\Sokil\Mongo\Document $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']);
     }
 }
Example #2
0
 public function validateField(\Sokil\Mongo\Document $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']);
 }
 public function validateField(\Sokil\Mongo\Document $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);
 }
Example #4
0
 public function clearRevisions()
 {
     $documentId = $this->document->getId();
     $this->getRevisionsCollection()->deleteDocuments(function (Expression $expression) use($documentId) {
         /* @var $expression \Sokil\Mongo\Expression */
         return $expression->where('__documentId__', $documentId);
     });
     return $this;
 }
Example #5
0
 public function testPushInvalidEmbeddedDocument()
 {
     $document = new Document($this->collection);
     try {
         $document->push('profiles', new ProfileDocument(array('name' => null)));
         $this->fail('InvalidDocumentException must be thrown, but method call was successfull');
     } catch (InvalidDocumentException $e) {
         $this->assertSame(array('name' => array('required' => 'REQUIRED_FIELD_EMPTY_MESSAGE')), $e->getDocument()->getErrors());
     }
 }
Example #6
0
 public function validateField(\Sokil\Mongo\Document $document, $fieldName, array $params)
 {
     if (!$document->get($fieldName)) {
         return;
     }
     if (is_null($document->get($fieldName))) {
         return;
     }
     if (!isset($params['message'])) {
         $params['message'] = 'Field "' . $fieldName . '" must be null in model ' . get_called_class();
     }
     $document->addError($fieldName, $this->getName(), $params['message']);
 }
Example #7
0
 public function validateField(\Sokil\Mongo\Document $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']);
 }
Example #8
0
 public function insert(array $document)
 {
     if (!$this->isValidationEnabled) {
         self::$validator->merge($document);
         $isValid = self::$validator->isValid();
         self::$validator->reset();
         if (!$isValid) {
             throw new InvalidDocumentException('Document is invalid on batch insert');
         }
     }
     $this->add($document);
     return $this;
 }
 public function validateField(\Sokil\Mongo\Document $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']);
 }
 public function validateField(\Sokil\Mongo\Document $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']);
 }
Example #11
0
 public function validateField(\Sokil\Mongo\Document $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(\Sokil\Mongo\Document $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']);
 }
Example #13
0
 public function validateField(\Sokil\Mongo\Document $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']);
 }
Example #14
0
 public function validateField(\Sokil\Mongo\Document $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']);
     }
 }
Example #15
0
 public function validateField(\Sokil\Mongo\Document $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']);
 }
Example #16
0
 public function validateField(\Sokil\Mongo\Document $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;
         }
     }
 }
Example #17
0
 public function validateField(\Sokil\Mongo\Document $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));
     if (!$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']);
 }
Example #18
0
 public function removeRelation($relationName, Document $document = null)
 {
     if (!$this->isRelationExists($relationName)) {
         throw new \Exception('Relation ' . $relationName . ' not configured');
     }
     $relation = $this->relations[$relationName];
     list($relationType, $relatedCollectionName, $field) = $relation;
     $relatedCollection = $this->document->getCollection()->getDatabase()->getCollection($relatedCollectionName);
     if ($document && !$relatedCollection->hasDocument($document)) {
         throw new \Sokil\Mongo\Exception('Document must belongs to related collection');
     }
     switch ($relationType) {
         case Document::RELATION_BELONGS:
             $this->document->unsetField($field)->save();
             break;
         case Document::RELATION_HAS_ONE:
             $document = $this->getRelated($relationName);
             if (!$document) {
                 // relation not exists
                 return $this;
             }
             $document->unsetField($field)->save();
             break;
         case Document::RELATION_HAS_MANY:
             if (!$document) {
                 throw new \Sokil\Mongo\Exception('Related document must be defined');
             }
             $document->unsetField($field)->save();
             break;
         case Document::RELATION_MANY_MANY:
             if (!$document) {
                 throw new \Sokil\Mongo\Exception('Related document must be defined');
             }
             $isRelationListStoredInternally = isset($relation[3]) && $relation[3];
             if ($isRelationListStoredInternally) {
                 $this->document->pull($field, $document->getId())->save();
             } else {
                 $document->pull($field, $this->document->getId())->save();
             }
             break;
         default:
             throw new \Sokil\Mongo\Exception('Unsupported relation type "' . $relationType . '" when resolve relation "' . $relationName . '"');
     }
     return $this;
 }
Example #19
0
 public function __construct(Collection $revisionsCollection, array $data = null, array $options = array())
 {
     parent::__construct($revisionsCollection, $data, $options);
     $baseCollectionName = substr($revisionsCollection->getName(), 0, -1 * strlen(RevisionManager::REVISION_COLLECTION_SUFFIX));
     $this->baseCollection = $revisionsCollection->getDatabase()->getCollection($baseCollectionName);
 }
Example #20
0
 /**
  * @deprecated since 1.13. Use Document::delete()
  * @param \Sokil\Mongo\Document $document
  * @return \Sokil\Mongo\Collection
  */
 public function deleteDocument(Document $document)
 {
     $document->delete();
     return $this;
 }
Example #21
0
 /**
  * Push reference to list
  *
  * @param string $name
  * @param Document $document
  * @return Document
  */
 public function pushReference($name, Document $document)
 {
     return $this->push($name, $document->createReference());
 }
Example #22
0
 private function addToCart(\Sokil\Mongo\Document $product)
 {
     $this->cart[] = array('title' => $product->get('title'), 'price' => $product->get('price'));
     $this->sum += (int) $product->get('price');
     $this->count++;
 }