Completed
Push — master ( a743c4...8e8354 )
by Tomáš
02:35
created

WithCategories::getCategories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
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 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
4
namespace TournamentGenerator\Traits;
5
6
7
use TournamentGenerator\Category;
8
use TournamentGenerator\Containers\BaseContainer;
9
use TournamentGenerator\Containers\ContainerQuery;
10
use TournamentGenerator\Interfaces\WithCategories as WithCategoriesInterface;
11
12
/**
13
 * Definitions of methods for objects that contain categories
14
 *
15
 * @author  Tomáš Vojík <[email protected]>
16
 * @package TournamentGenerator\Traits
17
 * @since   0.4
18
 */
19
trait WithCategories
20
{
21
22
	/**
23
	 * Get all categories
24
	 *
25
	 * @return Category[]
26
	 */
27 10
	public function getCategories() : array {
28 10
		return $this->container->getHierarchyLevel(Category::class);
29
	}
30
31
	/**
32
	 * Get categories container query
33
	 *
34
	 * @return ContainerQuery
35
	 */
36 3
	public function queryCategories() : ContainerQuery {
37 3
		return $this->container->getHierarchyLevelQuery(Category::class);
38
	}
39
40
41
	/**
42
	 * Add one or more category to object
43
	 *
44
	 * @param Category ...$categories Category objects
45
	 *
46
	 * @return $this
47
	 */
48 1
	public function addCategory(Category ...$categories) : WithCategoriesInterface {
49 1
		foreach ($categories as $category) {
50 1
			$this->insertIntoContainer($category);
51
		}
52 1
		return $this;
53
	}
54
55
	/**
56
	 * Creates a new category and adds it to the object
57
	 *
58
	 * @param string $name New category name
59
	 * @param string|int|null   $id   Id of the new category - if omitted -> it is generated automatically as unique string
60
	 *
61
	 * @return Category New category
62
	 */
63 10
	public function category(string $name = '', $id = null) : Category {
64 10
		$c = new Category($name, $id);
65 10
		$this->insertIntoContainer($c->setSkip($this->allowSkip));
66 10
		return $c;
67
	}
68
69
}