init
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/vendor/
|
||||
/lab/
|
||||
composer.lock
|
||||
.idea
|
21
composer.json
Normal file
21
composer.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "salarmehr/ary",
|
||||
"description": "A php array/stdClass alternative to store configs, options and more.",
|
||||
"type": "Utility",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Reza",
|
||||
"email": "salarmehr@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Salarmehr\\": "src/"
|
||||
},
|
||||
"files":[
|
||||
"src/helper.php"
|
||||
]
|
||||
}
|
||||
}
|
27
readme.md
Normal file
27
readme.md
Normal file
@ -0,0 +1,27 @@
|
||||
Ary
|
||||
===
|
||||
Ary makes PHP array syntax more flexible in addiction to some really necessary utility methods.
|
||||
|
||||
1. You can access array items using `->` or `[]` syntax.
|
||||
2. You will get `null` if an index does not exists.
|
||||
3. You can specify a default value for a missing index.
|
||||
|
||||
|
||||
// instantiation
|
||||
$ary= new Ary();
|
||||
// or simply
|
||||
$ary=ary();
|
||||
|
||||
// filling ary
|
||||
|
||||
$ary=ary(2,4,6,8); //or
|
||||
$ary=ary([2,4,6,8]); //or
|
||||
|
||||
$ary=ary(['x'=>'foo','y'=>'bar]);
|
||||
$foo= $ary->foo; //or
|
||||
$foo= $ary['foo'];
|
||||
|
||||
$missed=$ary->get('missed','Default value');
|
||||
|
||||
count($ary); //return 3
|
||||
$ary->all(); // return simple php array;
|
244
src/Ary.php
Normal file
244
src/Ary.php
Normal file
@ -0,0 +1,244 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
*
|
||||
* Inspired by Laravel Collection and Arr class.
|
||||
*
|
||||
* Date: 18/09/2015
|
||||
* Time: 22:37
|
||||
*/
|
||||
|
||||
namespace Salarmehr;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use CachingIterator;
|
||||
use Countable;
|
||||
use IteratorAggregate;
|
||||
use JsonSerializable;
|
||||
|
||||
class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
{
|
||||
|
||||
/**
|
||||
* The items contained in the collection.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $items = [];
|
||||
|
||||
/**
|
||||
* Create a new collection.
|
||||
*
|
||||
* @param mixed $items
|
||||
*/
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$items = func_get_args();
|
||||
if (count($items) == 0) {
|
||||
$this->items = [];
|
||||
}
|
||||
elseif (count($items) > 1) {
|
||||
$this->items = $items;
|
||||
}
|
||||
else {
|
||||
$this->items = is_array($items[0]) ? $items[0] : $this->getArrayableItems($items[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Results array of items from Collection or Arrayable.
|
||||
*
|
||||
* @param mixed $items
|
||||
* @return array
|
||||
*/
|
||||
protected function getArrayableItems($items)
|
||||
{
|
||||
if ($items instanceof self) {
|
||||
return $items->all();
|
||||
}
|
||||
|
||||
return (array)$items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the items in the collection.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function &__get($item)
|
||||
{
|
||||
return $this->get($item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from the collection by key.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function &get($key, $default = null)
|
||||
{
|
||||
if ($this->offsetExists($key)) {
|
||||
return $this->items[$key];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if an item exists at an offset.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists($key)
|
||||
{
|
||||
return array_key_exists($key, $this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the object into something JSON serializable.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the collection of items as a plain array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a CachingIterator instance.
|
||||
*
|
||||
* @param int $flags
|
||||
* @return \CachingIterator
|
||||
*/
|
||||
public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
|
||||
{
|
||||
return new CachingIterator($this->getIterator(), $flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an iterator for the items.
|
||||
*
|
||||
* @return \ArrayIterator
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new ArrayIterator($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of items in the collection.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item at a given offset.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function offsetGet($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the item at a given offset.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet($key, $value)
|
||||
{
|
||||
if (is_null($key)) {
|
||||
$this->items[] = $value;
|
||||
}
|
||||
else {
|
||||
$this->items[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset the item at a given offset.
|
||||
*
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public function offsetUnset($key)
|
||||
{
|
||||
unset($this->items[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the collection to its string representation.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the collection of items as JSON.
|
||||
*
|
||||
* @param int $options
|
||||
* @return string
|
||||
*/
|
||||
public function toJson($options = 0)
|
||||
{
|
||||
return json_encode($this->toArray(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a subset of the items from the given array.
|
||||
*
|
||||
* @param array|string $keys
|
||||
* @return array
|
||||
*/
|
||||
public function only($keys)
|
||||
{
|
||||
return array_intersect_key($this->all(), array_flip((array)$keys));
|
||||
}
|
||||
|
||||
public function __isset($name)
|
||||
{
|
||||
return $this->has($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if an item exists in the collection by key.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return bool
|
||||
*/
|
||||
public function has($key)
|
||||
{
|
||||
return $this->offsetExists($key);
|
||||
}
|
||||
}
|
21
src/helper.php
Normal file
21
src/helper.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by Reza Salarmehr
|
||||
* Date: 30/09/2015
|
||||
* Time: 16:24
|
||||
*/
|
||||
|
||||
use Salarmehr\Ary;
|
||||
|
||||
if (!function_exists('ary')) {
|
||||
|
||||
/**
|
||||
* @param mixed $items,...
|
||||
* @return Ary
|
||||
*/
|
||||
function ary()
|
||||
{
|
||||
return new Ary(...func_get_args());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user