Completed
Push — master ( ea613e...54b4d6 )
by Iqbal
02:33
created

Collection::setPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs;
12
13
use Borobudur\Collection\Collection as BaseCollection;
14
use Borobudur\Collection\Criteria;
15
use Borobudur\Cqrs\Exception\InvalidArgumentException;
16
use Borobudur\Cqrs\ReadModel\ReadModelInterface;
17
use Borobudur\Cqrs\ReadModel\Storage\Finder\Pagination;
18
use Borobudur\Cqrs\Serializer\SerializableInterface;
19
20
/**
21
 * @author      Iqbal Maulana <[email protected]>
22
 * @created     8/20/15
23
 */
24
class Collection extends BaseCollection
25
{
26
    /**
27
     * @var string|null
28
     */
29
    private $classMapper;
30
31
    /**
32
     * @var Pagination
33
     */
34
    private $pagination;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param array       $elements
40
     * @param string|null $classMapper
41
     */
42
    public function __construct(array $elements = array(), $classMapper = null)
43
    {
44
        $this->assertValidClassMapper($classMapper);
45
        $this->classMapper = $classMapper;
46
47
        parent::__construct($elements);
48
    }
49
50
    /**
51
     * Find collection by id.
52
     *
53
     * @param mixed $id
54
     *
55
     * @return ReadModelInterface
56
     */
57
    public function findById($id)
58
    {
59
        $collection = $this->matching(Criteria::create()->where(
60
            Criteria::expr()->equal('id', $id)
61
        ));
62
63
        return $collection->first();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function set($key, $value)
70
    {
71
        if (null !== $this->classMapper && false === is_object($value)) {
72
            $class = $this->classMapper;
73
            $value = $class::{'deserialize'}(new ParameterBag($value));
74
        }
75
76
        parent::set($key, $value);
77
    }
78
79
    /**
80
     * @param Pagination $pagination
81
     */
82
    public function setPagination(Pagination $pagination)
83
    {
84
        $this->pagination = $pagination;
85
    }
86
87
    /**
88
     * @return Pagination
89
     */
90
    public function getPagination()
91
    {
92
        return $this->pagination;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getFieldValues($field = null)
99
    {
100
        $fields = func_get_args();
101
        $elements = array();
102
        if ($fields || (isset($this->elements[0]) && is_array($this->elements[0]))) {
103
            $this->each(function ($element) use (&$elements, $fields) {
104
                if (is_object($element)) {
105
                    if ($fields) {
106
                        foreach ($fields as $field) {
107
                            $methodGet = 'get' . ucfirst($field);
108
                            $methodIs = 'is' . ucfirst($field);
109
110
                            if (method_exists($element, $methodGet)) {
111
                                $elements[] = $element->{$methodGet}();
112
                            } elseif (method_exists($element, $methodIs)) {
113
                                $elements[] = $element->{$methodIs}();
114
                            } elseif (property_exists($element, $field)) {
115
                                $elements[] = $element->{$field};
116
                            }
117
                        }
118
                    }
119
120
                    return;
121
                }
122
123
                if (is_array($element)) {
124
                    if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
125
                        foreach ($fields as $field) {
126
                            if (array_key_exists($field, $element)) {
127
                                $elements[] = $element[$field];
128
                            }
129
                        }
130
                    } else {
131
                        $elements = array_merge($elements, array_values($element));
132
                    }
133
                }
134
            });
135
136
            return $elements;
137
        }
138
139
        return array();
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function toArray($resortIndex = false)
146
    {
147
        $elements = parent::toArray($resortIndex);
148
149
        if (null !== $this->classMapper) {
150
            return array_map(function (SerializableInterface $element) {
151
                return $element->serialize();
152
            }, (array) $elements);
153
        }
154
155
        return $elements;
156
    }
157
158
    /**
159
     * Assert that class mapper should be exists and can be deserialize.
160
     *
161
     * @param string $classMapper
162
     *
163
     * @throws InvalidArgumentException
164
     */
165
    private function assertValidClassMapper($classMapper)
166
    {
167
        if (null !== $classMapper) {
168
            $classMapper = '\\' . ltrim($classMapper, '\\');
169
            if (!class_exists($classMapper)) {
170
                throw new InvalidArgumentException(sprintf('Class "%s" is undefined.', $classMapper));
171
            }
172
173
            if (!in_array('Borobudur\Cqrs\Serializer\SerializableInterface', class_implements($classMapper))) {
174
                throw new InvalidArgumentException(sprintf(
175
                    'Class "%s" should implement \Borobudur\Cqrs\Serializer\SerializableInterface',
176
                    $classMapper
177
                ));
178
            }
179
        }
180
    }
181
}
182