1
|
|
|
<?php namespace Mbh\Collection\Traits; |
2
|
|
|
|
3
|
|
|
use Mbh\Collection\Interfaces\Collection as CollectionInterface; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* MBHFramework |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
9
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
10
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
trait Collection |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Returns whether the collection is empty. |
17
|
|
|
* |
18
|
|
|
* This should be equivalent to a count of zero, but is not required. |
19
|
|
|
* Implementations should define what empty means in their own context. |
20
|
|
|
* |
21
|
|
|
* @return bool whether the collection is empty. |
22
|
|
|
*/ |
23
|
|
|
public function isEmpty(): bool |
24
|
|
|
{ |
25
|
|
|
return $this->count() === 0; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns a representation that can be natively converted to JSON, which is |
30
|
|
|
* called when invoking json_encode. |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
* |
34
|
|
|
* @see JsonSerializable |
35
|
|
|
*/ |
36
|
|
|
public function jsonSerialize(): array |
37
|
|
|
{ |
38
|
|
|
return $this->toArray(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function serialize() |
42
|
|
|
{ |
43
|
|
|
return serialize($this->toArray()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Invoked when calling var_dump. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function __debugInfo() |
52
|
|
|
{ |
53
|
|
|
return $this->toArray(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns a string representation of the collection, which is invoked when |
58
|
|
|
* the collection is converted to a string. |
59
|
|
|
*/ |
60
|
|
|
public function __toString() |
61
|
|
|
{ |
62
|
|
|
return 'object(' . get_class($this) . ')'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Creates a shallow copy of the collection. |
67
|
|
|
* |
68
|
|
|
* @return CollectionInterface a shallow copy of the collection. |
69
|
|
|
*/ |
70
|
|
|
abstract public function copy(); |
71
|
|
|
|
72
|
|
|
abstract public function count(): int; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns an array representation of the collection. |
76
|
|
|
* |
77
|
|
|
* The format of the returned array is implementation-dependent. Some |
78
|
|
|
* implementations may throw an exception if an array representation |
79
|
|
|
* could not be created (for example when object are used as keys). |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
abstract public function toArray(): array; |
84
|
|
|
|
85
|
|
|
abstract public function unserialize($values); |
86
|
|
|
} |
87
|
|
|
|