All Projects → yii2mod → Yii2 Helpers

yii2mod / Yii2 Helpers

Licence: mit
Collection of useful helper functions for Yii Framework 2.0

Projects that are alternatives of or similar to Yii2 Helpers

yii2-tinymce
Yii2 extension, tinymce wysiwyg editor
Stars: ✭ 16 (+0%)
Mutual labels:  yii2, yii2-extension
yii2-star-rating
Star rating widget based on jQuery Raty
Stars: ✭ 16 (+0%)
Mutual labels:  yii2, yii2-extension
filedb
ActiveRecord for static data definitions based on files
Stars: ✭ 72 (+350%)
Mutual labels:  yii2, yii2-extension
yii2-behaviors
Collection of useful behaviors for Yii Framework 2.0
Stars: ✭ 25 (+56.25%)
Mutual labels:  yii2, yii2-extension
yii2-blog
Simple, configurable blog module for Yii2 (post, comment, nested category, tags). + frontend, backend. + SEO! (Opengraph, Schema.org) ~~~COMING SOON V2.0~~~ Please STAR this repo
Stars: ✭ 79 (+393.75%)
Mutual labels:  yii2, yii2-extension
yii2-ledap
yii2 for ledap
Stars: ✭ 20 (+25%)
Mutual labels:  yii2, yii2-extension
yii2-telegram
Support chat for site based on Telegram bot
Stars: ✭ 49 (+206.25%)
Mutual labels:  yii2, yii2-extension
yii2-presenter
Yii2 View Presenter
Stars: ✭ 13 (-18.75%)
Mutual labels:  yii2, yii2-extension
yii2-jwt-user
JWT (JSON Web Token) User component for Yii 2
Stars: ✭ 16 (+0%)
Mutual labels:  yii2, yii2-extension
yii2-firebird
Firebird connector for Yii2 framework
Stars: ✭ 23 (+43.75%)
Mutual labels:  yii2, yii2-extension
yii2-timezone
Timezone detector
Stars: ✭ 14 (-12.5%)
Mutual labels:  yii2, yii2-extension
yii2-queuemanager
Yii2 Queue Manager (Analytic & Monitor)
Stars: ✭ 18 (+12.5%)
Mutual labels:  yii2, yii2-extension
yii2-payment
Yii2 Payment extension hổ trợ tích hợp các cổng thanh toán VnPayment, Onepay, Bảo Kim, Ngân Lượng, VTCPay, MoMo.
Stars: ✭ 20 (+25%)
Mutual labels:  yii2, yii2-extension
ar-dynattribute
Provide ActiveRecord dynamic attributes stored into the single field in serialized state
Stars: ✭ 43 (+168.75%)
Mutual labels:  yii2, yii2-extension
ar-variation
Variation behavior for ActiveRecord
Stars: ✭ 46 (+187.5%)
Mutual labels:  yii2, yii2-extension
yii2-merit
Reputation engine for Yii2 用于实现积分,等级功能的设计
Stars: ✭ 16 (+0%)
Mutual labels:  yii2, yii2-extension
yii2-link-preview
LinkPreview widget render page preview
Stars: ✭ 26 (+62.5%)
Mutual labels:  yii2, yii2-extension
ar-role
ActiveRecord behavior, which provides relation roles (table inheritance)
Stars: ✭ 34 (+112.5%)
Mutual labels:  yii2, yii2-extension
yii2-facades
Facades for Yii 2
Stars: ✭ 21 (+31.25%)
Mutual labels:  yii2, yii2-extension
collection
Basic collection library for Yii Framework 2.0
Stars: ✭ 29 (+81.25%)
Mutual labels:  yii2, yii2-extension

Yii2 Helpers

This extension is a collection of useful helper functions for Yii Framework 2.0.

Latest Stable Version Total Downloads License Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2mod/yii2-helpers "*"

or add

"yii2mod/yii2-helpers": "*"

to the require section of your composer.json file.

Available Methods

  1. add()
  2. average()
  3. collapse()
  4. except()
  5. has()
  6. first()
  7. flatten()
  8. last()
  9. only()
  10. pluck()
  11. prepend()
  12. pull()
  13. set()
  14. sort()
  15. sortRecursive()
  16. where()
  17. xmlStrToArray()

Method Listing

#####add()

Add a given key / value pair to the array if the given key doesn't already exist in the array:

$array = ArrayHelper::add(['card' => 'Visa'], 'price', 200);
// ['card' => 'Visa', 'price' => 200]

#####average()

Get the average value of a given key:

ArrayHelper::average([1, 2, 3, 4, 5]);
// 3

You may also pass a key to the average method:

$array = [
   ['score' => 10],
   ['score' => 30],
   ['score' => 50],
];

ArrayHelper::average($array, 'score');

// 30

#####collapse()

Collapse an array of arrays into a single array:

$array = ArrayHelper::collapse([[1, 2, 3], [4, 5, 6]]);

// [1, 2, 3, 4, 5, 6]

#####except()

Get all of the given array except for a specified array of items:

$array = ['name' => 'Desk', 'price' => 100];

$array = ArrayHelper::except($array, ['price']);

// ['name' => 'Desk']

#####has()

Check if an item exists in an array using "dot" notation:

$array = ['products' => ['desk' => ['price' => 100]]];

$hasDesk = ArrayHelper::has($array, 'products.desk');

// true

#####first()

Return the first element in an array passing a given truth test:

$array = [100, 200, 300];
$value = ArrayHelper::first($array); // 100

// or apply custom callback

$value = ArrayHelper::first($array, function($key, $value) {
      return $value >= 150; // 200
});

#####flatten()

Flatten a multi-dimensional array into a single level:

$array = ['name' => 'Bob', 'languages' => ['PHP', 'Python']];

$array = ArrayHelper::flatten($array);

// ['Bob', 'PHP', 'Python'];

#####last()

Return the last element in an array passing a given truth test:

$array = [100, 200, 300];
$value = ArrayHelper::last($array); // 300

// or apply custom callback

$value = ArrayHelper::last($array, function($key, $value) {
      return $value; // 300
});

#####only()

Get a subset of the items from the given array:

$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];

$array = ArrayHelper::only($array, ['name', 'price']);

// ['name' => 'Desk', 'price' => 100]

#####prepend()

Push an item onto the beginning of an array:

$array = ['one', 'two', 'three', 'four'];

$array = ArrayHelper::prepend($array, 'zero');

// $array: ['zero', 'one', 'two', 'three', 'four']

#####pluck()

The function retrieves all of the collection values for a given key:

$array = [
   ['product_id' => 'prod-100', 'name' => 'Desk'],
   ['product_id' => 'prod-200', 'name' => 'Chair'],
];


$plucked = ArrayHelper::pluck($array, 'name');

// ['Desk', 'Chair']

You may also specify how you wish the resulting collection to be keyed:

$plucked = ArrayHelper::pluck($array, 'name', 'product_id');

// ['prod-100' => 'Desk', 'prod-200' => 'Chair']

#####pull()

Get a value from the array, and remove it:

$array = ['name' => 'Desk', 'price' => 100];

$name = ArrayHelper::pull($array, 'name');

// $name: Desk

// $array: ['price' => 100]

#####set()

Set an array item to a given value using "dot" notation:

$array = ['products' => ['desk' => ['price' => 100]]];

ArrayHelper::set($array, 'products.desk.price', 200);

// ['products' => ['desk' => ['price' => 200]]]

#####sort()

Sort the array using the given callback:

$array = [
    ['name' => 'Desk'],
    ['name' => 'Chair'],
];

$array = ArrayHelper::sort($array, function ($value) {
    return $value['name'];
});

/*
    [
        ['name' => 'Chair'],
        ['name' => 'Desk'],
    ]
*/

#####sortRecursive()

Recursively sort an array by keys and values:

$array = [
      [
          'Desc',
          'Chair',
      ],
      [
          'PHP',
          'Ruby',
          'JavaScript',
      ],
];

$array = ArrayHelper::sortRecursive($array);
        
/*
  [
      [
         'Chair',
         'Desc',
      ],
      [
         'JavaScript',
         'PHP',
         'Ruby',
      ]
  ];
*/

#####where()

Filter the array using the given Closure.:

$array = ["100", 200, "300"];
$value = ArrayHelper::where($array, function($key, $value) {
   return is_string($value);
});
// Will be return Array ( [0] => 100 [2] => 300 );

#####xmlStrToArray()

Convert xml string to array.

$xml = '<?xml version="1.0"?>
          <root>
            <id>1</id>
            <name>Bob</name>
          </root>';
          
ArrayHelper::xmlStrToArray($xml)

// ['id' => 1, 'name' => 'Bob']

####StringHelper

  • StringHelper::removeStopWords('some text'); - remove stop words from string
  • StringHelper::removePunctuationSymbols('some text'); - Remove punctuation symbols from string
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].