/** * Define a converter for a specific field. * * @param string $field * @param string $type * @param array $options * @return $this * @throws \Titon\Db\Exception\InvalidArgumentException */ public function convert($field, $type, array $options = []) { if (!isset($this->_defaults[$type])) { throw new InvalidArgumentException(sprintf('Converter %s does not exist', $type)); } $this->_converters[$field] = Hash::merge(['encode' => true, 'decode' => true, 'type' => $type], $this->_defaults[$type], $options); return $this; }
/** * Merge the custom configuration with the defaults and inherit from parent classes. * * @uses Titon\Utility\Hash * * @param array $config * @return $this */ public function applyConfig(array $config = []) { $parent = $this; $defaults = isset($this->_config) ? $this->_config : []; // Inherit config from parents while ($parent = get_parent_class($parent)) { $props = get_class_vars($parent); if (isset($props['_config'])) { $defaults = Hash::merge($props['_config'], $defaults); } } $this->__config = new ConfigAugment($config, $defaults); return $this; }
/** * Insert multiple records into the database using a single query. * Missing fields will be added with an empty value or the schema default value. * Does not support callbacks or transactions. * * @uses Titon\Utility\Hash * * @param array $data Multi-dimensional array of records * @param bool $allowPk If true will allow primary key fields, else will remove them * @param array $options * @return int The count of records inserted */ public function createMany(array $data, $allowPk = false, array $options = []) { $pk = $this->getPrimaryKey(); $columns = $this->getSchema()->getColumns(); $records = []; $defaults = []; if ($columns) { foreach ($columns as $key => $column) { $defaults[$key] = array_key_exists('default', $column) ? $column['default'] : ''; } unset($defaults[$pk]); } foreach ($data as $record) { // Convert from an entity if ($record instanceof Arrayable) { $record = $record->toArray(); } // Merge in defaults $record = Hash::merge($defaults, $record); // Remove primary key if (!$allowPk) { unset($record[$pk]); } // Filter out invalid columns if ($columns) { $record = array_intersect_key($record, $columns); } $records[] = $record; } return $this->query(Query::MULTI_INSERT)->save($records, $options); }
/** * Test that merge() will correctly merge nested arrays. */ public function testMerge() { $data1 = array('foo' => 'bar', 'boolean' => true, 'string' => 'abc', 'number' => 123, 'one'); $data2 = array('foo' => 'baz', 'boolean' => false, 'string' => 'xyz', 'number' => 456, 'two'); $this->assertEquals(array('foo' => 'baz', 'boolean' => false, 'string' => 'xyz', 'number' => 456, 'two'), Hash::merge($data1, $data2)); $data1['array'] = array('key' => 'value', 123, true); $data2['array'] = array(); $this->assertEquals(array('foo' => 'baz', 'boolean' => false, 'string' => 'xyz', 'number' => 456, 'two', 'array' => array('key' => 'value', 123, true)), Hash::merge($data1, $data2)); $data2['array'] = array('key' => 'base', 'foo' => 'bar', 123); $this->assertEquals(array('foo' => 'bar', 'boolean' => true, 'string' => 'abc', 'number' => 123, 'one', 'array' => array('key' => 'value', 'foo' => 'bar', 123, true)), Hash::merge($data2, $data1)); $this->assertEquals(array(), Hash::merge()); }
/** * Apply defaults and merge the custom configuration in. * * @uses Titon\Utility\Hash * * @param array $config * @param array $defaults */ public function __construct(array $config = [], array $defaults = []) { parent::__construct(Hash::merge($defaults, $config)); $this->_defaults = $defaults; }