|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Cubiche\Core\Collections; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Collections\DataSource\DataSourceInterface; |
|
14
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* DataSourceCollection Trait. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
trait DataSourceCollectionTrait |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var DataSourceInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $dataSource; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param DataSourceInterface $dataSource |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(DataSourceInterface $dataSource) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->dataSource = $dataSource; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function count() |
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->isInitialized()) { |
|
|
|
|
|
|
43
|
|
|
return parent::count(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this->dataSource->count(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getIterator() |
|
53
|
|
|
{ |
|
54
|
|
|
if ($this->isInitialized()) { |
|
|
|
|
|
|
55
|
|
|
return parent::getIterator(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->dataSource->getIterator(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function slice($offset, $length = null) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->isInitialized()) { |
|
|
|
|
|
|
67
|
|
|
return parent::slice($offset, $length); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return new self($this->dataSource->slicedDataSource($offset, $length)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function find(SpecificationInterface $criteria) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->isInitialized()) { |
|
|
|
|
|
|
79
|
|
|
return parent::find($criteria); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return new self($this->dataSource->filteredDataSource($criteria)); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.