/** * Set a configuration by key. Autobox the value if a default exists. * * @uses Titon\Utility\Hash * * @param string $key * @param mixed $value * @return $this */ public function set($key, $value = null) { if (($default = Hash::extract($this->_defaults, $key)) !== null) { if (is_float($default)) { $value = (double) $value; } else { if (is_numeric($default)) { $value = (int) $value; } else { if (is_bool($default)) { $value = (bool) $value; } else { if (is_string($default)) { $value = (string) $value; } else { if (is_array($default)) { $value = (array) $value; } } } } } } $this->_data = Hash::insert($this->_data, $key, $value); return $this; }
/** * 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; }
/** * {@inheritdoc} */ public function after(array $results, array $options = []) { $key = isset($options['key']) ? $options['key'] : null; $value = isset($options['value']) ? $options['value'] : null; $list = []; if (!$key || !$value) { throw new InvalidArgumentException('Missing key or value option for ListFinder'); } foreach ($results as $result) { if ($result instanceof Entity) { $result = $result->toArray(); } $list[Hash::get($result, $key)] = Hash::get($result, $value); } return $list; }
/** * Return the validation rules from the locale bundle. * * @uses Titon\Utility\Hash * * @param string $key * @return string|array */ public function getValidationRules($key = null) { return Hash::get($this->_loadResource('validations'), $key); }
function array_some(array $set, Closure $callback) { return Hash::some($set, $callback); }
/** * {@inheritdoc} */ public function getContextConfig($key) { $config = $this->allConfig(); if (isset($config['contexts'][$key])) { $config = array_merge($config, $config['contexts'][$key]); } return Hash::reduce($config, ['user', 'pass', 'host', 'port', 'database']); }
/** * 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); }
/** * Add values to the current loaded configuration. * * @uses Titon\Utility\Hash * * @param string $key * @param mixed $value */ public static function set($key, $value) { static::$_config = Hash::set(static::$_config, $key, $value); }
/** * Set a parameter value by key. * * @uses Titon\Utility\Hash * * @param string $key * @param mixed $value * @return $this */ public function set($key, $value = null) { if (strpos($key, '.') === false) { $this->_data[$key] = $value; } else { $this->_data = Hash::set($this->_data, $key, $value); } return $this; }
/** * When data is mapped through the constructor, set the exists flag if necessary, * and save the original data state to monitor for changes. * * @param array $data * @return $this */ public function mapData(array $data) { $this->flush(); if (!empty($data[$this->primaryKey])) { $this->_exists = true; } $this->_original = Hash::exclude($data, $this->reserved); $this->_attributes = $data; return $this; }
/** * Create a validator instance from a set of shorthand or expanded rule sets. * * @param array $data * @param array $fields * @return $this */ public static function makeFromShorthand(array $data = array(), array $fields = array()) { /** @type \Titon\Utility\Validator $obj */ $obj = new static($data); foreach ($fields as $field => $data) { $title = $field; // Convert to array if (is_string($data)) { $data = array('rules' => $data); } else { if (!is_array($data)) { continue; } else { if (Hash::isNumeric(array_keys($data))) { $data = array('rules' => $data); } } } // Prepare for parsing if (isset($data['title'])) { $title = $data['title']; } if (is_string($data['rules'])) { $data['rules'] = explode('|', $data['rules']); } $obj->addField($field, $title); foreach ($data['rules'] as $ruleOpts) { $shorthand = self::splitShorthand($ruleOpts); $obj->addRule($field, $shorthand['rule'], $shorthand['message'], $shorthand['options']); } } return $obj; }
/** * Turn an array into an XML document. Alternative to array_map magic. * * @param \SimpleXMLElement $xml * @param array $array * @return \SimpleXMLElement */ public static function buildXml(SimpleXMLElement &$xml, $array) { if (is_array($array)) { foreach ($array as $key => $value) { // XML_NONE if (!is_array($value)) { $xml->addChild($key, static::unbox($value)); continue; } // Multiple nodes of the same name if (Hash::isNumeric(array_keys($value))) { foreach ($value as $kValue) { if (is_array($kValue)) { static::buildXml($xml, array($key => $kValue)); } else { $xml->addChild($key, static::unbox($kValue)); } } // XML_GROUP } else { if (isset($value['attributes'])) { if (!isset($value['value'])) { $value['value'] = null; } if (is_array($value['value'])) { $node = $xml->addChild($key); static::buildXml($node, $value['value']); } else { $node = $xml->addChild($key, static::unbox($value['value'])); } if (!empty($value['attributes'])) { foreach ($value['attributes'] as $aKey => $aValue) { $node->addAttribute($aKey, static::unbox($aValue)); } } // XML_MERGE } else { if (isset($value['value'])) { $node = $xml->addChild($key, $value['value']); unset($value['value']); if (!empty($value)) { foreach ($value as $aKey => $aValue) { if (is_array($aValue)) { static::buildXml($node, array($aKey => $aValue)); } else { $node->addAttribute($aKey, static::unbox($aValue)); } } } // XML_ATTRIBS } else { $node = $xml->addChild($key); if (!empty($value)) { foreach ($value as $aKey => $aValue) { if (is_array($aValue)) { static::buildXml($node, array($aKey => $aValue)); } else { $node->addChild($aKey, static::unbox($aValue)); } } } } } } } } return $xml; }
/** * Test that some() returns true if one value matches the callback condition. */ public function testSome() { $this->assertTrue(Hash::some(array(123, 'abc', true, null), function ($value, $key) { return is_string($value); })); $this->assertFalse(Hash::some(array(123, true, null), function ($value, $key) { return is_string($value); })); }