Mam pewny przyklad z ksiazki 'PHP5 Zaawansowane programowanie' ktory nie moge rozwiazac problemu odnosnie sprawdzenia walidacji zmiennych.
plik class.Address.php
CODE
<?php
require_once('class.PropertyObject.php');
class Address extends PropertyObject {
function __construct($addressid) {
$arData = DataManager::getAddressData($addressid);
parent::__construct($arData);
$this->propertyTable['addressid'] = 'adres_id';
$this->propertyTable['id'] = 'adres_id';
$this->propertyTable['entityid'] = 'jednostka_id';
$this->propertyTable['address1'] = 'sadres1';
$this->propertyTable['address2'] = 'sadres2';
$this->propertyTable['city'] = 'smiasto';
$this->propertyTable['zipcode'] = 'skod';
$this->propertyTable['type'] = 'styp';
}
function validate() {
if(strlen($this->zipcode) != 6) {
$this->errors['zipcode'] = 'Nalezy podac poprawny kod pocztowy';
}
if(!$this->address1) {
$this->errors['address1'] = 'Adres to pole wymagane';
}
if(!isset($this->city)) {
$this->errors['city'] = 'Miasto to pole wymagane';
}
if(sizeof($this->errors)) {
return true;
} else {
return false;
}
}
function __toString() {
return " $this->address1
$this->address2
$this->city
$this->zipcode";
}
}
?>
zalaczony plik class.PropertyObject.php tylko obsuguje funkcje magiczne get() i set().
Nastepnie rdzen przykladu znajduje sie tutaj:
CODE
<?php
require_once('class.PropertyObject.php');
require_once('class.PhoneNumber.php');
require_once('class.Address.php');
require_once('class.Email.php');
abstract class Entity extends PropertyObject {
private $_emails;
private $_addresses;
private $_phonenumbers;
public function __construct($entityID) {
$arData = DataManager::getEntityData($entityID);
parent::__construct($arData);
$this->propertyTable['entityid'] = 'jednostka_id';
$this->propertyTable['id'] = 'jednostka_id';
$this->propertyTable['name1'] = 'snazwa1';
$this->propertyTable['name2'] = 'snazwa2';
$this->propertyTable['type'] = 'ctyp';
$this->_emails = DataManager::getEmailObjectsForEntity($entityID);
$this->_addresses = DataManager::getAddressObjectsForEntity($entityID);
$this->_phonenumbers = DataManager::getPhoneNumberObjectsForEntity($entityID);
}
function setID($val) {
throw new Exception('Wartosc pola ID nie moze byc zmieniana');
}
function setEntityID($val) {
$this->setID($val);
}
function phonenumbers($index) {
if(!isset($this->_phonenumbers[$index])) {
throw new Exception('Bledny numer telefonu');
} else {
return $this->_phonenumbers[$index];
}
}
function getNumberOfPhoneNumbers() {
return sizeof($this->_phonenumbers);
}
function addPhoneNumber(PhoneNumber $phone) {
$this->_phonenumbers[] = $phone;
}
function addresses($index) {
if(!isset($this->_addresses[$index])) {
throw new Exception('Bledny adres pocztowy');
} else {
return $this->_addresses[$index];
}
}
function getNumberOfAddresses() {
return sizeof($this->_addresses);
}
function addAddress(Address $address) {
$this->_address[] = $address;
}
function emails($index) {
if(!isset($this->_emails[$index])) {
throw new Exception('Bledny adres email');
} else {
return $this->_emails[$index];
}
}
function getNumberOfEmails() {
return sizeof($this->_emails);
}
function addEmail(Email $email) {
$this->_emails[] = $email;
}
public function validate() {
//tutaj kod odpowiedzialny za walidacje
}
}
?>
i metodzie validate() jest informacja ze musze napisac kod odpowiedzialny za walidacje danych.... to moje pytanie brzmi to po co w pliku class.Address.php zostala przedstawiona funkcja do walidacji danych (chociaz i ona rowniez nie dziala jak np nie ma w bazie danych podanego adresu to nie wyswietla bledu...)
tutaj mam plik ostateczny ktory wyswietla dane:
CODE
<?php
require_once('class.DataManager.php');
function printIn($data) {
print $data . "<br/>\n";
}
$arContacts = DataManager::getAllEntitiesAsObjects();
foreach($arContacts as $objEntity) {
if(get_class($objEntity) == 'Individual') {
print "<h1> Osoba - {$objEntity->__toString()}</h1>";
} else{
print "<h1>Organizacja {$objEntity->__toString()} </h1>";
}
if($objEntity->getNumberOfEmails()) {
print "<h2>Adresy email </h2>";
for($x=0; $x < $objEntity->getNumberOfEmails(); $x++) {
printIn($objEntity->emails($x)->__toString());
}
}
if($objEntity->getNumberOfAddresses()) {
print "<h2>Adresy pocztowe </h2>";
for($x=0; $x < $objEntity->getNumberOfAddresses(); $x++) {
printIn($objEntity->addresses($x)->__toString());
}
}
if($objEntity->getNumberOfPhoneNumbers()) {
print "<h2>Numery telefonu </h2>";
for($x=0; $x < $objEntity->getNumberOfPhoneNumbers(); $x++) {
printIn($objEntity->phonenumbers($x)->__toString());
}
}
print "<hr>\n";
}
foreach($arContacts as $objEntity) {
if(get_class($objEntity) == 'Individual') {
print "<h1> Pracodawca - {$objEntity->__toString()}</h1>";
} else{
print "<h1> Pracownik {$objEntity->__toString()} </h1>";
}
}
?>
Czy moze mi ktos wytlumaczyc jak ta walidacja powinna dzialac... co musze zrobic zeby mi to zadzialalo prawidlowo.
Dzieki wielkie za podpowiedzi.