/**
  * Synchronisiert eine Collection von (Datenbank-)Objekten mit einer Collection von Daten / Unique-Kriterien oder Exportieren Objekten
  *
  * Die $fromCollection ist die Collection der bereits hydrierten Objekte. (fertige Tags)
  * die $toCollection ist die noch nicht hydrierte Collection der Objekte die Objekte in $fromCollection repräsentieren (Tag Labels)
  * 
  * zur Hilfe werden zwei Funktionen benötigt.
  * Die hydrierFunktion erstellt aus den Daten asu $toCollection ein Objekt aus $fromCollection ( $label ===> Entity $tag )
  * die hash Funktion erstellt aus einem Objekt von $fromCollection einen Skalar ( Entity $tag => $id )
  *
  * Article und Test beispiel:
  *
  * $fromCollection = $article->getTags();
  * $toCollection = array('Tag1Name','Tag2Name','....');
  *
  * so ungefähr!! muss NULL zurückgeben nicht array
  * $hydrateUniqueObject = function (\stdClass $json) {
  *    return $em->createQueryBuilder()
  *      ->select('tag')->from('Entities\Tag')
  *      ->where('id = :id OR uniqueconstraint = :unique)
  *      ->setParameter(
  *         array(
  *           'id'=>$json->id,
  *           'unique'>$json->label
  *         )
  *       )
  *    ;
  * };
  *
  * $hashObject = function (Tag $tag) {
  *   return $tag->getIdentifier();
  * };
  *
  * @param ArrayCollection $dbCollection die Collection als fertige Objekte aus der Datenbank
  * @param collection $toCollection eine noch nicht hydrierte Collection von Objekten die Objekte in $fromCollection repräsentieren kann
  *
  * @return list($insert,$updates,$deletes) die jeweiligen listen von dispatchten events
  */
 public function process($fromCollection, $toCollection)
 {
     // wir haben keine Ahnung welche Objekte in der $fromCollection und welche in der $toCollection sind
     $fromCollection = \Psc\Code\Code::castArray($fromCollection);
     // damit wir eine copy machen
     $updates = $inserts = $deletes = array();
     $index = array();
     foreach ($toCollection as $toCollectionKey => $toObject) {
         $fromObject = $this->hydrateUniqueObject($toObject, $toCollectionKey);
         // kein objekt konnte hydriert werden
         if ($fromObject === NULL) {
             $inserts[] = $this->dispatchInsert($toObject, array('key' => $toCollectionKey));
             // inserts müssen nicht in den index, da sie nicht im universum gefunden wurden, können sie auch nicht in $fromCollection sein
         } else {
             // objekt hydriert, kann mit korrekter id sein oder matching unique-constraint
             $updates[] = $this->dispatchUpdate($fromObject, array('from' => $fromObject, 'to' => $toObject, 'key' => $toCollectionKey));
             $index[$this->hashObject($fromObject)] = TRUE;
         }
     }
     foreach ($fromCollection as $fromObject) {
         if (!array_key_exists($this->hashObject($fromObject), $index)) {
             // object ist weder ein insert noch ein update
             $deletes[] = $this->dispatchDelete($fromObject);
         }
     }
     return array($inserts, $updates, $deletes);
 }
示例#2
0
 public function validate($data)
 {
     if ($data === NULL) {
         throw EmptyDataException::factory(array());
     }
     $data = Code::castArray($data);
     if (count($data) == 0) {
         throw EmptyDataException::factory(array());
     }
     if (count($data) > 0) {
         return $data;
     }
     throw new \Psc\Exception('Konnte nicht validiert werden');
 }
示例#3
0
 /**
  * @var mixed $print Closure|String wenn ein String wird dies als Attribute gesehen welches mit get$print() vom Objekt geladen werden kann
  */
 public static function listStrings($collection, $sep = ', ', $andsep = NULL, Closure $formatter = NULL)
 {
     $collection = Code::castArray($collection);
     $cnt = count($collection);
     if (isset($andsep) && $cnt >= 2) {
         $last = array_pop($collection);
     } else {
         $andsep = NULL;
     }
     $formatter = $formatter ?: function ($item) {
         return (string) $item;
     };
     $ret = A::implode($collection, $sep, $formatter);
     if (isset($andsep) && $last != NULL) {
         $ret .= $andsep . $formatter($last);
     }
     return $ret;
 }
示例#4
0
 public static function debugCollection($collection, $glue = "\n", $label = NULL)
 {
     $ret = $label ? 'Collection ' . $label . $glue : NULL;
     if (count($collection) === 0) {
         $ret .= '[leere Collection]';
     } else {
         $ret .= \Webforge\Common\ArrayUtil::implode(Code::castArray($collection), $glue, function ($item, $key) {
             return sprintf('[%s] %s', $key, \Psc\Doctrine\Helper::debugEntity($item));
         });
     }
     return $ret;
 }