Completed
Push — master ( ccf80a...5ce56d )
by WEBEWEB
01:32
created

Document::getPage()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2020 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\ThirdParty\OcrLad\Model;
13
14
use WBW\Library\Core\Model\Attribute\StringFilenameTrait;
15
use WBW\Library\Core\ThirdParty\OcrLad\Model\Attribute\ArrayWordsTrait;
16
17
/**
18
 * Document.
19
 *
20
 * @author webeweb <https://github.com/webeweb>
21
 * @package WBW\Library\Core\ThirdParty\OcrLad\Model
22
 */
23
class Document {
24
25
    use ArrayWordsTrait;
26
    use StringFilenameTrait;
27
28
    /**
29
     * Index.
30
     *
31
     * @var Word[]
32
     */
33
    private $index;
34
35
    /**
36
     * Pages.
37
     *
38
     * @var Page[]
39
     */
40
    private $pages;
41
42
    /**
43
     * Constructor.
44
     */
45
    public function __construct() {
46
        $this->setIndex([]);
47
        $this->setPages([]);
48
        $this->setWords([]);
49
    }
50
51
    /**
52
     * Add a page.
53
     *
54
     * @param Page $page The page.
55
     * @return Document Returns this document.
56
     */
57
    public function addPage(Page $page) {
58
        $this->pages[] = $page;
59
        return $this;
60
    }
61
62
    /**
63
     * Get the index.
64
     *
65
     * @return Word[] Returns the index.
66
     */
67
    public function getIndex() {
68
        return $this->index;
69
    }
70
71
    /**
72
     * Get the number of pages.
73
     *
74
     * @return int Returns the number of pages.
75
     */
76
    public function getNumberPages() {
77
        return count($this->getPages());
78
    }
79
80
    /**
81
     * Get a page.
82
     *
83
     * @param int $p The page.
84
     * @return Page|null Returns the page in case of success, null otherwise.
85
     */
86
    public function getPage($p) {
87
        if (false === is_int($p) || $p < 0 || $this->getNumberPages() <= $p) {
88
            return null;
89
        }
90
        return $this->getPages()[$p];
91
    }
92
93
    /**
94
     * Get the pages.
95
     *
96
     * @return Page[] Returns the pages.
97
     */
98
    public function getPages() {
99
        return $this->pages;
100
    }
101
102
    /**
103
     * Determines if this instance has pages.
104
     *
105
     * @return bool Returns true in case of success, false otherwise.
106
     */
107
    public function hasPages() {
108
        return 1 <= $this->getNumberPages();
109
    }
110
111
    /**
112
     * Index a word.
113
     *
114
     * @param Word $word The word.
115
     * @return Document Returns this document.
116
     */
117
    public function index(Word $word) {
118
119
        if (false === array_key_exists($word->getContent(), $this->index)) {
120
            $this->index[$word->getContent()] = [];
121
        }
122
123
        $this->index[$word->getContent()][] = $word;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Set the index.
130
     *
131
     * @param Word[] $index The index.
132
     * @return Document Returns this document.
133
     */
134
    protected function setIndex($index) {
135
        $this->index = $index;
136
        return $this;
137
    }
138
139
    /**
140
     * Set the pages.
141
     *
142
     * @param Page[] $pages The pages.
143
     * @return Document Returns this document.
144
     */
145
    protected function setPages(array $pages) {
146
        $this->pages = $pages;
147
        return $this;
148
    }
149
}