| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHPMongo package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Dmytro Sokil <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Sokil\Mongo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Sokil\Mongo\Document\RevisionManager; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | class Revision extends \Sokil\Mongo\Document | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     protected $data = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         '__date__' => null, | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @var \Sokil\Mongo\Collection | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     private $baseCollection; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function __construct(Collection $revisionsCollection, array $data = null, array $options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         parent::__construct($revisionsCollection, $data, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $baseCollectionName = substr( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             $revisionsCollection->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             -1 * strlen(RevisionManager::REVISION_COLLECTION_SUFFIX) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->baseCollection = $revisionsCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ->getDatabase() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ->getCollection($baseCollectionName); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * Set document data | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @param array $document | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @return \Sokil\Mongo\Revision | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function setDocumentData(array $document) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->set('__date__', new \MongoDate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->set('__documentId__', $document['_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         unset($document['_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->merge($document); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * Get document instance | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @return \Sokil\Mongo\Document | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function getDocument() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $data = $this->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         // restore document id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $data['_id'] = $data['__documentId__']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // unset meta fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         unset($data['__date__'], $data['__documentId__']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ->baseCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ->hydrate($data); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * Get date | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @param string|null $format format of date compatible with php's date function | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @return \MongoDate|string | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function getDate($format = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if (empty($format)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             return $this->get('__date__')->sec; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return date($format, $this->get('__date__')->sec); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |