Author: Vojtěch Kohout
Inheritance: implements Iterator
Beispiel #1
0
 /**
  * @param Row|Traversable|array|null $arg
  * @throws InvalidArgumentException
  */
 public function __construct($arg = null)
 {
     if ($arg instanceof Row) {
         if ($arg->isDetached()) {
             throw new InvalidArgumentException('It is not allowed to create entity ' . get_called_class() . ' from detached instance of LeanMapper\\Row.');
         }
         $this->row = $arg;
         $this->mapper = $arg->getMapper();
     } else {
         $this->row = Result::createDetachedInstance()->getRow();
         foreach ($this->getCurrentReflection()->getEntityProperties() as $property) {
             if ($property->hasDefaultValue()) {
                 $propertyName = $property->getName();
                 $this->set($propertyName, $property->getDefaultValue());
             }
         }
         $this->initDefaults();
         if ($arg !== null) {
             if (!is_array($arg) and !$arg instanceof Traversable) {
                 $type = gettype($arg) !== 'object' ? gettype($arg) : 'instance of ' . get_class($arg);
                 throw new InvalidArgumentException("Argument \$arg in " . get_called_class() . "::__construct must contain either null, array, instance of LeanMapper\\Row or instance of Traversable, {$type} given.");
             }
             $this->assign($arg);
         }
     }
 }
Beispiel #2
0
 /**
  * @param string $email
  * @param DateTime $validity
  * @return Invitation
  */
 public function __construct($email, DateTime $validity)
 {
     $this->row = \LeanMapper\Result::createDetachedInstance()->getRow();
     $this->setEmail($email);
     $this->setValidity($validity);
     $this->generateToken();
 }
Beispiel #3
0
 /**
  * @param DibiRow[] $data
  * @param HydratorMeta $hydratorMeta
  * @param array|null $relationshipsFilter
  * @return array
  */
 public function buildResultsGraph(array $data, HydratorMeta $hydratorMeta, array $relationshipsFilter = null)
 {
     $results = array_fill_keys(array_keys($hydratorMeta->getTablesByPrefixes()), array());
     $index = array();
     foreach ($data as $row) {
         $currentPrimaryKeys = array();
         foreach ($hydratorMeta->getTablesByPrefixes() as $prefix => $table) {
             $alias = $prefix . QueryHelper::PREFIX_SEPARATOR . $hydratorMeta->getPrimaryKeyByTable($table);
             if (isset($row[$alias])) {
                 $currentPrimaryKeys[$prefix] = $row[$alias];
             }
         }
         foreach ($row as $field => $value) {
             if (!isset($index[$field])) {
                 $index[$field] = explode(QueryHelper::PREFIX_SEPARATOR, $field, 2);
             }
             list($prefix, $field) = $index[$field];
             if (!isset($results[$prefix]) or !isset($currentPrimaryKeys[$prefix]) or isset($results[$prefix][$currentPrimaryKeys[$prefix]][$field])) {
                 continue;
             }
             if (!isset($results[$prefix][$currentPrimaryKeys[$prefix]])) {
                 $results[$prefix][$currentPrimaryKeys[$prefix]] = array();
             }
             $results[$prefix][$currentPrimaryKeys[$prefix]][$field] = $value;
         }
     }
     foreach ($results as $prefix => $rows) {
         $results[$prefix] = Result::createInstance($rows, $hydratorMeta->getTableByPrefix($prefix), $this->connection, $this->mapper);
     }
     $relationships = $hydratorMeta->getRelationships($relationshipsFilter);
     if (!empty($relationships)) {
         $this->linkResults($results, $relationships);
     }
     return $results;
 }
Beispiel #4
0
 /**
  * @param int $year
  * @param int $month
  * @param User|int $user
  * @param string|null $description
  * @param string|null $hourlyWage
  * @return Listing
  */
 public function __construct($year, $month, $user, $description = null, $hourlyWage = null)
 {
     $this->row = \LeanMapper\Result::createDetachedInstance()->getRow();
     $this->setListingPeriod($year, $month);
     $this->setUser($user);
     $this->setDescription($description);
     $this->setHourlyWage($hourlyWage);
 }
Beispiel #5
0
 /**
  * @param int $day
  * @param Listing $listing
  * @param WorkedHours $workedHours
  * @param Locality $locality
  * @param string|null $description
  * @param string|null $descOtherHours
  * @return ListingItem
  */
 public function __construct($day, Listing $listing, WorkedHours $workedHours, Locality $locality, $description = null, $descOtherHours = null)
 {
     $this->row = \LeanMapper\Result::createDetachedInstance()->getRow();
     $this->setDay($day);
     $this->setListing($listing);
     $this->setLocality($locality);
     $this->setDescription($description);
     $this->setWorkedTime($workedHours, $descOtherHours);
 }
Beispiel #6
0
 /**
  * @param string $subject
  * @param string $message
  * @param string $author
  * @param \DateTime $sent If $sent is null, sent is set to current date
  * @return Message
  */
 public function __construct($subject, $message, $author, DateTime $sent = null)
 {
     $this->row = \LeanMapper\Result::createDetachedInstance()->getRow();
     $this->setSubject($subject);
     $this->setMessage($message);
     $this->setAuthor($author);
     if (is_null($sent)) {
         $sent = new DateTime();
     }
     $this->setSent($sent);
 }
Beispiel #7
0
 /**
  * @param string $username
  * @param string $password
  * @param string $email
  * @param string $ip
  * @param string $role
  * @param string|null $name
  * @return User
  */
 public function __construct($username, $password, $email, $ip, $role = 'employee', $name = null)
 {
     $this->row = \LeanMapper\Result::createDetachedInstance()->getRow();
     $this->setUsername($username);
     $this->setPassword($password);
     $this->setEmail($email);
     $this->setIp($ip);
     $this->setRole($role);
     $this->setName($name);
     $this->setLastIP($ip);
     $this->setLastLogin(new DateTime());
 }
Beispiel #8
0
 /**
  * @param \DateTime|InvoiceTime|int|string|null $workStart
  * @param \DateTime|InvoiceTime|int|string|null $workEnd
  * @param \DateTime|InvoiceTime|int|string|null $lunch
  * @param \DateTime|InvoiceTime|int|string|null null $otherHours
  */
 public function __construct($workStart, $workEnd, $lunch, $otherHours = null)
 {
     $workStart = new InvoiceTime($workStart);
     $workEnd = new InvoiceTime($workEnd);
     $lunch = new InvoiceTime($lunch);
     $otherHours = new InvoiceTime($otherHours);
     if ($workStart->compare($workEnd) === 1) {
         throw new ShiftEndBeforeStartException('You cannot quit your shift before you even started!');
     }
     $this->row = \LeanMapper\Result::createDetachedInstance()->getRow();
     $this->row->workStart = $workStart->getTime();
     $this->row->workEnd = $workEnd->getTime();
     $this->row->lunch = $lunch->getTime();
     $this->row->otherHours = $otherHours->getTime();
 }
Beispiel #9
0
 public function preloadEdges(ResultProxy $resultProxy)
 {
     $nodesIds = array();
     foreach ($resultProxy as $node) {
         $nodesIds[$node['id']] = true;
     }
     $edges = $this->connection->select('*')->from('edge')->where('[source] IN %in OR [target] IN %in', $ids = array_keys($nodesIds), $ids)->orderBy('type')->fetchAll();
     $referencing = Result::createInstance(array(), 'edge', $this->connection, $this->mapper);
     foreach ($edges as $edge) {
         if (isset($nodesIds[$edge['source']]) || isset($nodesIds[$edge['target']])) {
             $edge = $edge->toArray();
             $edge['related_node'] = $edge['source'];
             $referencing->addDataEntry($edge);
             if ($edge['target'] !== $edge['source'] and $edge['target'] !== null) {
                 $edge['related_node'] = $edge['target'];
                 $referencing->addDataEntry($edge);
             }
         }
     }
     $referencing->cleanAddedAndRemovedMeta();
     $resultProxy->setReferencingResult($referencing, 'edge', 'related_node');
 }
Beispiel #10
0
 /**
  * @param Result $referencingResult
  * @param string $table
  * @param string $viaColumn
  * @param string $strategy
  */
 public function setReferencingResult(self $referencingResult, $table, $viaColumn = null, $strategy = self::STRATEGY_IN)
 {
     $strategy = $this->translateStrategy($strategy);
     if ($viaColumn === null) {
         $viaColumn = $this->mapper->getRelationshipColumn($table, $this->table);
     }
     $this->referencing["{$table}({$viaColumn}){$strategy}#" . self::PRELOADED_KEY] = $referencingResult;
     unset($this->index[$referencingResult->getOriginKey()]);
 }
Beispiel #11
0
 /**
  * Creates new set of Entity's instances from given array of \Dibi\Row instances
  *
  * @param \Dibi\Row[] $rows
  * @param string|null $entityClass
  * @param string|null $table
  * @return array
  */
 protected function createEntities(array $rows, $entityClass = null, $table = null)
 {
     if ($table === null) {
         $table = $this->getTable();
     }
     $entities = [];
     $collection = Result::createInstance($rows, $table, $this->connection, $this->mapper);
     $primaryKey = $this->mapper->getPrimaryKey($this->getTable());
     if ($entityClass !== null) {
         foreach ($rows as $dibiRow) {
             $entity = $this->entityFactory->createEntity($entityClass, $collection->getRow($dibiRow->{$primaryKey}));
             $entity->makeAlive($this->entityFactory);
             $entities[$dibiRow->{$primaryKey}] = $entity;
         }
     } else {
         foreach ($rows as $dibiRow) {
             $row = $collection->getRow($dibiRow->{$primaryKey});
             $entityClass = $this->mapper->getEntityClass($this->getTable(), $row);
             $entity = $this->entityFactory->createEntity($entityClass, $row);
             $entity->makeAlive($this->entityFactory);
             $entities[$dibiRow->{$primaryKey}] = $entity;
         }
     }
     return $this->entityFactory->createCollection($entities);
 }
Beispiel #12
0
 /**
  * @param string $localityName
  * @return Locality
  */
 public function __construct($localityName)
 {
     $this->row = \LeanMapper\Result::createDetachedInstance()->getRow();
     $this->setName($localityName);
 }
Beispiel #13
0
 /**
  * @param Message $message
  * @param $recipient
  * @return UserMessage
  */
 public function __construct(Message $message, $recipient)
 {
     $this->row = \LeanMapper\Result::createDetachedInstance()->getRow();
     $this->setMessage($message);
     $this->setRecipient($recipient);
 }