/** * @param $entity * @return mixed * @throws RuntimeException */ public function getIdentity($entity) { $identity = $this->propertyAccessor->getValue($entity, $this->identity->getPropertyName()); if (empty($identity) && $identity !== 0) { throw new RuntimeException("Can't get identity from not identified entity."); } return $identity; }
/** * @param $target * @param $source * @param $propertyName * @throws InvalidArgumentException * @throws NotExistingPropertyException */ public function cloneProperty($target, $source, $propertyName) { $this->validaObjects($target, $source); $reflection = new \ReflectionClass($target); if (!$reflection->hasProperty($propertyName)) { throw new NotExistingPropertyException(); } $this->propertyAccessor->setValue($target, $propertyName, $this->propertyAccessor->getValue($source, $propertyName)); }
/** * @param Property $property * @param $firstObject * @param $secondObject * @return bool * @throws InvalidArgumentException */ public function hasDifferentValue(Property $property, $firstObject, $secondObject) { $this->validaObjects($firstObject, $secondObject); $firstValue = $this->propertyAccessor->getValue($firstObject, $property->getName()); $secondValue = $this->propertyAccessor->getValue($secondObject, $property->getName()); $comparator = $this->comparatorFactory->getComparatorFor($firstValue, $secondValue); try { $comparator->assertEquals($firstValue, $secondValue); return false; } catch (ComparisonFailure $exception) { return true; } }
/** * @param $oldEntity * @param $newEntity * @return ChangeSet * @throws RuntimeException * * @api */ public function buildChanges($oldEntity, $newEntity) { $changes = []; $entityDefinition = $this->definitions->getDefinition($oldEntity); foreach ($entityDefinition->getObservedProperties() as $property) { if ($this->isDifferent($property, $oldEntity, $newEntity)) { $oldValue = $this->propertyAccessor->getValue($oldEntity, $property->getName()); $newValue = $this->propertyAccessor->getValue($newEntity, $property->getName()); $changes[] = $this->buildChange($property, $oldValue, $newValue); } } return new ChangeSet($changes); }
/** * @param NewCommand $command */ public function handle(NewCommand $command) { $this->persistedEntities[] = $command->getEntity(); //After persisting entity we need to give it some unique identity $this->propertyAccessor->setValue($command->getEntity(), "id", time()); }
/** * @param mixed $defaultValue * @param mixed $newValue * @param mixed $targetObject */ public function execute($defaultValue, $newValue, $targetObject) { $newValueSnapshot = $this->snapshotMaker->makeSnapshotOf($newValue); $this->propertyAccessor->setValue($this->snapshot, (string) $this->lazyProperty->getName(), $newValueSnapshot); }