Entity/DocumentRepository.php
changeset 18 6f16b9fd6a17
parent 5 45378793512a
child 23 b435f8055cb4
equal deleted inserted replaced
17:81962874e172 18:6f16b9fd6a17
     1 <?php
     1 <?php
     2 
     2 
     3 namespace IRI\Bundle\WikiTagBundle\Entity;
     3 namespace IRI\Bundle\WikiTagBundle\Entity;
     4 
     4 
     5 use Doctrine\ORM\EntityRepository;
     5 use Doctrine\ORM\EntityRepository;
     6 use IRI\Bundle\WikiTagBundle\Model\DocumentInterface;
     6 use IRI\Bundle\WikiTagBundle\Entity\Document;
       
     7 
       
     8 use \ReflectionClass;
     7 
     9 
     8 /**
    10 /**
     9  * DocumentRepository
    11  * DocumentRepository
    10  *
    12  *
    11  * This class was generated by the Doctrine ORM. Add your own custom
    13  * This class was generated by the Doctrine ORM. Add your own custom
    12  * repository methods below.
    14  * repository methods below.
    13  */
    15  */
    14 class DocumentRepository extends EntityRepository
    16 class DocumentRepository extends EntityRepository
    15 {
    17 {
       
    18     /**
       
    19      *
       
    20      * TODO : Enter description here ...
       
    21      * @var ReflectionClass
       
    22      */
       
    23     private $reflection_class;
       
    24     private $reflection_doc_class;
       
    25     private $set_methods = array();
       
    26     private $get_methods = array();
       
    27     
    16     function findOneByExternalId($external_id)
    28     function findOneByExternalId($external_id)
    17     {
    29     {
    18         return $this->findOneBy(array("externalId" => strval($external_id)));
    30         return $this->findOneBy(array("externalId" => $external_id));
    19     }
    31     }
    20     
    32     
    21     function writeDocument(DocumentInterface $document)
    33     private function reflectionSetField($object, $method_name, $value)
       
    34     {
       
    35         if(isset($this->set_methods[$method_name]))
       
    36         {
       
    37             $set_method = $this->set_methods[$method_name];
       
    38         }
       
    39         
       
    40         if(!isset($set_method) || is_null($set_method))
       
    41         {
       
    42             if(is_null($this->reflection_doc_class))
       
    43             {
       
    44                 $this->reflection_doc_class = new ReflectionClass(get_class($object));
       
    45             }
       
    46             
       
    47             $set_method = NULL;
       
    48             if($this->reflection_doc_class->hasMethod($method_name))
       
    49             {
       
    50                 $set_method = $this->reflection_doc_class->getMethod($method_name);
       
    51             }
       
    52             if(!is_null($set_method))
       
    53             {
       
    54                 $this->set_methods[$method_name]=$set_method;
       
    55             }
       
    56         }
       
    57         
       
    58         if(!isset($set_method) || is_null($set_method) || !$set_method->isPublic())
       
    59         {
       
    60             throw new \Exception("setter method unknown $method_name");
       
    61         }
       
    62         
       
    63         //set value
       
    64         $set_method->invoke($object, $value);
       
    65         
       
    66     }
       
    67     
       
    68     private function reflectionGetField($document, $accessor)
       
    69     {
       
    70         
       
    71         if(!isset($this->get_methods[$accessor]) ||  is_null($this->get_methods[$accessor]))
       
    72         {
       
    73             if(is_null($this->reflection_class))
       
    74             {
       
    75                 $this->reflection_class = new \ReflectionClass(get_class($document));
       
    76             }
       
    77             
       
    78             //look at properties
       
    79             if($this->reflection_class->hasProperty($accessor))
       
    80             {
       
    81                 $get_object = $this->reflection_class->getProperty($accessor);
       
    82                 if(!$get_object->isPublic())
       
    83                 {
       
    84                     $get_object = NULL;
       
    85                 }
       
    86             }
       
    87             
       
    88             if((!isset($get_object) || is_null($get_object)) && $this->reflection_class->hasMethod($accessor))
       
    89             {
       
    90                 $get_object = $this->reflection_class->getMethod($accessor);
       
    91                 if(!$get_object->isPublic())
       
    92                 {
       
    93                     $get_object = NULL;
       
    94                 }
       
    95             }
       
    96             
       
    97             if((!isset($get_object) || is_null($get_object)) && $this->reflection_class->hasMethod("get".ucfirst($accessor)))
       
    98             {
       
    99                 $get_object = $this->reflection_class->getMethod("get".ucfirst($accessor));
       
   100                 if(!$get_object->isPublic())
       
   101                 {
       
   102                     $get_object = NULL;
       
   103                 }
       
   104             }
       
   105 
       
   106             if(isset($get_object) && !is_null($get_object))
       
   107             {
       
   108                 $this->get_methods[$accessor] = $get_object;
       
   109             }
       
   110         }
       
   111 
       
   112         if(isset($this->get_methods[$accessor]))
       
   113         {
       
   114             $get_object = $this->get_methods[$accessor];
       
   115             if(!is_null($get_object))
       
   116             {
       
   117                 if(is_a($get_object,"\ReflectionMethod"))
       
   118                 {
       
   119                     return $get_object->invoke($document);
       
   120                 }
       
   121                 elseif(is_a($get_object,"\ReflectionProperty"))
       
   122                 {
       
   123                     return $get_object->getValue($document);
       
   124                 }
       
   125                 else
       
   126                 {
       
   127                     //TODO : custom exception
       
   128                     throw new \Exception("Bad reflection object type");
       
   129                 }
       
   130             }
       
   131         }
       
   132         
       
   133         //TODO: replace by custom exception
       
   134         throw new \Exception("Unknown accessor $accessor");
       
   135     }
       
   136     
       
   137     function writeDocument($document,  $document_id_column, $fields)
    22     {
   138     {
    23         # get document from id
   139         # get document from id
    24         $baseDocument = $this->findOneByExternalId($document->getId());
   140         $docid = $this->reflectionGetField($document, $document_id_column);
       
   141         $baseDocument = $this->findOneByExternalId($docid);
    25     
   142     
    26         if(is_null($baseDocument))
   143         if(is_null($baseDocument))
    27         {
   144         {
    28             $baseDocument = new Document();
   145             $baseDocument = new Document();
    29             $baseDocument->setExternalId(strval($document->getId()));
   146             $baseDocument->setExternalId($docid);
    30         }
   147         }
    31         $baseDocument->setDescription($document->getDescription());
   148         
    32         $baseDocument->setTitle($document->getTitle());
   149         foreach ($fields as $name => $field_def) {
       
   150             if(isset($field_def['accessor']))
       
   151             {
       
   152                 $accessor = $field_def['accessor'];
       
   153             }
       
   154             else
       
   155             {
       
   156                 $accessor = NULL;
       
   157             }
       
   158             if(is_null($accessor))
       
   159             {
       
   160                 $accessor = $name;
       
   161             }
       
   162             
       
   163             $value = strval($this->reflectionGetField($document,$accessor));
       
   164             
       
   165             $method_name = "set".ucfirst($name);
       
   166             
       
   167             $this->reflectionSetField($baseDocument, $method_name, $value);
       
   168             
       
   169         }
    33         
   170         
    34         $this->getEntityManager()->persist($baseDocument);
   171         $this->getEntityManager()->persist($baseDocument);
    35         $this->getEntityManager()->flush();
   172         $this->getEntityManager()->flush();
    36         return $baseDocument;
   173         return $baseDocument;
    37     
   174     
    38     }
   175     }
    39     
   176     
    40     function removeDocument(DocumentInterface $document)
   177     function removeDocument($document, $document_id_column)
    41     {
   178     {
    42         $baseDocument = $this->findOneByExternalId($document->getId());
   179         $docid = $this->reflectionGetField($document, $document_id_column);
       
   180         $baseDocument = $this->findOneByExternalId($docid);
    43         if(!is_null($baseDocument))
   181         if(!is_null($baseDocument))
    44         {
   182         {
    45             $this->getEntityManager()->remove($baseDocument);
   183             $this->getEntityManager()->remove($baseDocument);
    46         }
   184         }
    47     }
   185     }