1: <?php
2:
3: namespace malkusch\bav;
4:
5: /**
6: * Facade for BAV's API.
7: *
8: * This class provides methods for validation of German bank accounts.
9: *
10: * If you don't inject a {@link Configuration} the facade will use the
11: * {@link ConfigurationRegistry}. The registry provides per default the
12: * {@link DefaultConfiguration}.
13: *
14: * @author Markus Malkusch <markus@malkusch.de>
15: * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
16: * @license GPL
17: * @api
18: * @see ConfigurationRegistry
19: */
20: class BAV
21: {
22:
23: /**
24: * @var Configuration
25: */
26: private $configuration;
27:
28: /**
29: * @var DataBackend
30: */
31: private $backend;
32:
33: /**
34: * @var ContextValidation
35: */
36: private $contextValidation;
37:
38: /**
39: * Inject the configuration.
40: *
41: * If the $configuration is null the configuration from
42: * {@link ConfigurationRegistry::getConfiguration()} will be used.
43: *
44: * @see ConfigurationRegistry
45: */
46: public function __construct(Configuration $configuration = null)
47: {
48: if (is_null($configuration)) {
49: $configuration = ConfigurationRegistry::getConfiguration();
50:
51: }
52: $this->configuration = $configuration;
53:
54: $this->backend = $configuration->getDataBackendContainer()->getDataBackend();
55:
56: $this->contextValidation = new ContextValidation($this->backend);
57: }
58:
59: /**
60: * Returns the data backend
61: *
62: * @return DataBackend
63: */
64: public function getDataBackend()
65: {
66: return $this->backend;
67: }
68:
69: /**
70: * Updates bav with a new bundesbank file.
71: *
72: * You might consider enabling automatic update with setting
73: * AutomaticUpdatePlan as configuration.
74: *
75: * @see AutomaticUpdatePlan
76: * @see Configuration::setUpdatePlan()
77: * @throws DataBackendException
78: */
79: public function update()
80: {
81: $this->getDataBackend()->update();
82: }
83:
84: /**
85: * Returns true if both the bank exists and the account is valid.
86: *
87: * @throws DataBackendException for some reason the validator might not be implemented
88: * @param string $bankID
89: * @param string $account
90: * @see isValidBank()
91: * @see getBank()
92: * @see Bank::isValid()
93: * @return bool
94: */
95: public function isValidBankAccount($bankID, $account)
96: {
97: try {
98: $bank = $this->getBank($bankID);
99: return $bank->isValid($account);
100:
101: } catch (BankNotFoundException $e) {
102: return false;
103:
104: }
105: }
106:
107: /**
108: * Returns true if the account is valid for the current context.
109: *
110: * You have to have called isValidBank() before! If the current context
111: * is no valid bank every account will validate to true.
112: *
113: * @param string $account
114: * @see isValidBank()
115: * @see ContextValidation::isValidAccount()
116: * @throws InvalidContextException isValidBank() was not called before.
117: * @return bool
118: */
119: public function isValidAccount($account)
120: {
121: return $this->contextValidation->isValidAccount($account);
122: }
123:
124: /**
125: * Returns true if a bank exists
126: *
127: * @throws DataBackendException
128: * @param string $bankID
129: * @return bool
130: * @see ContextValidation::isValidBank()
131: */
132: public function isValidBank($bankID)
133: {
134: return $this->contextValidation->isValidBank($bankID);
135: }
136:
137: /**
138: * Every bank has one main agency.
139: *
140: * This agency is not included in getAgencies().
141: *
142: * @throws DataBackendException
143: * @throws BankNotFoundException
144: * @param string $bankID Bank id (Bankleitzahl)
145: * @see Bank::getMainAgency()
146: * @see getAgencies()
147: * @return Agency
148: */
149: public function getMainAgency($bankID)
150: {
151: return $this->getBank($bankID)->getMainAgency();
152: }
153:
154: /**
155: * A bank may have more agencies.
156: *
157: * The main agency is not included in this list.
158: *
159: * @param string $bankID Bank id (Bankleitzahl)
160: * @throws DataBackendException
161: * @throws BankNotFoundException
162: * @return Agency[]
163: */
164: public function getAgencies($bankID)
165: {
166: return $this->getBank($bankID)->getAgencies();
167: }
168:
169: /**
170: * With this method you get the Bank objects for certain IDs. Note
171: * that a call to this method with an identical id will return the same
172: * objects.
173: *
174: * @throws BankNotFoundException
175: * @throws DataBackendException
176: * @param string $bankID
177: * @return Bank
178: * @see DataBackend::isValidBank()
179: */
180: public function getBank($bankID)
181: {
182: return $this->backend->getBank($bankID);
183: }
184:
185: /**
186: * Returns bank agencies for a given BIC.
187: *
188: * @param string $bic BIC
189: * @return Agency[]
190: */
191: public function getBICAgencies($bic)
192: {
193: return $this->backend->getBICAgencies(BICUtil::normalize($bic));
194: }
195:
196: /**
197: * Returns if a bic is valid.
198: *
199: * @param string $bic BIC
200: * @return bool
201: */
202: public function isValidBIC($bic)
203: {
204: return $this->backend->isValidBIC(BICUtil::normalize($bic));
205: }
206:
207: /**
208: * Returns the third call back parameter for filter_var() for validating
209: * a bank.
210: *
211: * filter_var($bankID, FILTER_CALLBACK, $bav->getValidBankFilterCallback());
212: *
213: * @return array
214: * @see isValidBank()
215: * @see filter_var()
216: */
217: public function getValidBankFilterCallback()
218: {
219: return $this->contextValidation->getValidBankFilterCallback();
220: }
221:
222: /**
223: * Returns the third call back parameter for filter_var() for validating
224: * a bank account.
225: *
226: * filter_var($account, FILTER_CALLBACK, $bav->getValidBankFilterCallback());
227: *
228: * @return array
229: * @see isValidAccount()
230: * @see filter_var()
231: */
232: public function getValidAccountFilterCallback()
233: {
234: return $this->contextValidation->getValidAccountFilterCallback();
235: }
236: }
237: