Completed
Push — master ( 4eaf4e...392da2 )
by WEBEWEB
02:39
created

AbstractChartAccountsModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 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\ChartAccounts\Model;
13
14
use WBW\Library\Core\Exception\ChartAccounts\AccountAlreadyExistsException;
15
16
/**
17
 * Abstract chart of accounts model.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\ChartAccounts\Model
21
 * @abstract
22
 */
23
abstract class AbstractChartAccountsModel {
24
25
    /**
26
     * Accounts.
27
     *
28
     * @var ChartAccountsAccount[]
29
     */
30
    private $accounts;
31
32
    /**
33
     * Label.
34
     *
35
     * @var string
36
     */
37
    private $label;
38
39
    /**
40
     * Number
41
     *
42
     * @var string
43
     */
44
    private $number;
45
46
    /**
47
     * Constructor.
48
     */
49
    protected function __construct() {
50
        $this->setAccounts([]);
51
    }
52
53
    /**
54
     * Add an account.
55
     *
56
     * @param ChartAccountsAccount $account The account.
57
     * @return AbstractChartAccountsModel Returns this charts of accounts model.
58
     * @throws AccountAlreadyExistsException Throws an account already exists.
59
     */
60
    public function addAccount(ChartAccountsAccount $account) {
61
        foreach ($this->accounts as $current) {
62
            if ($current->getNumber() === $account->getNumber()) {
63
                throw new AccountAlreadyExistsException($account);
64
            }
65
        }
66
        $this->accounts[] = $account;
67
        return $this;
68
    }
69
70
    /**
71
     * Get the accounts.
72
     *
73
     * @return ChartAccountsAccount[] Returns the accounts.
74
     */
75
    public function getAccounts() {
76
        return $this->accounts;
77
    }
78
79
    /**
80
     * Get the label.
81
     *
82
     * @return string Returns the label.
83
     */
84
    public function getLabel() {
85
        return $this->label;
86
    }
87
88
    /**
89
     * Get the number.
90
     *
91
     * @return string Returns the number.
92
     */
93
    public function getNumber() {
94
        return $this->number;
95
    }
96
97
    /**
98
     * Remove an account.
99
     *
100
     * @param ChartAccountsAccount $account The account.
101
     * @return AbstractChartAccountsModel Returns this charts of accounts model.
102
     */
103
    public function removeAccount(ChartAccountsAccount $account) {
104
        for ($i = count($this->accounts) - 1; 0 <= $i; --$i) {
105
            if ($account->getNumber() !== $this->accounts[$i]->getNumber()) {
106
                continue;
107
            }
108
            unset($this->accounts[$i]);
109
        }
110
        return $this;
111
    }
112
113
    /**
114
     * Set the accounts.
115
     *
116
     * @param ChartAccountsAccount[] $accounts The ccounts.
117
     * @return AbstractChartAccountsModel Returns this charts of accounts model.
118
     */
119
    protected function setAccounts(array $accounts) {
120
        $this->accounts = $accounts;
121
        return $this;
122
    }
123
124
    /**
125
     * Set the label.
126
     *
127
     * @param string $label The label.
128
     * @return AbstractChartAccountsModel Returns this charts of accounts model.
129
     */
130
    public function setLabel($label) {
131
        $this->label = $label;
132
        return $this;
133
    }
134
135
    /**
136
     * Set the number.
137
     *
138
     * @param string $number
139
     * @return AbstractChartAccountsModel Returns this charts of accounts model.
140
     */
141
    public function setNumber($number) {
142
        $this->number = $number;
143
        return $this;
144
    }
145
146
}
147