commit 0c348467c439ec67484fdd9fc14bbdd336585b27 Author: Reza Date: Thu Oct 1 08:23:42 2015 +0330 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..edbb9fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor/ +/lab/ +composer.lock +.idea diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0b0aebc --- /dev/null +++ b/composer.json @@ -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" + ] + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..f82f9ad --- /dev/null +++ b/readme.md @@ -0,0 +1,28 @@ +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; \ No newline at end of file diff --git a/src/Ary.php b/src/Ary.php new file mode 100644 index 0000000..0cadce0 --- /dev/null +++ b/src/Ary.php @@ -0,0 +1,244 @@ +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); + } +} \ No newline at end of file diff --git a/src/helper.php b/src/helper.php new file mode 100644 index 0000000..97b6531 --- /dev/null +++ b/src/helper.php @@ -0,0 +1,21 @@ +