コード例 #1
0
ファイル: array_take.php プロジェクト: morrelinko/array-take
function array_take(array $from, $keys, $options = [])
{
    $options = array_merge(['includeEmpty' => true, 'ignoreMissingKeys' => true], $options);
    // Checks if the array is a list
    if (array_values($from) === $from) {
        return array_map(function ($item) use($keys, $options) {
            return array_take($item, $keys, $options);
        }, $from);
    }
    $retrieved = array();
    array_walk($keys, function (&$value, $index, $retrieved) use($from, $options) {
        if (is_int($index) && !$value instanceof \Closure) {
            $index = $value;
        }
        if (!array_key_exists($index, $from) && !$options['ignoreMissingKeys']) {
            $from[$index] = null;
        }
        if (array_key_exists($index, $from) && ($options['includeEmpty'] || $options['includeEmpty'] == false && !empty($from[$index]))) {
            if ($value instanceof \Closure) {
                $retrieved[0][$index] = call_user_func($value, $from[$index]);
            } else {
                $retrieved[0][$value] = $from[$index];
            }
        }
    }, array(&$retrieved));
    return $retrieved;
}
コード例 #2
0
 public function testApplyCallbackOnKeys()
 {
     $original = array('name' => 'John Doe', 'password' => '123456', 'gender' => 'male');
     $actual = array_take($original, array('name', 'password' => function ($value) use(&$password) {
         return $password = md5($value);
     }));
     $expected = array('name' => 'John Doe', 'password' => $password);
     $this->assertSame($expected, $actual);
 }
コード例 #3
0
ファイル: form.php プロジェクト: dalinhuang/zotop
 public static function field($attrs)
 {
     $html[] = '';
     if (is_array($attrs)) {
         $type = array_take('type', $attrs);
         $type = isset($type) ? $type : 'text';
         $html[] = field::get($type, $attrs);
     } else {
         $html[] = $attrs;
     }
     return implode("\n", $html);
 }
コード例 #4
0
ファイル: table.php プロジェクト: dalinhuang/zotop
 public static function row($rows, $classname = '')
 {
     static $i = 0;
     if (is_array($rows)) {
         $html[] = '';
         $html[] = '		<tr class="item ' . ($i % 2 == 0 ? 'odd' : 'even') . ' ' . $classname . '">';
         foreach ($rows as $key => $value) {
             if (is_string($value)) {
                 $html[] = '			<td class="' . $key . '">' . $value . '</td>';
             } else {
                 $inner = array_take('inner', $value);
                 $html[] = '			<td class="' . $key . '" ' . html::attributes($value) . '>' . $inner . '</td>';
             }
         }
         $html[] = '		</tr>';
         $i++;
     }
     echo implode("\n", $html);
 }
コード例 #5
0
ファイル: field.php プロジェクト: dalinhuang/zotop
 /**
  * 控件组
  * 
  * @param $attrs array 控件参数
  * @return string 控件代码
  */
 public static function group($attrs)
 {
     $html[] = '<div class="field-group">';
     $fields = array_take('fields', $attrs);
     if (is_array($fields)) {
         foreach ($fields as $field) {
             if (is_array($field)) {
                 $type = array_take('type', $field);
                 $type = isset($type) ? $type : 'text';
                 $html[] = '	<div class="field-group-item">';
                 $html[] = '		<label for="' . $field['name'] . '">' . array_take('label', $field) . '</label>';
                 $html[] = '		' . field::get($type, $field);
                 $html[] = '	</div>';
             } else {
                 $html[] = $field;
             }
         }
     } else {
         $html[] = $fields;
     }
     $html[] = '</div>';
     return implode("\n", $html);
 }
コード例 #6
0
ファイル: Dictionary.php プロジェクト: weew/collections
 /**
  * @param $key
  * @param null $default
  *
  * @return mixed
  */
 public function take($key, $default = null)
 {
     return array_take($this->items, $key, $default);
 }
コード例 #7
0
ファイル: ArrayTest.php プロジェクト: weew/helpers-array
 public function test_array_take()
 {
     $array = ['foo' => ['bar' => 'baz']];
     $this->assertEquals('baz', array_take($array, 'foo.bar'));
     $this->assertEquals(['foo' => []], $array);
 }
コード例 #8
0
 /**
  * @param array $args
  * @param string $name
  * @param string $alias
  *
  * @return array
  */
 protected function mergeNameAndAliasCount(array $args, $name, $alias)
 {
     if (!array_has($args, 'options')) {
         return $args;
     }
     $namedCount = array_take($args, s('options.%s', $name), 0);
     $aliasedCount = array_take($args, s('options.%s', $alias), 0);
     $totalCount = $namedCount + $aliasedCount;
     if ($name) {
         array_set($args, s('options.%s', $name), $totalCount);
     } else {
         array_set($args, s('options.%s', $alias), $totalCount);
     }
     return $args;
 }
コード例 #9
0
 /**
  * @param IOption $option
  * @param array $groupedArgs
  * @param bool $strict
  *
  * @return array
  * @throws MissingOptionValueException
  */
 protected function matchRegularOption(IOption $option, array $groupedArgs, $strict)
 {
     $isRequired = $option->isRequired();
     $hasValue = array_has($groupedArgs, $option->getNameOrAlias()) && count(array_get($groupedArgs, $option->getNameOrAlias())) > 0;
     if ($isRequired && !$hasValue && $strict) {
         throw new MissingOptionValueException(s('Missing option value "%s".', $option->getNameOrAlias()));
     }
     if (array_has($groupedArgs, $option->getNameOrAlias())) {
         $values = array_take($groupedArgs, $option->getNameOrAlias());
         if ($option->isSingle()) {
             $option->setValue(array_pop($values));
         } else {
             if ($option->isMultiple()) {
                 $option->setValue($values);
             }
         }
     }
     return $groupedArgs;
 }
コード例 #10
0
 public function test_array_take()
 {
     $array = array('foo' => array('bar' => 'baz'));
     $this->assertEquals('baz', array_take($array, 'foo.bar'));
     $this->assertEquals(array('foo' => array()), $array);
 }