/** * Set routes and nested resources based on the models * * @param string|arrau $model the name of the resource or an array of names * @param array $options array of options - see the usage description * @param string $parent the name of the parent route */ public static function set($name, array $options = array(), $parent = NULL) { if (Arr::is_array($name)) { $resources = array(); foreach ($name as $resource_name) { $resources[] = Resource::set($resource_name, $options, $parent); } return $resources; } $resource = new Resource($name, $options, $parent); return Resource::$_resources[$resource->name()] = $resource; }
public function get_product($id, $qty, array $options = array()) { // get only active products & return correct fields names $this->clear()->select(array('in_stock', 'qty'))->where('active', '=', 1); if (Arr::is_array($id)) { $result = $this->where($this->primary_key(), 'IN', $id)->find_all(); // TODO: ADD in_stock check } else { $result = $this->where($this->primary_key(), '=', $id)->and_where('in_stock', '>=', $qty)->find(); } if ($result->loaded()) { return $result->as_array(); } else { // TODO: throw new Kohana_Exception(__('Old data =\\ ')); } }
/** * Gets a value from an array using a dot separated path. * * // Get the value of $array['foo']['bar'] * $value = Arr::path($array, 'foo.bar'); * * Using a wildcard "*" will search intermediate arrays and return an array. * * // Get the values of "color" in theme * $colors = Arr::path($array, 'theme.*.color'); * * // Using an array of keys * $colors = Arr::path($array, array('theme', '*', 'color')); * * @param array $array array to search * @param mixed $path key path string (delimiter separated) or array of keys * @param mixed $default default value if the path is not set * @param string $delimiter key path delimiter * @return mixed */ public static function path($array, $path, $default = NULL, $delimiter = NULL) { if (!Arr::is_array($array)) { // This is not an array! return $default; } if (is_array($path)) { // The path has already been separated into keys $keys = $path; } else { if (array_key_exists($path, $array)) { // No need to do extra processing return $array[$path]; } if ($delimiter === NULL) { // Use the default delimiter $delimiter = Arr::$delimiter; } // Remove starting delimiters and spaces $path = ltrim($path, "{$delimiter} "); // Remove ending delimiters, spaces, and wildcards $path = rtrim($path, "{$delimiter} *"); // Split the keys by delimiter $keys = explode($delimiter, $path); } do { $key = array_shift($keys); if (ctype_digit($key)) { // Make the key an integer $key = (int) $key; } if (isset($array[$key])) { if ($keys) { if (Arr::is_array($array[$key])) { // Dig down into the next part of the path $array = $array[$key]; } else { // Unable to dig deeper break; } } else { // Found the path requested return $array[$key]; } } elseif ($key === '*') { // Handle wildcards $values = array(); foreach ($array as $arr) { if ($value = Arr::path($arr, implode('.', $keys))) { $values[] = $value; } } if ($values) { // Found the values requested return $values; } else { // Unable to dig deeper break; } } else { // Unable to dig deeper break; } } while ($keys); // Unable to find the value requested return $default; }
/** * Tests Arr::is_array() * * @test * @dataProvider provider_is_array * @param mixed $value Value to check * @param boolean $expected Is $value an array? */ public function test_is_array($array, $expected) { $this->assertSame($expected, Arr::is_array($array)); }
</dd> <dt class="petfinderBioLabel">Status</dt><dd class="petfinderBioData"><?php echo $info['status']; ?> </dd> <dt class="petfinderBioLabel">Age</dt><dd class="petfinderBioData"><?php echo $info['age']; ?> </dd> <dt class="petfinderBioLabel">Sex</dt><dd class="petfinderBioData"><?php echo $info['sex']; ?> </dd> <dt class="petfinderBioLabel">Breed</dt><dd class="petfinderBioData"> <?php if (Arr::is_array($info['breed'])) { echo implode(' and ', $info['breed']); } else { echo $info['breed']; } ?> </dd> <?php if (isset($info['options'])) { ?> <dt class="petfinderBioLabel">Details</dt><dd class="petfinderBioData"> <ul> <?php $option_count = count($info['options']); for ($i = 0; $i < $option_count; $i++) { echo '<li>' . $info['options'][$i] . '</li>';
/** * * @param array $column_params * @param array $rule_data * @return array */ private function set_column_options_using_matching_rules(array $column_params, array $rule_data) { foreach ($rule_data as $data_key => $value) { if (Arr::is_array($value)) { $rule_data[$data_key] = $this->set_column_options_using_matching_rules($column_params, $value); } else { if (is_string($value)) { if (UTF8::substr($value, 0, 1) == ':') { $key = str_replace(':', '', $value); if (Arr::get($column_params, $key)) { $rule_data[$data_key] = $column_params[$key]; } } } } } return $rule_data; }
public function action_delete_table() { try { $tables = $this->request->post('tables'); if (Arr::is_array($tables)) { foreach ($tables as $alias) { $table = Cms_Structure::factory($alias); if ($table != NULL) { unlink($table->get_file_name()); } } } $this->add_result_data('urlToRedirect', Cms_Urlmanager::get_tools_url('structure')); } catch (Exception $exc) { $this->set_error($exc->getMessage()); } }
public function set($id, $qty = 1, array $options = array()) { if (Arr::is_array($id)) { $products = $this->_get_product(Arr::pluck($id, 'id'), Arr::pluck($id, 'qty'), Arr::pluck($id, 'options')); // $products = $this->_get_product($id, $qty, $options); $products = $this->_get_product($id, $qty, $options); } else { $qty = max(1, $qty); $products[0] = $this->_get_product($id, $qty, $options); } foreach ($products as $product) { $product['options'] = Arr::get($product, 'options', array()); // Composite identifier $mix_id = $this->_get_mix_id($product['id'], $product['options']); if (isset($this->_content['products'][$mix_id])) { if ($product['qty'] >= $this->_content['products'][$mix_id]['qty'] + $qty) { $this->_content['products'][$mix_id]['qty'] += $qty; } else { throw new Kohana_Exception('In stock only :qty items', array(':qty' => $product['qty'])); // return FALSE; } } else { //$product['qty'] = $qty; $this->_content['products'][$mix_id] = $product; } } return $this->_save(); }