예제 #1
0
파일: Arrays.php 프로젝트: ymarillet/sknife
 /**
  * Reassigns the keys of a 2-dimensional array from one of its second level field value
  * @param  array             $array the array to parse
  * @param  int|string        $key   the key to map
  * @throws BusinessException
  * @return array
  * @author Yohann Marillet
  */
 public static function reassignKeyFromObjects($array, $key)
 {
     self::validateForeachable($array);
     $return = array();
     if (count($array) == 0) {
         return $return;
     }
     $firstEntry = reset($array);
     $getter = Strings::toGetter($key);
     $reflectionClass = new \ReflectionClass($firstEntry);
     if (empty($key) || !$reflectionClass->hasMethod($getter)) {
         throw new BusinessException('Property "' . strval($key) . '" does not exist in the second dimension of this array');
     }
     foreach ($array as $e) {
         $return[$e->{$getter}()] = $e;
     }
     return $return;
 }
예제 #2
0
 /**
  * Soft deletes a record (implementing IsSoftDeletable)
  * @param $list
  * @return array ID list of actual soft deleted records
  * @author Yohann Marillet
  */
 public function softDelete($list)
 {
     if (!is_array($list) && !$list instanceof \Traversable) {
         $list = [$list];
     }
     $className = $this->getClassName();
     $idName = $this->getClassMetadata()->getIdentifier();
     //does not work for compound PKs
     $idName = reset($idName);
     $getter = Strings::toGetter($idName);
     $notDeleted = [];
     $resolveAlreadyDeleted = false;
     if (reset($list) instanceof $className) {
         $entities = $list;
     } else {
         $entities = $this->findByIdentifiersArray($list);
         $resolveAlreadyDeleted = true;
     }
     $return = [];
     foreach ($entities as $entity) {
         $id = $entity->{$getter}();
         if ($resolveAlreadyDeleted) {
             $notDeleted[] = $id;
         }
         /** @var IsSoftDeletable $entity */
         if ($entity->isSoftDeletable()) {
             $return[] = $id;
             $entity->setIsDeleted(true);
             $this->getEntityManager()->persist($entity);
         }
     }
     if (!empty($return)) {
         $this->getEntityManager()->flush();
     }
     if ($resolveAlreadyDeleted) {
         $diff = array_diff($list, $notDeleted);
         $return = $return + $diff;
     }
     return $return;
 }