예제 #1
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;
     }
     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']);
 }
예제 #3
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']);
 }
예제 #4
0
 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']);
 }
예제 #5
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']);
 }
예제 #6
0
 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']);
 }
예제 #7
0
 public function testPushEmbeddedDocument()
 {
     $document = new Document($this->collection);
     $document->push('profiles', new ProfileDocument(array('name' => 'USER_NAME1')));
     $document->push('profiles', new ProfileDocument(array('name' => 'USER_NAME2')));
     $this->assertSame(array(array('name' => 'USER_NAME1'), array('name' => 'USER_NAME2')), $document->get('profiles'));
 }
예제 #8
0
 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']);
     }
 }
예제 #9
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']);
 }
예제 #10
0
 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);
 }
예제 #11
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']);
 }
예제 #12
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']);
     }
 }
예제 #13
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']);
 }
예제 #14
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;
         }
     }
 }
예제 #15
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']);
 }
예제 #16
0
 /**
  * Get related documents
  * @param string $relationName name of relation
  */
 public function getRelated($relationName)
 {
     // check if relation exists
     if (!$this->isRelationExists($relationName)) {
         throw new \Sokil\Mongo\Exception('Relation with name "' . $relationName . '" not found');
     }
     // get relation metadata
     $relation = $this->relations[$relationName];
     $relationType = $relation[0];
     $targetCollectionName = $relation[1];
     // get target collection
     $foreignCollection = $this->document->getCollection()->getDatabase()->getCollection($targetCollectionName);
     // check if relation already resolved
     if (isset($this->resolvedRelationIds[$relationName])) {
         if (is_array($this->resolvedRelationIds[$relationName])) {
             // has_many, many_many
             return $foreignCollection->getDocuments($this->resolvedRelationIds[$relationName]);
         } else {
             //has_one, belongs
             return $foreignCollection->getDocument($this->resolvedRelationIds[$relationName]);
         }
     }
     switch ($relationType) {
         case Document::RELATION_HAS_ONE:
             $localKey = isset($relation['localKey']) ? $relation['localKey'] : '_id';
             $foreignKey = $relation[2];
             $document = $foreignCollection->find()->where($foreignKey, $this->document->get($localKey))->findOne();
             if ($document) {
                 $this->resolvedRelationIds[$relationName] = (string) $document->getId();
             }
             return $document;
         case Document::RELATION_BELONGS:
             $localKey = $relation[2];
             $foreignKey = isset($relation['foreignKey']) ? $relation['foreignKey'] : '_id';
             if ($foreignKey === '_id') {
                 $document = $foreignCollection->getDocument($this->document->get($localKey));
             } else {
                 $document = $foreignCollection->find()->where($foreignKey, $this->document->get($localKey))->findOne();
             }
             if ($document) {
                 $this->resolvedRelationIds[$relationName] = (string) $document->getId();
             }
             return $document;
         case Document::RELATION_HAS_MANY:
             $localKey = isset($relation['localKey']) ? $relation['localKey'] : '_id';
             $foreignKey = $relation[2];
             $documents = $foreignCollection->find()->where($foreignKey, $this->document->get($localKey))->findAll();
             foreach ($documents as $document) {
                 $this->resolvedRelationIds[$relationName][] = (string) $document->getId();
             }
             return $documents;
         case Document::RELATION_MANY_MANY:
             $isRelationListStoredInternally = isset($relation[3]) && $relation[3];
             if ($isRelationListStoredInternally) {
                 // relation list stored in this document
                 $localKey = $relation[2];
                 $foreignKey = isset($relation['foreignKey']) ? $relation['foreignKey'] : '_id';
                 $relatedIdList = $this->document->get($localKey);
                 if (!$relatedIdList) {
                     return array();
                 }
                 $documents = $foreignCollection->find()->whereIn($foreignKey, $relatedIdList)->findAll();
             } else {
                 // relation list stored in external document
                 $localKey = isset($relation['localKey']) ? $relation['localKey'] : '_id';
                 $foreignKey = $relation[2];
                 $documents = $foreignCollection->find()->where($foreignKey, $this->document->get($localKey))->findAll();
             }
             foreach ($documents as $document) {
                 $this->resolvedRelationIds[$relationName][] = (string) $document->getId();
             }
             return $documents;
         default:
             throw new \Sokil\Mongo\Exception('Unsupported relation type "' . $relationType . '" when resolve relation "' . $relationName . '"');
     }
 }
예제 #17
0
 /**
  * Get related documents
  * 
  * @param string $relationName name of relation
  */
 public function getRelated($relationName)
 {
     // check if relation exists
     if (!$this->isRelationExists($relationName)) {
         throw new \Sokil\Mongo\Exception('Relation with name "' . $relationName . '" not found');
     }
     // get relation metadata
     $relation = $this->relations[$relationName];
     $relationType = $relation[0];
     $targetCollectionName = $relation[1];
     if (empty($this->resolvedRelations[$targetCollectionName])) {
         if (false !== stripos($targetCollectionName, '\\')) {
             $targetCollection = new $targetCollectionName();
             $targetCollection->enableDocumentPool();
         } else {
             // get target collection
             $targetCollection = $this->document->getCollection()->getDatabase()->getCollection($targetCollectionName);
         }
         $this->resolvedRelations[$targetCollectionName] =& $targetCollection;
     } else {
         $targetCollection = $this->resolvedRelations[$targetCollectionName];
     }
     // check if relation already resolved
     if (isset($this->resolvedRelationIds[$relationName])) {
         if (is_array($this->resolvedRelationIds[$relationName])) {
             // has_many, many_many
             return $targetCollection->getDocumentsFromDocumentPool($this->resolvedRelationIds[$relationName]);
         } else {
             //has_one, belongs
             return $targetCollection->getDocumentFromDocumentPool($this->resolvedRelationIds[$relationName]);
         }
     }
     switch ($relationType) {
         case Document::RELATION_HAS_ONE:
             if (isset($relation[3])) {
                 $internalField = $relation[3];
             } else {
                 $internalField = '_id';
             }
             $externalField = $relation[2];
             $document = $targetCollection->find()->where($externalField, $this->document->get($internalField))->findOne();
             if ($document) {
                 $this->resolvedRelationIds[$relationName] = (string) $document->getId();
                 //                     $targetCollection->addDocumentToDocumentPool($document);
             }
             return $document;
         case Document::RELATION_BELONGS:
             $internalField = $relation[2];
             $document = $targetCollection->getDocument($this->document->get($internalField));
             if ($document) {
                 $this->resolvedRelationIds[$relationName] = (string) $document->getId();
             }
             return $document;
         case Document::RELATION_HAS_MANY:
             $internalField = '_id';
             $externalField = $relation[2];
             $documents = $targetCollection->find()->where($externalField, $this->document->get($internalField))->findAll();
             foreach ($documents as $document) {
                 $this->resolvedRelationIds[$relationName][] = (string) $document->getId();
             }
             return $documents;
         case Document::RELATION_MANY_MANY:
             $isRelationListStoredInternally = isset($relation[3]) && $relation[3];
             if ($isRelationListStoredInternally) {
                 // relation list stored in this document
                 $internalField = $relation[2];
                 $relatedIdList = $this->document->get($internalField);
                 if (!$relatedIdList) {
                     return array();
                 }
                 $externalField = '_id';
                 $documents = $targetCollection->find()->whereIn($externalField, $relatedIdList)->findAll();
             } else {
                 // relation list stored in external document
                 $internalField = '_id';
                 $externalField = $relation[2];
                 $documents = $targetCollection->find()->where($externalField, $this->document->get($internalField))->findAll();
             }
             foreach ($documents as $document) {
                 $this->resolvedRelationIds[$relationName][] = (string) $document->getId();
             }
             return $documents;
         default:
             throw new \Sokil\Mongo\Exception('Unsupported relation type "' . $relationType . '" when resolve relation "' . $relationName . '"');
     }
 }
예제 #18
0
파일: Cart.php 프로젝트: boliev/tstask
 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++;
 }