Completed
Push — master ( cf24ba...2baed6 )
by WEBEWEB
02:24
created

DocumentHelper::setDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Helper;
13
14
use WBW\Library\Core\Geometry\Point;
15
use WBW\Library\Core\ThirdParty\OcrLad\Model\Document;
16
use WBW\Library\Core\ThirdParty\OcrLad\Model\Page;
17
use WBW\Library\Core\ThirdParty\OcrLad\Model\Word;
18
19
/**
20
 * Document helper.
21
 *
22
 * @author webeweb <https://github.com/webeweb>
23
 * @package WBW\Library\Core\ThirdParty\OcrLad\Helper
24
 */
25
class DocumentHelper {
26
27
    /**
28
     * Document.
29
     *
30
     * @var Document
31
     */
32
    private $document;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param Document $document The document.
38
     */
39
    public function __construct(Document $document) {
40
        $this->setDocument($document);
41
    }
42
43
    /**
44
     * Find a word.
45
     *
46
     * @param Point $position The position.
47
     * @param Page|null $page The page.
48
     * @return Word Returns the word in case of success, null otherwise.
49
     */
50
    public function findWordAt(Point $position, Page $page = null) {
51
52
        foreach ($this->getWords($page) as $current) {
53
54
            $dX = $position->getXInt() === $current->getPoint1()->getXInt();
55
            $dY = $position->getYInt() === $current->getPoint1()->getYInt();
56
57
            if (true === $dX && true === $dY) {
58
                return $current;
59
            }
60
        }
61
62
        return null;
63
    }
64
65
    /**
66
     * Find the words.
67
     *
68
     * @param Point $start The start position.
69
     * @param Point $end The end position.
70
     * @param Page|null $page The page.
71
     * @return Word[] Returns the words.
72
     */
73
    public function findWordsIn(Point $start, Point $end, Page $page = null) {
74
75
        $matches = [];
76
77
        foreach ($this->getWords($page) as $current) {
78
79
            $dX1 = $start->getXInt() <= $current->getPoint1()->getXInt();
80
            $dY1 = $start->getYInt() === $current->getPoint1()->getYInt();
81
82
            $dX2 = $current->getPoint2()->getXInt() < $end->getXInt();
83
            $dY2 = $current->getPoint1()->getYInt() <= $end->getYInt();
84
85
            if (true === $dX1 && true === $dY1 && true === $dX2 && true === $dY2) {
86
                $matches[] = $current;
87
            }
88
        }
89
90
        return $matches;
91
    }
92
93
    /**
94
     * Find the words.
95
     *
96
     * @param Point $start The start position.
97
     * @param Page|null $page The page.
98
     * @return Word[] Returns the words.
99
     */
100
    public function findWordsStartAt(Point $start, Page $page = null) {
101
102
        $matches = [];
103
104
        foreach ($this->getWords($page) as $current) {
105
106
            $dX = $start->getXInt() <= $current->getPoint1()->getXInt();
107
            $dY = $start->getYInt() === $current->getPoint1()->getYInt();
108
109
            if (true === $dX && true === $dY) {
110
                $matches[] = $current;
111
            }
112
        }
113
114
        return $matches;
115
    }
116
117
    /**
118
     * Get the document.
119
     *
120
     * @return Document Returns the document.
121
     */
122
    public function getDocument() {
123
        return $this->document;
124
    }
125
126
    /**
127
     * Get the words.
128
     *
129
     * @param Page|null $page The page.
130
     * @return Word[] Returns the words.
131
     */
132
    protected function getWords(Page $page = null) {
133
134
        if (null !== $page) {
135
            return $page->getWords();
136
        }
137
138
        return $this->getDocument()->getWords();
139
    }
140
141
    /**
142
     * Set the document.
143
     *
144
     * @param Document $document The document.
145
     * @return DocumentHelper Returns this document helper.
146
     */
147
    protected function setDocument($document) {
148
        $this->document = $document;
149
        return $this;
150
    }
151
}