/** * Doctrine\ORM\EntityRepository.__call()と同じ動きで自前のfindBy関数を動かす * * @see Doctrine\ORM\EntityRepository::__call() */ public function __call($method, $arguments) { $by = null; switch (true) { case 0 === strpos($method, 'findCountBy'): $by = substr($method, 11); $method = 'findCountBy'; break; } // $by があれば、上記に定義した独自関数だったとする if (!is_null($by)) { $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { switch (count($arguments)) { case 1: return $this->{$method}(array($fieldName => $arguments[0])); case 2: return $this->{$method}(array($fieldName => $arguments[0]), $arguments[1]); case 3: return $this->{$method}(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]); case 4: return $this->{$method}(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]); default: // Do nothing } } } return parent::__call($method, $arguments); }
/** * Sets the attribute * * @param AttributeInterface $attribute * * @throws ColumnLabelException */ public function setAttribute(AttributeInterface $attribute = null) { $this->attribute = $attribute; if (null === $attribute) { $this->locale = null; $this->scope = null; $this->suffixes = $this->rawSuffixes; $this->propertyPath = lcfirst(Inflector::classify($this->name)); } else { if (!in_array($attribute->getBackendType(), [AbstractAttributeType::BACKEND_TYPE_REF_DATA_OPTION, AbstractAttributeType::BACKEND_TYPE_REF_DATA_OPTIONS])) { $this->propertyPath = $attribute->getBackendType(); } else { $this->propertyPath = $attribute->getReferenceDataName(); } $suffixes = $this->rawSuffixes; if ($attribute->isLocalizable()) { if (count($suffixes)) { $this->locale = array_shift($suffixes); } else { throw new ColumnLabelException('The column "%column%" must contain a locale code', ['%column%' => $this->label]); } } if ($attribute->isScopable()) { if (count($suffixes)) { $this->scope = array_shift($suffixes); } else { throw new ColumnLabelException('The column "%column%" must contain a scope code', ['%column%' => $this->label]); } } $this->suffixes = $suffixes; } }
/** * @inheritdoc */ public static function classify($word) { if (!isset(static::$cache['classify'][$word])) { static::$cache['classify'][$word] = parent::classify($word); } return static::$cache['classify'][$word]; }
public function __get($property) { if (count($this->getPropertiesMap()) > 0) { $map = $this->getPropertiesMap(); if (array_key_exists($property, $map)) { $methodName = $map[$property]; if (method_exists($this, $methodName)) { return $this->{$methodName}(); } elseif (method_exists($this->dataObject, $methodName)) { return $this->dataObject->{$methodName}(); } } throw new \LogicException("Undefined " . $property . " property."); } $methodName = 'get' . \Doctrine\Common\Util\Inflector::classify($property); if (method_exists($this, $methodName)) { return $this->{$methodName}(); } elseif (property_exists($this, $property)) { return $this->{$property}; } elseif (method_exists($this->dataObject, $methodName)) { return $this->dataObject->{$methodName}(); } elseif (property_exists($this->dataObject, $property)) { return $this->dataObject->{$property}; } throw new \LogicException("Undefined " . $methodName . " method or missing " . $property . " property."); }
/** * Sets the attribute * * @param AttributeInterface $attribute * * @throws ColumnLabelException */ public function setAttribute(AttributeInterface $attribute = null) { $this->attribute = $attribute; if (null === $attribute) { $this->locale = null; $this->scope = null; $this->suffixes = $this->rawSuffixes; $this->propertyPath = lcfirst(Inflector::classify($this->name)); } else { $this->propertyPath = $attribute->getBackendType(); $suffixes = $this->rawSuffixes; if ($attribute->isLocalizable()) { if (count($suffixes)) { $this->locale = array_shift($suffixes); } else { throw new ColumnLabelException('The column "%column%" must contain a locale code', array('%column%' => $this->label)); } } if ($attribute->isScopable()) { if (count($suffixes)) { $this->scope = array_shift($suffixes); } else { throw new ColumnLabelException('The column "%column%" must contain a scope code', array('%column%' => $this->label)); } } $this->suffixes = $suffixes; } }
private function _autofixeuroformat($object) { $formRequestData = $this->getRequest()->request->all(); if (!isset($formRequestData[$this->getForm()->getName()])) { return; } $formRequestData = $formRequestData[$this->getForm()->getName()]; $fields = $object->fieldsAsArray($object); foreach ($fields as $field => $value) { $fieldDescription = $this->getFormFieldDescription($field); if ($fieldDescription && ($type = $fieldDescription->getType())) { if ($type == 'money' || $type == 'number' || $type == 'float') { if (isset($formRequestData[$field])) { $method = ucfirst(\Doctrine\Common\Util\Inflector::classify($field)); $setMethod = 'set' . $method; $getMethod = 'get' . $method; $value = str_replace(array("\n", "\t", "\r"), " ", $formRequestData[$field]); $value = str_replace(array(' ', ','), array('', '.'), $value); $value = preg_replace('/[^(\\x20-\\x7F)]*/', '', $value); $object->{$setMethod}($value); } } } } }
/** * {@inheritdoc} */ public function transformName($name) { if ($this->ucFirst) { return Inflector::classify($name); } else { return Inflector::camelize($name); } }
public function testSetProperty() { $options = array('label' => 'my label', 'getter' => 'getFoo', 'sort_on' => 'foo', 'sortOn' => 'foo', 'dbType' => 'text', 'formType' => 'choices', 'formOptions' => array('foo' => 'bar'), 'filterType' => 'choice', 'filterOptions' => array('bar' => 'foo')); $column = new Column("test", false); foreach ($options as $option => $value) { $column->setProperty($option, $value); $this->assertEquals($value, call_user_func_array(array($column, 'get' . Inflector::classify($option)), array())); } }
public function setProperty($option, $value) { $setter = 'set' . Inflector::classify($option); if (method_exists($this, $setter)) { call_user_func_array(array($this, 'set' . $option), array($value)); } else { throw new InvalidOptionException($option, $this->name, $this->debug['generator'], $this->debug['builder']); } }
public function offsetGet($offset) { $method = Inflector::classify($offset); if (method_exists($this, "get{$method}")) { return $this->{"get{$method}"}(); } elseif (method_exists($this, "is{$method}")) { return $this->{"is{$method}"}(); } }
public function testSetOption() { $from_to_array = array('name' => 'Name', 'underscored_name' => 'Underscored name'); $options = array('label' => 'my label', 'getter' => 'getFoo', 'sort_on' => 'foo', 'sortOn' => 'foo', 'dbType' => 'text', 'formType' => 'choices', 'formOptions' => array('foo' => 'bar')); $column = new Column($from_to_array); foreach ($options as $option => $value) { $column->setOption($option, $value); $this->assertEquals($value, call_user_func_array(array($column, 'get' . Inflector::classify($option)), array())); } }
protected function getRelation($fieldName, $class = null) { $table = $this->getMetadatas($class); foreach ($table->getRelations() as $relation) { if (Inflector::classify($fieldName) == $relation->getName()) { return $relation; } } return false; }
/** * Initialises the controller. */ public function init() { /** @var \BedRest\Framework\Zend1\Container _bedRest */ $this->_bedRest = $this->getFrontController()->getParam('bootstrap')->getResource('bedrest'); $this->_bedRestRequest = new Request(); $this->_bedRestRequest->setResource(Inflector::classify($this->getRequest()->getControllerName())); $routeComponents = array('identifier' => $this->_getParam('id')); $this->_bedRestRequest->setRouteComponents($routeComponents); $data = file_get_contents('php://input'); $this->_bedRestRequest->setRawBody($data); $this->_helper->ViewRenderer->setNoRender(true); }
/** * @param Object $object * @param array $valuesToCompare */ public static function compareValues($object, $valuesToCompare) { foreach ($valuesToCompare as $key => $value) { $method = 'get' . Inflector::classify($key); if (method_exists($object, $method)) { if ($value != $object->{$method}()) { return false; } } } return (bool) $valuesToCompare; }
/** * * @param string $className * @return ClassMetadata */ protected function extractClassMetadata($className) { $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm); foreach ($driver->getAllClassNames() as $dbTableName) { if (strtolower(Inflector::classify($dbTableName)) != strtolower($className)) { continue; } $class = new ClassMetadataInfo($dbTableName); $driver->loadMetadataForClass($dbTableName, $class); return $class; } $this->fail("No class matching the name '" . $className . "' was found!"); }
/** * 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)); } }
public function addDateFilter($field, $value) { if (is_array($value)) { $filters = array(); if ($value['from']) { $filters['min'] = $value['from']->format('Y-m-d'); } if ($value['to']) { $filters['max'] = $value['to']->format('Y-m-d'); } if (count($filters) > 0) { $method = 'filterBy' . Inflector::classify($field); $this->query->{$method}($filters); } } elseif ($value instanceof \DateTime) { $this->query->filterBy($field, $value->format('Y-m-d')); } }
/** * @param FormEvent $event */ public function submit(FormEvent $event) { $block = $event->getForm()->getData(); if (null !== $block) { $blockAttributes = array(); foreach ($event->getForm()->all() as $key => $children) { $value = $children->getData(); if (in_array($key, $this->fixedParameters)) { $setter = 'set' . Inflector::classify($key); $block->{$setter}($value); continue; } if (is_string($value) && is_array(json_decode($value, true))) { $value = json_decode($value, true); } $blockAttributes[$key] = $value; } $block->setAttributes($blockAttributes); } }
public function addDateFilter($field, $value, $format = 'Y-m-d') { if (is_array($value)) { $filters = array(); if (array_key_exists('from', $value) && ($from = $this->formatDate($value['from'], $format))) { $filters['min'] = $from; } if (array_key_exists('to', $value) && ($to = $this->formatDate($value['to'], $format))) { $filters['max'] = $to; } if (count($filters) > 0) { $method = 'filterBy' . Inflector::classify($field); $this->query->{$method}($filters); } } else { if (false !== ($date = $this->formatDate($value, $format))) { $this->query->filterBy($field, $date); } } }
public function setProperty($option, $value) { $option = Inflector::classify($option); call_user_func_array(array($this, 'set' . $option), array($value)); }
/** * @param \ReflectionClass $reflClass * @param string $fieldName * * @return array [variable name, getter method name] */ protected function getFieldAccessInfo(\ReflectionClass $reflClass, $fieldName) { $getter = null; if ($reflClass->hasProperty($fieldName) && $reflClass->getProperty($fieldName)->isPublic()) { return [$fieldName, null]; } $name = Inflector::classify($fieldName); $getter = 'get' . $name; if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) { return [lcfirst($name), $getter]; } $getter = 'is' . $name; if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) { return [lcfirst($name), $getter]; } return [null, null]; }
/** * Returns the mapped class name for a table if it exists. Otherwise return "classified" version. * * @param string $tableName * * @return string */ private function getClassNameForTable($tableName) { if (isset($this->classNamesForTables[$tableName])) { return $this->namespace . $this->classNamesForTables[$tableName]; } return $this->namespace . Inflector::classify(strtolower($tableName)); }
/** * Adds support for magic finders. * * @return array|object The found entity/entities. * @throws BadMethodCallException If the method called is an invalid find* method * or no find* method at all and therefore an invalid * method call. */ public function __call($method, $arguments) { switch (true) { case 0 === strpos($method, 'findBy'): $by = substr($method, 6); $method = 'findBy'; break; case 0 === strpos($method, 'findOneBy'): $by = substr($method, 9); $method = 'findOneBy'; break; default: throw new \BadMethodCallException("Undefined method '{$method}'. The method name must start with " . "either findBy or findOneBy!"); } if (empty($arguments)) { throw ORMException::findByRequiresParameter($method . $by); } $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { switch (count($arguments)) { case 1: return $this->{$method}(array($fieldName => $arguments[0])); case 2: return $this->{$method}(array($fieldName => $arguments[0]), $arguments[1]); case 3: return $this->{$method}(array($fieldName => $arguments[0]), $arguments[1], $arguments[2]); case 4: return $this->{$method}(array($fieldName => $arguments[0]), $arguments[1], $arguments[2], $arguments[3]); default: // Do nothing } } throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method . $by); }
private function _generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { if ($type == "add") { $addMethod = explode("\\", $typeHint); $addMethod = end($addMethod); $methodName = $type . $addMethod; } else { $methodName = $type . Inflector::classify($fieldName); } if ($this->_hasMethod($methodName, $metadata)) { return; } $this->_staticReflection[$metadata->name]['methods'][] = $methodName; $var = sprintf('_%sMethodTemplate', $type); $template = self::${$var}; $variableType = $typeHint ? $typeHint . ' ' : null; $types = \Doctrine\DBAL\Types\Type::getTypesMap(); $methodTypeHint = $typeHint && !isset($types[$typeHint]) ? '\\' . $typeHint . ' ' : null; $replacements = array('<description>' => ucfirst($type) . ' ' . $fieldName, '<methodTypeHint>' => $methodTypeHint, '<variableType>' => $variableType, '<variableName>' => Inflector::camelize($fieldName), '<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); }
/** * Adds support for magic finders. * * @return array|object The found document/documents. * @throws BadMethodCallException If the method called is an invalid find* method * or no find* method at all and therefore an invalid * method call. */ public function __call($method, $arguments) { if (substr($method, 0, 6) == 'findBy') { $by = substr($method, 6, strlen($method)); $method = 'findBy'; } elseif (substr($method, 0, 9) == 'findOneBy') { $by = substr($method, 9, strlen($method)); $method = 'findOneBy'; } else { throw new \BadMethodCallException("Undefined method '{$method}'. The method name must start with " . "either findBy or findOneBy!"); } if (!isset($arguments[0])) { throw MongoDBException::findByRequiresParameter($method . $by); } $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); if ($this->_class->hasField($fieldName)) { return $this->{$method}(array($fieldName => $arguments[0])); } else { throw MongoDBException::invalidFindByCall($this->_documentName, $fieldName, $method . $by); } }
private function _generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null) { $methodName = $type . Inflector::classify($fieldName); if ($this->_hasMethod($methodName, $metadata)) { return; } $var = sprintf('_%sMethodTemplate', $type); $template = self::${$var}; $variableType = $typeHint ? $typeHint . ' ' : null; $types = \Doctrine\DBAL\Types\Type::getTypesMap(); $methodTypeHint = $typeHint && !isset($types[$typeHint]) ? '\\' . $typeHint . ' ' : null; $replacements = array('<description>' => ucfirst($type) . ' ' . $fieldName, '<methodTypeHint>' => $methodTypeHint, '<variableType>' => $variableType, '<variableName>' => Inflector::camelize($fieldName), '<methodName>' => $methodName, '<fieldName>' => $fieldName); $method = str_replace(array_keys($replacements), array_values($replacements), $template); return $this->_prefixCodeWithSpaces($method); }
private function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { $methodName = $type . Inflector::classify($fieldName); if (in_array($type, array("add", "remove")) && substr($methodName, -1) == "s") { $methodName = substr($methodName, 0, -1); } 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 . ' '; } $replacements = array('<description>' => ucfirst($type) . ' ' . $fieldName, '<methodTypeHint>' => $methodTypeHint, '<variableType>' => $variableType, '<variableName>' => Inflector::camelize($fieldName), '<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 $instance * @param $properties */ public function setProperties($instance, $properties) { if (!array_key_exists('created_at', $properties)) { $properties['created_at'] = new \DateTime(); } if (!array_key_exists('updated_at', $properties)) { $properties['updated_at'] = null; } $entity = get_class($instance); $fieldNames = $this->getEntityManager()->getClassMetadata($entity)->getFieldNames(); $missedProperties = []; foreach ($properties as $name => $value) { $classifiedName = Inflector::classify($name); if (in_array(lcfirst($classifiedName), $fieldNames)) { $method = 'set' . $classifiedName; $instance->{$method}($value); } else { $missedProperties[] = $name . ': ' . $value; } } if (count($missedProperties) > 0) { $output = 'property missed at introspection for entity ' . $entity . "\n" . implode("\n", $missedProperties) . "\n"; echo $output; } return $instance; }
/** * Adds support for magic finders. * * @return array|object The found entity/entities. * @throws BadMethodCallException If the method called is an invalid find* method * or no find* method at all and therefore an invalid * method call. */ public function __call($method, $arguments) { if (substr($method, 0, 6) == 'findBy') { $by = substr($method, 6, strlen($method)); $method = 'findBy'; } else { if (substr($method, 0, 9) == 'findOneBy') { $by = substr($method, 9, strlen($method)); $method = 'findOneBy'; } else { throw new \BadMethodCallException("Undefined method '{$method}'. The method name must start with " . "either findBy or findOneBy!"); } } if (!isset($arguments[0])) { // we dont even want to allow null at this point, because we cannot (yet) transform it into IS NULL. throw ORMException::findByRequiresParameter($method . $by); } $fieldName = lcfirst(\Doctrine\Common\Util\Inflector::classify($by)); if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) { return $this->{$method}(array($fieldName => $arguments[0])); } else { throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method . $by); } }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadataInfo $metadata) { $tableName = $className; $className = Inflector::classify($tableName); $metadata->name = $className; $metadata->table['name'] = $tableName; $columns = $this->_sm->listTableColumns($tableName); if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) { $foreignKeys = $this->_sm->listTableForeignKeys($tableName); } else { $foreignKeys = array(); } $indexes = $this->_sm->listTableIndexes($tableName); $ids = array(); $fieldMappings = array(); foreach ($columns as $column) { // Skip columns that are foreign keys foreach ($foreignKeys as $foreignKey) { if (in_array(strtolower($column->getName()), array_map('strtolower', $foreignKey->getColumns()))) { continue 2; } } $fieldMapping = array(); if (isset($indexes['primary']) && in_array($column->getName(), $indexes['primary']->getColumns())) { $fieldMapping['id'] = true; } $fieldMapping['fieldName'] = Inflector::camelize(strtolower($column->getName())); $fieldMapping['columnName'] = $column->getName(); $fieldMapping['type'] = strtolower((string) $column->getType()); if ($column->getType() instanceof \Doctrine\DBAL\Types\StringType) { $fieldMapping['length'] = $column->getLength(); $fieldMapping['fixed'] = $column->getFixed(); } else { if ($column->getType() instanceof \Doctrine\DBAL\Types\IntegerType) { $fieldMapping['unsigned'] = $column->getUnsigned(); } } $fieldMapping['notnull'] = $column->getNotNull(); if (isset($fieldMapping['id'])) { $ids[] = $fieldMapping; } else { $fieldMappings[] = $fieldMapping; } } if ($ids) { if (count($ids) == 1) { $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); } foreach ($ids as $id) { $metadata->mapField($id); } } foreach ($fieldMappings as $fieldMapping) { $metadata->mapField($fieldMapping); } foreach ($foreignKeys as $foreignKey) { $cols = $foreignKey->getColumns(); $localColumn = current($cols); $fkCols = $foreignKey->getForeignColumns(); $associationMapping = array(); $associationMapping['fieldName'] = Inflector::camelize(str_ireplace('_id', '', $localColumn)); $associationMapping['targetEntity'] = Inflector::classify($foreignKey->getForeignTableName()); for ($i = 0; $i < count($cols); $i++) { $associationMapping['joinColumns'][] = array('name' => $cols[$i], 'referencedColumnName' => $fkCols[$i]); } $metadata->mapManyToOne($associationMapping); } }