Passed
Push — master ( cf33e8...48bc14 )
by Bobby
09:17
created

CollectionExport::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Ballen\Collection;
4
5
/**
6
 * Collection
7
 *
8
 * A Collection class (library) which provides OOP replacement for the
9
 * traditional array data structure.
10
 *
11
 * @author Bobby Allen <[email protected]>
12
 * @license https://opensource.org/licenses/MIT
13
 * @link https://github.com/allebb/collection
14
 * @link http://bobbyallen.me
15
 *
16
 */
17
class CollectionExport
18
{
19
20
    /**
21
     * Array of collection items.
22
     * @var array
23
     */
24
    private $collection;
25
26
    /**
27
     * Initiate the CollectionExport object.
28
     * @param array $collection The collection array.
29
     */
30 11
    public function __construct(array $collection)
31
    {
32 11
        $this->collection = $collection;
33 11
    }
34
35
    /**
36
     * The total number of items in the collection.
37
     * @return integer
38
     */
39 1
    public function count()
40
    {
41 1
        return count($this->collection);
42
    }
43
44
    /**
45
     * Return the contents of the collection as an array.
46
     * @return array
47
     */
48 8
    public function toArray()
49
    {
50 8
        return $this->collection;
51
    }
52
53
    /**
54
     * Return the contents of the collection as an stdClass object.
55
     * @return object
56
     */
57 1
    public function toObject()
58
    {
59 1
        return (object)$this->toArray();
60
    }
61
62
    /**
63
     * Return the contents of the collection as a JSON encoded string.
64
     * @return string
65
     */
66 2
    public function toJson()
67
    {
68 2
        return json_encode($this->collection);
69
    }
70
71
    /**
72
     * Default return value on the object, will return the collection as JSON representation.
73
     * @return string
74
     */
75 1
    public function __toString()
76
    {
77 1
        return $this->toJson();
78
    }
79
}
80