/** * @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; }
/** * @inheritdoc */ public static function camelize($word) { if (!isset(static::$cache['camelize'][$word])) { static::$cache['camelize'][$word] = parent::camelize($word); } return static::$cache['camelize'][$word]; }
/** * Creates and returns one object based on the given data and metadata * * @param $class object's class name * @param $data array of the object's fixture data * @param $metadata the class metadata for doctrine * @param $embedded true for embedded documents * @return Object */ public function createObject($class, $data, $metadata, $options = array()) { // options to state if a document is to be embedded or persisted on its own $embedded = isset($options['embedded']); $mapping = array_keys($metadata->fieldMappings); // Instantiate new object $object = new $class(); foreach ($data as $field => $value) { // Add the fields defined in the fixtures file $method = Inflector::camelize('set_' . $field); // This is a standard field if (in_array($field, $mapping)) { // Dates need to be converted to DateTime objects $type = $metadata->fieldMappings[$field]['type']; if ($type == 'many') { $method = Inflector::camelize('add_' . $field); // EmbedMany if (isset($metadata->fieldMappings[$field]['embedded']) && $metadata->fieldMappings[$field]['embedded']) { foreach ($value as $embedded_value) { $embed_class = $metadata->fieldMappings[$field]['targetDocument']; $embed_data = $embedded_value; $embed_meta = $this->getMetaDataForClass($embed_class); $value = $this->createObject($embed_class, $embed_data, $embed_meta, array('embedded' => true)); $object->{$method}($value); } //ReferenceMany } else { foreach ($value as $reference_object) { $object->{$method}($this->loader->getReference($reference_object)); } } } else { if ($type == 'datetime' || $type == 'date' || $type == 'time') { $value = new \DateTime($value); } if ($type == 'one') { // EmbedOne if (isset($metadata->fieldMappings[$field]['embedded']) && $metadata->fieldMappings[$field]['embedded']) { $embed_class = $metadata->fieldMappings[$field]['targetDocument']; $embed_data = $value; $embed_meta = $this->getMetaDataForClass($embed_class); $value = $this->createObject($embed_class, $embed_data, $embed_meta, array('embedded' => true)); // ReferenceOne } else { $value = $this->loader->getReference($value); } } $object->{$method}($value); } } else { // The key is not a field's name but the name of a method to be called $object->{$method}($value); } } // Save a reference to the current object if (!$embedded) { $this->runServiceCalls($object); } return $object; }
public function load(ObjectManager $manager) { $yaml = new Parser(); // TODO: find a way of obtainin Bundle's path with the help of $this->container $bpath = $this->container->get('kernel')->getBundle('SiwappEstimateBundle')->getPath(); $value = $yaml->parse(file_get_contents($bpath . '/DataFixtures/estimates.yml')); foreach ($value['Item'] as $ref => $values) { $item = new Item(); $estimate = new Estimate(); foreach ($values as $fname => $fvalue) { if ($fname == 'Estimate') { $fvalue = $manager->merge($this->getReference($fvalue)); $fvalue->addItem($item); $manager->persist($fvalue); } $method = 'set' . Inflector::camelize($fname); if (is_callable(array($item, $method))) { call_user_func(array($item, $method), $fvalue); } } $manager->persist($item); $manager->flush(); $this->addReference($ref, $item); } foreach ($value['ItemTax'] as $ref => $values) { $item = $this->getReference($values['Item']); $tax = $this->getReference($values['Tax']); $item->addTax($tax); $manager->persist($item); $manager->flush(); } }
/** * Execute certain hook handler * * @return void */ public function executeHookHandler() { // It's the metadata collected by Doctrine foreach ($this->getMetadata() as $main) { $node = static::getClassesTree()->find($main->name); // Process only certain classes if (!$node->isTopLevelNode() && !$node->isDecorator()) { foreach ($main->fieldMappings as $field => $info) { if ('money' == $info['type']) { $fieldName = \Includes\Utils\Converter::convertToCamelCase($field); $purposes = array('net' => ''); $behaviors = array(); if (isset($info['options']) && is_array($info['options'])) { foreach ($info['options'] as $option) { if ($option instanceof \XLite\Core\Doctrine\Annotation\Behavior) { $behaviors = array_merge($behaviors, $option->list); } elseif ($option instanceof \XLite\Core\Doctrine\Annotation\Purpose) { $purposes[$option->name] = $option->source; } } } foreach ($purposes as $purpose => $source) { $camelField = ucfirst(\Doctrine\Common\Util\Inflector::camelize($field)); $source = $source ? ucfirst($source) . $camelField : $camelField; $this->addReplacement($main, 'get', array('<getter>' => 'get' . $source, '<fieldName>' => $fieldName, '<methodName>' => ucfirst($purpose) . $camelField, '<behaviors>' => $behaviors ? '\'' . implode('\',\'', $behaviors) . '\'' : '', '<purpose>' => $purpose)); } } } } } // Populate changes $this->writeData(); }
public function load(ObjectManager $manager) { $yaml = new Parser(); $bpath = $this->container->get('kernel')->getBundle('SiwappRecurringInvoiceBundle')->getPath(); $value = $yaml->parse(file_get_contents($bpath . '/DataFixtures/recurring_invoices.yml')); foreach ($value['Item'] as $ref => $values) { $item = new Item(); $recurring_invoice = new RecurringInvoice(); foreach ($values as $fname => $fvalue) { if ($fname == 'RecurringInvoice') { $fvalue = $manager->merge($this->getReference($fvalue)); } $method = 'set' . Inflector::camelize($fname); if (is_callable(array($item, $method))) { call_user_func(array($item, $method), $fvalue); } } $manager->persist($item); $manager->flush(); $this->addReference($ref, $item); } foreach ($value['ItemTax'] as $ref => $values) { $item = $this->getReference($values['Item']); $tax = $this->getReference($values['Tax']); $item->addTax($tax); $manager->persist($item); $manager->flush(); } }
/** * {@inheritdoc} */ public function transformName($name) { if ($this->ucFirst) { return Inflector::classify($name); } else { return Inflector::camelize($name); } }
public function load(ObjectManager $manager, $tags = null) { if (!$this->hasTag($tags)) { return; } $cmf = $manager->getMetadataFactory(); // The model class for all fixtures defined in this file $class = $this->file['model']; // Get the fields that are not "associations" $metadata = $cmf->getMetaDataFor($class); $mapping = array_keys($metadata->fieldMappings); $associations = array_keys($metadata->associationMappings); foreach ($this->file['fixtures'] as $reference => $fixture) { // Instantiate new object $object = new $class(); foreach ($fixture as $field => $value) { // Add the fields defined in the fistures file $method = Inflector::camelize('set_' . $field); // if (strpos($value, '$') === 0) { // custom reference loader format: $<referencedEntityKey>|<referencedColumn> // ex: $first_campaign|idcampaign list($referencedEntity, $referenceColumn) = explode('|', ltrim($value, '$')); $getterMethod = Inflector::camelize('get_' . $referenceColumn); $object->{$method}($this->loader->getReference($referencedEntity)->{$getterMethod}()); } elseif (in_array($field, $mapping)) { // Dates need to be converted to DateTime objects $type = $metadata->fieldMappings[$field]['type']; if ($type == 'datetime' or $type == 'date') { $value = new \DateTime($value); } $object->{$method}($value); } else { if (in_array($field, $associations)) { // This field is an association, we load it from the references $object->{$method}($this->loader->getReference($value)); } else { // It's a method call that will set a field named differently // eg: FOSUserBundle ->setPlainPassword sets the password after // Encrypting it $object->{$method}($value); } } } // Save a reference to the current object $this->loader->setReference($reference, $object); if (!$this->isReverseSaveOrder()) { $manager->persist($object); } } if ($this->isReverseSaveOrder()) { $refs = array_keys($this->file['fixtures']); for ($i = count($refs) - 1; $i >= 0; $i--) { $manager->persist($this->loader->getReference($refs[$i])); } } $manager->flush(); }
/** * @param $data */ public function setData($data) { foreach ($data as $field => $value) { //Ex: user_id => user id => userId => UserId => setUserId $setter_method_name = Inflector::camelize(str_replace('_', ' ', $field)); $setter_method_name = ucfirst($setter_method_name); $setter_method_name = 'set' . $setter_method_name; $this->{$setter_method_name}($value); } return $data; }
/** * convert to array from object. * * @return array */ public function toArray() : array { $result = []; foreach ($this->getObjectVars() as $property) { $method = Inflector::camelize('get_' . $property); if (method_exists($this, $method)) { $result[$property] = $this->{$method}(); } } return $result; }
public function testTimestamps() { $entity = new Document(); foreach (['created', 'updated'] as $field) { $setter = sprintf('set%s', ucfirst(Inflector::camelize($field))); $getter = sprintf('get%s', ucfirst(Inflector::camelize($field))); $this->assertNull($entity->{$getter}()); $fluent = $entity->{$setter}(new \DateTime()); $this->assertEquals($entity, $fluent, 'Fluent interface is not working'); $this->assertNotNull($entity->{$getter}()); } }
/** * @param $rawCart * @return Cart * @throws ApiTransformationException */ public function transform($rawCart) { $cart = new Cart(); foreach (json_decode($rawCart) as $key => $value) { $setter = 'set' . ucfirst(Inflector::camelize($key)); if (!method_exists($cart, $setter)) { throw new ApiTransformationException(); } $cart->{$setter}($value); } return $cart; }
/** * Calls methods from the specified object in underscore case. * * @param object $object * @param string $method * @param mixed $parameters * @param object|null $anotherObject * @return mixed */ public static function call($object, $method, $parameters, $anotherObject = null) { $method = \Doctrine\Common\Util\Inflector::camelize($method); $result = $object; if (is_null($anotherObject)) { $anotherObject = $object; } if (method_exists($anotherObject, $method)) { $result = call_user_func_array([$anotherObject, $method], $parameters); } return $result; }
/** * Generate page url from name and parameters. * * @param string $page * @param array $parameters * * @return string */ private function generatePageUrl($page, array $parameters = array()) { $parts = explode(' ', trim($page), 2); if (2 === count($parts)) { $parts[1] = Inflector::camelize($parts[1]); } $route = implode('_', $parts); $routes = $this->getContainer()->get('router')->getRouteCollection(); if (null === $routes->get($route)) { $route = 'app_' . $route; } return $this->getMinkParameter('base_url') . $this->generateUrl($route, $parameters); }
/** * @param string $title * @param string $class * @param array $services * @param array $parameters * * @return ItemStep */ public function createStep($title, $class, array $services, array $parameters) { $step = new $class($title); $step->setEventDispatcher($this->eventDispatcher); $step->setJobRepository($this->jobRepository); foreach ($services as $setter => $service) { $method = 'set' . Inflector::camelize($setter); $step->{$method}($service); } foreach ($parameters as $setter => $param) { $method = 'set' . Inflector::camelize($setter); $step->{$method}($param); } return $step; }
/** * @dataProvider integrationSettingFieldsProvider * * @param string $fieldName */ public function testIntegrationSettings($fieldName) { $accessor = PropertyAccess::createPropertyAccessor(); $referenceGetter = Inflector::camelize('get_' . $fieldName . '_reference'); $this->assertTrue(method_exists($this->entity, $referenceGetter)); $value = $accessor->getValue($this->entity, $fieldName); $this->assertNotEmpty($value); $this->assertInstanceOf('Oro\\Bundle\\DataGridBundle\\Common\\Object', $value); $newValue = Object::create([]); $accessor->setValue($this->entity, $fieldName, $newValue); $this->assertNotSame($value, $this->entity->{$referenceGetter}()); $this->assertEquals($newValue, $accessor->getValue($this->entity, $fieldName)); $this->assertNotSame($newValue, $accessor->getValue($this->entity, $fieldName)); $this->assertSame($newValue, $this->entity->{$referenceGetter}()); }
public function load(ObjectManager $manager) { $series = array('Serie_1' => array('name' => 'Internet', 'value' => 'ASET-', 'first_number' => 1, 'enabled' => true), 'Serie_2' => array('name' => 'Design', 'value' => 'BSET-', 'first_number' => 4, 'enabled' => true), 'Serie_3' => array('name' => 'Others', 'value' => 'CSET-', 'first_number' => 1, 'enabled' => true)); foreach ($series as $ref => $values) { $serie = new Serie(); foreach ($values as $fname => $fvalue) { $method = 'set' . Inflector::camelize($fname); if (is_callable(array($serie, $method))) { call_user_func(array($serie, $method), $fvalue); } } $manager->persist($serie); $manager->flush(); $this->addReference($ref, $serie); } }
public function load(ObjectManager $manager) { $taxes = array('Tax_1' => array('name' => 'IVA 16%', 'value' => 16, 'active' => true, 'is_default' => true), 'Tax_2' => array('name' => 'IVA 4%', 'value' => 4, 'active' => true, 'is_default' => false), 'Tax_3' => array('name' => 'IVA 7%', 'value' => 7, 'active' => false, 'is_default' => false), 'Tax_4' => array('name' => 'IRPF', 'value' => -15, 'active' => true, 'is_default' => true)); foreach ($taxes as $ref => $values) { $tax = new Tax(); foreach ($values as $fname => $fvalue) { $method = 'set' . Inflector::camelize($fname); if (is_callable(array($tax, $method))) { call_user_func(array($tax, $method), $fvalue); } } $manager->persist($tax); $manager->flush(); $this->addReference($ref, $tax); } }
public static function hydrateObject($entityClass, $data) { $reader = new AnnotationReader(); $reflectionObj = new \ReflectionObject(new $entityClass()); $aAnnotations = array(); foreach ($data as $key => $value) { $property = Inflector::camelize($key); if ($reflectionObj->hasProperty($property)) { $reflectionProp = new \ReflectionProperty($entityClass, $property); $relation = $reader->getPropertyAnnotation($reflectionProp, 'Weysan\\DoctrineImgBundle\\Annotations\\ImgResize'); if ($relation) { $aAnnotations[] = array(0 => $relation->getVars(), 1 => $property); } } } return $aAnnotations; }
public function load(ObjectManager $manager) { $yaml = new Parser(); $bpath = $this->container->get('kernel')->getBundle('SiwappCustomerBundle')->getPath(); $value = $yaml->parse(file_get_contents($bpath . '/DataFixtures/customers.yml')); foreach ($value['Customer'] as $ref => $values) { $customer = new Customer(); foreach ($values as $fname => $fvalue) { $method = 'set' . Inflector::camelize($fname); if (is_callable(array($customer, $method))) { call_user_func(array($customer, $method), $fvalue); } } $manager->persist($customer); $manager->flush(); $this->addReference($ref, $customer); } }
public function getSlotConfiguration() { if (!$this->units) { $adUnits = array(); foreach ($this->positions as $key => $position) { $tmp = array(); foreach ($position as $attributeKey => $data) { if ($attributeKey == 'slot_name') { $data = "/{$this->networkId}/{$data}"; } $tmp[Inflector::camelize($attributeKey)] = $data; } $adUnits[] = $tmp; } $this->units = $adUnits; } return $this->units; }
public function load() { if ($this->callback) { $scriptName = 'runCallback.php'; } else { $scriptName = Inflector::camelize($this->mapperName) . '.php'; } $closurePath = $this->resourcesDir . '/mappers/closures/' . $scriptName; if (!file_exists($closurePath)) { throw new \InvalidArgumentException(sprintf('Invalid path to closure ("%s")', $closurePath)); } else { if ($this->callback) { // Required by closure below $callback = $this->callback; } $closure = (require $closurePath); return $closure; } }
public function load(ObjectManager $manager) { $yaml = new Parser(); // TODO: find a way of obtainin Bundle's path with the help of $this->container $bpath = $this->container->get('kernel')->getBundle('SiwappProductBundle')->getPath(); $value = $yaml->parse(file_get_contents($bpath . '/DataFixtures/products.yml')); foreach ($value['Product'] as $ref => $values) { $product = new Product(); foreach ($values as $fname => $fvalue) { $method = Inflector::camelize('set_' . $fname); if (is_callable(array($product, $method))) { call_user_func(array($product, $method), $fvalue); } } $manager->persist($product); $manager->flush(); $this->addReference($ref, $product); } }
/** * @param $to * @param array $data * @return $this */ public function hasMailTo($to, array $data = array()) { /**@var $message \Swift_Message*/ foreach ($this->getMailCollector()->getMessages() as $message) { if (key($message->getTo()) == $to) { foreach ($data as $key => $value) { $methodValue = $message->{'get' . Inflector::camelize($key)}(); if (strtolower($key) == 'body') { Assert::assertContains($value, $methodValue); } else { Assert::assertEquals($value, $methodValue); } } return $this; } } Assert::assertTrue(false, 'Not has mail to ' . $to); return $this; }
protected function setUp() { $this->importProcessor = $this->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\ImportExport\\Processor\\StepExecutionAwareImportProcessor')->disableOriginalConstructor()->getMock(); $this->exportProcessor = $this->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\ImportExport\\Processor\\StepExecutionAwareExportProcessor')->disableOriginalConstructor()->getMock(); $this->importProcessor->expects($this->any())->method('process')->will($this->returnCallback(function ($item) { $keys = array_map(function ($key) { return Inflector::camelize($key); }, array_keys($item)); return (object) array_combine($keys, array_values($item)); })); $this->exportProcessor->expects($this->any())->method('process')->will($this->returnCallback(function ($item) { $item = (array) $item; $keys = array_map(function ($key) { return Inflector::tableize($key); }, array_keys($item)); return array_combine($keys, array_values($item)); })); $this->strategy = new TwoWaySyncStrategy($this->importProcessor, $this->exportProcessor); }
public function load(ObjectManager $manager) { $yaml = new Parser(); $bpath = $this->container->get('kernel')->getBundle('SiwappEstimateBundle')->getPath(); $value = $yaml->parse(file_get_contents($bpath . '/DataFixtures/estimates.yml')); foreach ($value['Estimate'] as $ref => $values) { $estimate = new Estimate(); foreach ($values as $fname => $fvalue) { if ($fname == 'Serie') { $fvalue = $manager->merge($this->getReference($fvalue)); } $method = 'set' . Inflector::camelize($fname); if (is_callable(array($estimate, $method))) { call_user_func(array($estimate, $method), $fvalue); } } $manager->persist($estimate); $manager->flush(); $this->addReference($ref, $estimate); } }
public function load(ObjectManager $manager) { $yaml = new Parser(); // TODO: find a way of obtainin Bundle's path with the help of $this->container $bpath = $this->container->get('kernel')->getBundle('SiwappRecurringInvoiceBundle')->getPath(); $value = $yaml->parse(file_get_contents($bpath . '/DataFixtures/recurring_invoices.yml')); foreach ($value['RecurringInvoice'] as $ref => $values) { $recurring_invoice = new RecurringInvoice(); foreach ($values as $fname => $fvalue) { if ($fname == 'Serie') { $fvalue = $manager->merge($this->getReference($fvalue)); } $method = 'set' . Inflector::camelize($fname); if (is_callable(array($recurring_invoice, $method))) { call_user_func(array($recurring_invoice, $method), $fvalue); } } $manager->persist($recurring_invoice); $manager->flush(); $this->addReference($ref, $recurring_invoice); } }
public function load(ObjectManager $manager) { $yaml = new Parser(); // TODO: find a way of obtainin Bundle's path with the help of $this->container $bpath = './src/Siwapp/InvoiceBundle'; $value = $yaml->parse(file_get_contents($bpath . '/DataFixtures/payments.yml')); foreach ($value['Payment'] as $ref => $values) { $payment = new Payment(); $invoice = new Invoice(); foreach ($values as $fname => $fvalue) { if ($fname == 'Invoice') { $fvalue = $manager->merge($this->getReference($fvalue)); } $method = 'set' . Inflector::camelize($fname); if (is_callable(array($payment, $method))) { call_user_func(array($payment, $method), $fvalue); } } $manager->persist($payment); $manager->flush(); $this->addReference($ref, $payment); } }
public function load(ObjectManager $manager) { $yaml = new Parser(); $bpath = $this->container->get('kernel')->getBundle('SiwappInvoiceBundle')->getPath(); $value = $yaml->parse(file_get_contents($bpath . '/DataFixtures/invoices.yml')); foreach ($value['Invoice'] as $ref => $values) { $invoice = new Invoice(); foreach ($values as $fname => $fvalue) { if (in_array($fname, ['Series'])) { $fvalue = $manager->merge($this->getReference($fvalue)); } elseif (in_array($fname, ['created_at', 'updated_at'])) { $fvalue = new \DateTime($fvalue); } $method = Inflector::camelize('set_' . $fname); if (is_callable(array($invoice, $method))) { call_user_func(array($invoice, $method), $fvalue); } } $manager->persist($invoice); $manager->flush(); $this->addReference($ref, $invoice); } foreach ($value['InvoiceItem'] as $ref => $values) { $item = $this->getReference($values['Item']); $invoice = $this->getReference($values['Invoice']); $invoice->addItem($item); $manager->persist($invoice); $manager->flush(); } foreach ($value['InvoicePayment'] as $ref => $values) { $payment = $this->getReference($values['Payment']); $invoice = $this->getReference($values['Invoice']); $invoice->addPayment($payment); $manager->persist($invoice); $manager->flush(); } }
/** * @param array $importedRecord * @param ContextInterface|null $context * @return array */ public static function addUnknownAttributes(array $importedRecord, ContextInterface $context = null) { $channelId = null; if ($context && $context->hasOption(self::CHANNEL_KEY)) { $channelId = $context->getOption(self::CHANNEL_KEY); } if (!empty($importedRecord[self::ATTRIBUTES_KEY])) { foreach ($importedRecord[self::ATTRIBUTES_KEY] as $attribute) { $name = $attribute[self::KEY]; $value = $attribute[self::VALUE]; $isIdentifier = substr($name, -strlen(self::ID_MARK)) === self::ID_MARK; if ($isIdentifier && $channelId) { $name = Inflector::camelize($name); $importedRecord = self::addAttribute($importedRecord, $name, $value); $name = substr($name, 0, strlen($name) - strlen(self::ID_MARK) + 1); $value = ['originId' => $value, self::CHANNEL_KEY => ['id' => $channelId]]; } $name = Inflector::camelize($name); $importedRecord = self::addAttribute($importedRecord, $name, $value); } unset($importedRecord[self::ATTRIBUTES_KEY]); } return $importedRecord; }