Files
cake-addresses/src/Model/Table/AddressesTable.php

201 lines
6.5 KiB
PHP
Raw Normal View History

2025-11-18 00:43:34 -08:00
<?php
declare(strict_types=1);
namespace CakeAddresses\Model\Table;
use ArrayObject;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\ResultSetInterface;
2025-11-18 00:43:34 -08:00
use Cake\Event\EventInterface;
use Cake\Log\Log;
use Cake\ORM\Association\BelongsTo;
2025-11-18 00:43:34 -08:00
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use CakeAddresses\Model\Entity\Address;
2025-11-18 00:43:34 -08:00
use CakeAddresses\Model\Entity\State;
use Closure;
use Psr\SimpleCache\CacheInterface;
2025-11-18 00:43:34 -08:00
/**
* Addresses Model
*
* @property CitiesTable&BelongsTo $Cities
* @property StatesTable&BelongsTo $States
* @property CountriesTable&BelongsTo $Countries
2025-11-18 00:43:34 -08:00
*
* @method Address newEmptyEntity()
* @method Address newEntity(array $data, array $options = [])
* @method array<Address> newEntities(array $data, array $options = [])
* @method Address get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
* @method Address findOrCreate($search, ?callable $callback = null, array $options = [])
* @method Address patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method array<Address> patchEntities(iterable $entities, array $data, array $options = [])
* @method Address|false save(EntityInterface $entity, array $options = [])
* @method Address saveOrFail(EntityInterface $entity, array $options = [])
* @method iterable<Address>|ResultSetInterface<Address>|false saveMany(iterable $entities, array $options = [])
* @method iterable<Address>|ResultSetInterface<Address> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<Address>|ResultSetInterface<Address>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<Address>|ResultSetInterface<Address> deleteManyOrFail(iterable $entities, array $options = [])
2025-11-18 00:43:34 -08:00
*/
class AddressesTable extends Table
{
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('addresses');
$this->setDisplayField('address_line1');
$this->setPrimaryKey('id');
$this->belongsTo('Cities', [
'foreignKey' => 'city_id',
'className' => 'CakeAddresses.Cities',
'propertyName' => 'city_entity',
]);
$this->belongsTo('States', [
'foreignKey' => 'state_id',
'className' => 'CakeAddresses.States',
'propertyName' => 'state_entity',
]);
$this->belongsTo('Countries', [
'foreignKey' => 'country_id',
'className' => 'CakeAddresses.Countries',
'propertyName' => 'country_entity',
]);
}
/**
* Default validation rules.
*
* @param Validator $validator Validator instance.
* @return Validator
2025-11-18 00:43:34 -08:00
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->scalar('address_name')
->maxLength('address_name', 255)
->allowEmptyString('address_name');
$validator
->scalar('contact_name')
->maxLength('contact_name', 255)
->allowEmptyString('contact_name');
$validator
->scalar('address_line1')
->maxLength('address_line1', 255)
->requirePresence('address_line1', 'create')
->notEmptyString('address_line1');
$validator
->scalar('address_line2')
->maxLength('address_line2', 255)
->allowEmptyString('address_line2');
$validator
->scalar('city')
->maxLength('city', 255)
->requirePresence('city', 'create')
->notEmptyString('city');
$validator
->integer('city_id')
->allowEmptyString('city_id');
$validator
->scalar('state')
->maxLength('state', 255)
->requirePresence('state', 'create')
->notEmptyString('state');
$validator
->integer('state_id')
->allowEmptyString('state_id');
$validator
->scalar('postal_code')
->maxLength('postal_code', 25)
->requirePresence('postal_code', 'create')
->notEmptyString('postal_code');
$validator
->scalar('country')
->maxLength('country', 255)
->requirePresence('country', 'create')
->notEmptyString('country');
$validator
->integer('country_id')
->allowEmptyString('country_id');
$validator
->scalar('phone_number')
->maxLength('phone_number', 25)
->allowEmptyString('phone_number');
$validator
->email('email')
->allowEmptyString('email');
$validator
->scalar('notes')
->allowEmptyString('notes');
$validator
->scalar('foreign_key')
->maxLength('foreign_key', 45)
->allowEmptyString('foreign_key');
$validator
->scalar('model')
->maxLength('model', 255)
->allowEmptyString('model');
2025-11-18 00:43:34 -08:00
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param RulesChecker $rules The rules object to be modified.
* @return RulesChecker
2025-11-18 00:43:34 -08:00
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn('city_id', 'Cities'), ['errorField' => 'city_id']);
$rules->add($rules->existsIn('state_id', 'States'), ['errorField' => 'state_id']);
$rules->add($rules->existsIn('country_id', 'Countries'), ['errorField' => 'country_id']);
return $rules;
}
/**
* @param EventInterface $event
* @param ArrayObject $data
* @param ArrayObject $options
*
* @return void
*/
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options): void
{
if ($data['state_id'] && !isset($data['state'])) {
$state = $this->States->find()->where(['id' => $data['state_id']])->first();
$data['state'] = $state->name ?? null;
2025-11-18 00:43:34 -08:00
}
if ($data['country_id'] && !isset($data['country'])) {
$country = $this->Countries->find()->where(['id' => $data['country_id']])->first();
$data['country'] = $country->name ?? null;
2025-11-18 00:43:34 -08:00
}
}
}