/** * @param \ReflectionClass $class * @param ResourceEntityInterface $entity * @param array $relationships * @return self */ protected function setRelationships(\ReflectionClass $class, ResourceEntityInterface $entity, array $relationships) { foreach ($relationships as $relationship => $value) { $camelCased = Inflector::camelize($relationship); if (is_array($value)) { $getter = DefaultMutator::GET . $camelCased; $singular = Inflector::singularize($camelCased); $remover = DefaultMutator::REMOVE . $singular; $adder = DefaultMutator::ADD . $singular; // @todo Improve algorithm. foreach ($entity->{$getter}() as $item) { $entity->{$remover}($item); } foreach ($value as $item) { $entity->{$adder}($item); } } else { $method = DefaultMutator::SET . $camelCased; if ($class->hasMethod($method)) { $entity->{$method}($value); } } } return $this; }
/** * Serialize a collection. * * @param string $resourceKey * @param array $data * * @return array */ public function collection($resourceKey, array $data) { $dataResponse = []; $resourceKey = strtolower($resourceKey); $pluralize = Inflector::pluralize($resourceKey); $singularize = Inflector::singularize($resourceKey); if (!$this->scope->hasParent()) { $dataResponse[$pluralize] = [$singularize => $data]; } else { $dataResponse = [$singularize => $data]; } return $dataResponse; }
/** * Searches for add and remove methods. */ protected function findAdderAndRemover() { $reflClass = new \ReflectionClass($this->object); $propertyName = Inflector::classify(Inflector::singularize($this->propertyName)); $addMethod = 'add' . $propertyName; $removeMethod = 'remove' . $propertyName; $addMethodFound = $this->isAccessible($reflClass, $addMethod, 1); $removeMethodFound = $this->isAccessible($reflClass, $removeMethod, 1); if ($addMethodFound && $removeMethodFound) { $this->addMethod = $addMethod; $this->removeMethod = $removeMethod; } if (!$addMethodFound || !$removeMethodFound) { throw new NoSuchPropertyException(sprintf('Not found a public methods "%s()" and/or "%s()" on class %s', $addMethod, $removeMethod, $reflClass->name)); } }
/** * Get relation query * * @return \Blast\Orm\Query */ public function getQuery() { if (null !== $this->query) { return $this->query; } $provider = $this->createProvider($this->getEntity()); $foreignProvider = $this->createProvider($this->getForeignEntity()); $foreignKey = $this->getForeignKey(); $junction = $this->getJunction(); $junctionLocalKey = $this->getJunctionLocalKey(); $junctionForeignKey = $this->getJunctionForeignKey(); $data = $provider->extract(); $localKey = $provider->getDefinition()->getPrimaryKeyName(); //determine foreign key if ($foreignKey === null) { $foreignKey = $foreignProvider->getDefinition()->getPrimaryKeyName(); } //determine through if (!is_string($junction) || $junction === null) { $junction = Inflector::singularize($provider->getDefinition()->getTableName()) . '_' . Inflector::singularize($foreignProvider->getDefinition()->getTableName()); } //determine through local key if ($junctionLocalKey === null) { $junctionLocalKey = Inflector::singularize($provider->getDefinition()->getTableName()) . '_' . $localKey; } //determine through foreign key if ($junctionForeignKey === null) { $junctionForeignKey = Inflector::singularize($foreignProvider->getDefinition()->getTableName()) . '_' . $foreignKey; } $query = new Query($provider->getDefinition()->getMapper()->getConnection()); //prepare query for foreign table $foreignQuery = $foreignProvider->getDefinition()->getMapper()->setConnection($this->getConnection())->select(); //get relations by through db object if (isset($data[$localKey])) { $junctionProvider = is_string($junction) ? $this->createProvider($junction) : $junction; $junctionMapper = $junctionProvider->getDefinition()->getMapper(); $junctionMapper->setConnection($this->getConnection()); if (true) { } $results = $junctionMapper->select([$junctionForeignKey])->where($query->expr()->eq($junctionLocalKey, $data[$localKey]))->execute(HydratorInterface::HYDRATE_RAW); //set conditions on foreign query foreach ($results as $result) { $foreignQuery->where($query->expr()->eq($foreignKey, $result[$junctionForeignKey])); } } $this->query = $foreignQuery; return $this->query; }
/** * @param ClassMetadataInfo $metadata * @param string $type * @param string $fieldName * @param string|null $typeHint * @param string|null $defaultValue * * @return string */ protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { $methodName = $type . Inflector::classify($fieldName); $variableName = Inflector::camelize($fieldName); if (in_array($type, array("add", "remove"))) { $methodName = Inflector::singularize($methodName); $variableName = Inflector::singularize($variableName); } if ($this->hasMethod($methodName, $metadata)) { return ''; } $this->staticReflection[$metadata->name]['methods'][] = strtolower($methodName); $var = sprintf('%sMethodTemplate', $type); $template = static::${$var}; $methodTypeHint = null; $types = Type::getTypesMap(); $variableType = $typeHint ? $this->getType($typeHint) : null; if ($typeHint && !isset($types[$typeHint])) { $variableType = '\\' . ltrim($variableType, '\\'); $methodTypeHint = '\\' . $typeHint . ' '; } $replacements = array('<description>' => ucfirst($type) . ' ' . $variableName, '<methodTypeHint>' => $methodTypeHint, '<variableType>' => $variableType, '<variableName>' => $variableName, '<methodName>' => $methodName, '<fieldName>' => $fieldName, '<variableDefault>' => $defaultValue !== null ? ' = ' . $defaultValue : '', '<entity>' => $this->getClassName($metadata)); $method = str_replace(array_keys($replacements), array_values($replacements), $template); return $this->prefixCodeWithSpaces($method); }
/** * @param \Doctrine\ORM\Mapping\ClassMetadata $meta * @param \Nette\ComponentModel\Component $component * @param mixed $entity * @return boolean */ public function save(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof BaseControl) { return false; } $name = $component->getOption(self::FIELD_NAME, $component->getName()); $value = $component->getValue(); if ($this->accessor->isWritable($entity, $name) && !$meta->hasAssociation($name)) { try { $this->accessor->setValue($entity, $name, $value); return true; } catch (\Kdyby\Doctrine\MemberAccessException $e) { } } if (!$meta->hasAssociation($name)) { return false; } $value = $component->getValue(); $entityClass = $this->relatedMetadata($entity, $name)->getName(); $repository = $this->entityManager->getRepository($entityClass); if ($meta->isCollectionValuedAssociation($name)) { $property = \Doctrine\Common\Util\Inflector::singularize($name); foreach ($repository->findAll() as $associatedEntity) { if (in_array($associatedEntity->id, $value)) { $hasMethod = 'has' . ucfirst($property); if (!$entity->{$hasMethod}($associatedEntity)) { $addMethod = 'add' . ucfirst($property); $entity->{$addMethod}($associatedEntity); } } else { $removeMethod = 'remove' . ucfirst($property); $entity->{$removeMethod}($associatedEntity); } } } elseif ($value === null || ($value = $repository->find($value))) { if ($this->accessor->isWritable($entity, $name)) { try { $this->accessor->setValue($entity, $name, $value); } catch (\Kdyby\Doctrine\MemberAccessException $e) { return false; } } } return true; }
/** * {@inheritdoc} */ protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { $methodName = $type . Inflector::classify($fieldName); if (in_array($type, array("add", "remove"))) { $methodName = Inflector::singularize($methodName); } if ($this->hasMethod($methodName, $metadata)) { return ''; } $this->staticReflection[$metadata->name]['methods'][] = $methodName; $var = sprintf('%sMethodTemplate', $type); $template = self::${$var}; $methodTypeHint = null; $types = Type::getTypesMap(); $variableType = $typeHint ? $this->getType($typeHint) . ' ' : null; if ($typeHint && !isset($types[$typeHint])) { $variableType = '\\' . ltrim($variableType, '\\'); $methodTypeHint = '\\' . $typeHint . ' '; } elseif ($variableType[0] == '\\') { $variableType = '\\' . ltrim($variableType, '\\'); $methodTypeHint = '\\' . ltrim($variableType, '\\'); } switch (trim($variableType)) { case 'bool': case 'boolean': $variableCast = '(bool) '; break; case 'int': case 'integer': $variableCast = '(int) '; break; case 'float': case 'double': $variableCast = '(float) '; break; case 'string': $variableCast = '(string) '; break; default: $variableCast = ''; } $variableName = Inflector::camelize($fieldName); if ($metadata->hasField($fieldName) && $metadata->isNullable($fieldName)) { $nullable = true; } else { $nullable = false; } if ($nullable && $variableCast) { $variableCast = sprintf('$%s === null ? null : %s', $variableName, $variableCast); } $replacements = array('<description>' => ucfirst($type) . ' ' . $fieldName, '<methodTypeHint>' => $methodTypeHint, '<variableType>' => $variableType, '<variableCast>' => $variableCast, '<variableName>' => $variableName, '<methodName>' => $methodName, '<fieldName>' => $fieldName, '<variableDefault>' => $defaultValue !== null ? ' = ' . $defaultValue : ($nullable ? ' = null' : ''), '<entity>' => $this->getClassName($metadata)); $method = str_replace(array_keys($replacements), array_values($replacements), $template); return $this->prefixCodeWithSpaces($method); }