Completed
Push — master ( 102c37...c32e8f )
by Peter
06:31
created

PaginationTrait::getPagination()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 9
cts 10
cp 0.9
rs 8.5906
cc 5
eloc 10
nc 7
nop 1
crap 5.025
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Traits\DataProvider;
15
16
use Maslosoft\EmbeDi\EmbeDi;
17
use Maslosoft\Mangan\Interfaces\PaginationInterface;
18
use Maslosoft\Mangan\Pagination;
19
use UnexpectedValueException;
20
21
/**
22
 * PaginationTrait
23
 *
24
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
25
 */
26
trait PaginationTrait
27
{
28
29
	/**
30
	 * Pagination instance
31
	 * @var boolean|array|PaginationInterface
32
	 */
33
	private $pagination = null;
34
35
	/**
36
	 * Returns the pagination object.
37
	 * @param string $className the pagination object class name, use this param to override default pagination class.
38
	 * @return PaginationInterface|Pagination|false the pagination object. If this is false, it means the pagination is disabled.
39
	 */
40 4
	public function getPagination($className = Pagination::class)
41
	{
42 4
		if ($this->pagination === false)
43
		{
44
			return false;
45
		}
46 4
		if ($this->pagination === null)
47
		{
48 2
			$this->pagination = new $className;
49
		}
50
51
		// FIXME: Attach pagination options if it's array.
52
		// It might be array, when configured via constructor
53 4
		if (is_array($this->pagination))
54
		{
55 2
			if (empty($this->pagination['class']))
56
			{
57 2
				$this->pagination['class'] = $className;
58
			}
59 2
			$this->pagination = EmbeDi::fly()->apply($this->pagination);
60
		}
61 4
		return $this->pagination;
62
	}
63
64
	/**
65
	 * Set pagination
66
	 * @param boolean|array|PaginationInterface $pagination
67
	 * @return static
68
	 */
69
	public function setPagination($pagination)
70
	{
71
		// Disable pagination completely
72
		if (false === $pagination)
73
		{
74
			$this->pagination = false;
75
			return $this;
76
		}
77
78
		// Configure from array
79
		if (is_array($pagination))
80
		{
81
			if (empty($pagination['class']))
82
			{
83
				$pagination['class'] = Pagination::class;
84
			}
85
			$this->pagination = EmbeDi::fly()->apply($pagination);
86
			return $this;
87
		}
88
89
		// Set object instance
90
		if ($pagination instanceof PaginationInterface)
91
		{
92
			$this->pagination = $pagination;
93
			return $this;
94
		}
95
96
		throw new UnexpectedValueException(sprintf('Expected `false` or `array` or `%s`, got %s', PaginationInterface::class, is_object($pagination) ? get_class($pagination) : gettype($pagination)));
97
	}
98
99
}
100