Completed
Push — master ( 144202...6a00f8 )
by Peter
11:59
created

PaginationTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 28.57%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 74
ccs 10
cts 35
cp 0.2857
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getPagination() 0 23 5
B setPagination() 0 29 6
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Traits\DataProvider;
10
11
use Maslosoft\EmbeDi\EmbeDi;
12
use Maslosoft\Mangan\Interfaces\PaginationInterface;
13
use Maslosoft\Mangan\Pagination;
14
use UnexpectedValueException;
15
16
/**
17
 * PaginationTrait
18
 *
19
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
20
 */
21
trait PaginationTrait
22
{
23
24
	/**
25
	 * Pagination instance
26
	 * @var PaginationInterface
27
	 */
28
	private $pagination = null;
29
30
	/**
31
	 * Returns the pagination object.
32
	 * @param string $className the pagination object class name, use this param to override default pagination class.
33
	 * @return PaginationInterface|Pagination|false the pagination object. If this is false, it means the pagination is disabled.
34
	 */
35 1
	public function getPagination($className = Pagination::class)
36
	{
37 1
		if ($this->pagination === false)
38 1
		{
39
			return false;
40
		}
41 1
		if ($this->pagination === null)
42 1
		{
43 1
			$this->pagination = new $className;
44 1
		}
45
46
		// FIXME: Attach pagination options if it's array.
47
		// It might be array, when configured via constructor
48 1
		if (is_array($this->pagination))
49 1
		{
50
			if (empty($this->pagination['class']))
51
			{
52
				$this->pagination['class'] = $className;
53
			}
54
			$this->pagination = EmbeDi::fly()->apply($this->pagination);
55
		}
56 1
		return $this->pagination;
57
	}
58
59
	/**
60
	 * Set pagination
61
	 * @param boolean|array|PaginationInterface $pagination
62
	 * @return static
63
	 */
64
	public function setPagination($pagination)
65
	{
66
		// Disable pagination completely
67
		if (false === $pagination)
68
		{
69
			$this->pagination = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<Maslosoft\Mangan\...es\PaginationInterface> of property $pagination.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
			return $this;
71
		}
72
73
		// Configure from array
74
		if (is_array($pagination))
75
		{
76
			if (empty($pagination['class']))
77
			{
78
				$pagination['class'] = Pagination::class;
79
			}
80
			$this->pagination = EmbeDi::fly()->apply($pagination);
81
			return $this;
82
		}
83
84
		// Set object instance
85
		if ($pagination instanceof PaginationInterface)
86
		{
87
			$this->pagination = $pagination;
88
			return $this;
89
		}
90
91
		throw new UnexpectedValueException(sprintf('Expected `false` or `array` or `%s`, got %s', PaginationInterface::class, is_object($pagination) ? get_class($pagination) : gettype($pagination)));
92
	}
93
94
}
95