PHPackages                             bartoszbartniczak/array-object - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bartoszbartniczak/array-object

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bartoszbartniczak/array-object
==============================

ArrayOfObjects is the extension of the SPL ArrayObject concept. ArrayOfObjects may contain only objects of given type.

1.3.3(9y ago)11801Apache Licence 2.0PHP &gt;7.0

Since Dec 16Compare

[ Source](https://github.com/BartoszBartniczak/ArrayOfObjects)[ Packagist](https://packagist.org/packages/bartoszbartniczak/array-object)[ RSS](/packages/bartoszbartniczak-array-object/feed)WikiDiscussions Synced today

READMEChangelogDependencies (3)Versions (9)Used By (1)

BartoszBartniczak/ArrayOfObjects [![Build Status](https://camo.githubusercontent.com/b8e40d29e6d61430f0d5d93212802e4a1ddf4e99fbd53e724d0c085315be5e71/68747470733a2f2f7472617669732d63692e6f72672f426172746f737a426172746e69637a616b2f41727261794f664f626a656374732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/BartoszBartniczak/ArrayOfObjects) [![Coverage Status](https://camo.githubusercontent.com/ed0562c482508c155104192dc2f4efba25e9e0e0dc9ba161162ef636eaaf3857/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f426172746f737a426172746e69637a616b2f41727261794f664f626a656374732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/BartoszBartniczak/ArrayOfObjects?branch=master)
===========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#bartoszbartniczakarrayofobjects--)

ArrayOfObjects is the extension of the SPL ArrayObject concept. ArrayOfObjects may contain only objects of given type.
----------------------------------------------------------------------------------------------------------------------

[](#arrayofobjects-is-the-extension-of-the-spl-arrayobject-concept-arrayofobjects-may-contain-only-objects-of-given-type)

### Table of contents:

[](#table-of-contents)

1. [Class ArrayObject](#class-arrayobject)
2. [Class ArrayOfObjects](#class-arrayofobjects)
3. [Tests](#tests)

### Class ArrayObject

[](#class-arrayobject)

#### Methods:

[](#methods)

##### filter()

[](#filter)

Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result ArrayObject. Array keys are preserved.

##### shift()

[](#shift)

Shifts an element off the beginning of array.

##### pop()

[](#pop)

Pops the element off the end of array.

##### merge()

[](#merge)

Merges two arrays.

##### isEmpty()

[](#isempty)

Checks if array is empty.

##### isNotEmpty()

[](#isnotempty)

Checks if array is not empty.

##### keys()

[](#keys)

Returns all the keys of the array.

##### first()

[](#first)

Returns the first element, ignoring the type of the keys.

##### last()

[](#last)

Returns the last element, ignoring the type of the keys.

### Class ArrayOfObjects

[](#class-arrayofobjects)

Class `ArrayOfObjects` is the extension of the `ArrayObject` concept. This array may contain only objects of given type.

#### Methods:

[](#methods-1)

##### \_\_construct()

[](#__construct)

The first argument is the class name. The rest of the arguments are this same as in class `\ArrayObject`. Constructor can throw `InvalidArgumentException` if one of objects is not instance of the class.

##### getClassName()

[](#getclassname)

Returns the name of the class which this array may contain.

##### offsetSet()

[](#offsetset)

Overrides the method in class `\ArrayObject`. It can throw `InvalidArgumentException` if the object is not the instance of the class.

This method is used in three methods of adding new object to the array:

```
$arrayOfObjects = new ArrayOfObjects(\DateTime::class, [new \DateTime(), new \DateTime()]);

$arrayOfObjects[] = new \DateTime();
$arrayOfObjects->offsetSet(4, new \DateTime());
$arrayOfObjects->append(new \DateTime());
```

**NOTICE:** `exchangeArray()` method does not use any of those methods. So, it has to be overwrite.

##### exchangeArray()

[](#exchangearray)

Exchange the array for another one.

##### throwExceptionIfObjectIsNotInstanceOfTheClass()

[](#throwexceptionifobjectisnotinstanceoftheclass)

You can overwrite this protected method if you want to throw your own exception.

### Key Naming Strategy

[](#key-naming-strategy)

Since version 1.3 this library delivers new functionality - Key Naming Strategy. It allows you to choose how the keys of array are generated.

#### StandardStrategy

[](#standardstrategy)

Default strategy is [StandardStrategy](src/ArrayObject/KeyNamingStrategy/StandardStrategy.php). Which behaves exactly like PHP arrays key naming strategy.

```
