Mbh/Collection/Traits/Collection.php
<?php namespace Mbh\Collection\Traits;
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
/**
* MBHFramework
*
* @link https://github.com/MBHFramework/mbh-framework
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
*/
trait Collection
{
/**
* Returns whether the collection is empty.
*
* This should be equivalent to a count of zero, but is not required.
* Implementations should define what empty means in their own context.
*
* @return bool whether the collection is empty.
*/
public function isEmpty(): bool
{
return $this->count() === 0;
}
/**
* Returns a representation that can be natively converted to JSON, which is
* called when invoking json_encode.
*
* @return mixed
*
* @see JsonSerializable
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
public function serialize()
{
return serialize($this->toArray());
}
/**
* Invoked when calling var_dump.
*
* @return array
*/
public function __debugInfo()
{
return $this->toArray();
}
/**
* Returns a string representation of the collection, which is invoked when
* the collection is converted to a string.
*/
public function __toString()
{
return 'object(' . get_class($this) . ')';
}
/**
* Creates a shallow copy of the collection.
*
* @return CollectionInterface a shallow copy of the collection.
*/
abstract public function copy();
abstract public function count(): int;
/**
* Returns an array representation of the collection.
*
* The format of the returned array is implementation-dependent. Some
* implementations may throw an exception if an array representation
* could not be created (for example when object are used as keys).
*
* @return array
*/
abstract public function toArray(): array;
abstract public function unserialize($values);
}