| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PWWEB\Localisation\Models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Database\Eloquent\Relations\BelongsTo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Database\Eloquent\SoftDeletes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PWWEB\Core\Traits\Migratable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use PWWEB\Localisation\Contracts\Address as AddressContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use PWWEB\Localisation\Exceptions\AddressDoesNotExist; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PWWEB\Localisation\LocalisationRegistrar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * PWWEB\Localisation\Models\Address Model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Standard Address Model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author    Frank Pillukeit <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author    Richard Browne <[email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @copyright 2020 pw-websolutions.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @license   http://www.opensource.org/licenses/mit-license.html  MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @property  \PWWEB\Localisation\Models\SystemLocalisationCountry country | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @property  \PWWEB\Localisation\Models\SystemAddressType type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @property  int country_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @property  int type_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @property  string street | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @property  string street2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @property  string city | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @property  string state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @property  string postcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @property  number lat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @property  number lng | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @property  bool primary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class Address extends Model implements AddressContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     use Migratable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     use SoftDeletes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     const CREATED_AT = 'created_at'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     const UPDATED_AT = 'updated_at'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * The attributes that should be casted to Carbon dates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     protected $dates = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         'deleted_at', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * The attributes that can be filled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public $fillable = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         'country_id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         'type_id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         'street', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         'street2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         'city', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         'state', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         'postcode', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         'lat', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         'lng', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         'primary', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * The attributes that should be casted to native types. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     protected $casts = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         'id' => 'integer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         'country_id' => 'integer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         'type_id' => 'integer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         'street' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         'street2' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         'city' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         'state' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         'postcode' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         'lat' => 'float', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         'lng' => 'float', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         'primary' => 'boolean', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Validation rules. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public static $rules = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         'country_id' => 'required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         'type_id' => 'required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         'primary' => 'required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param array $attributes additional attributes for model initialisation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function __construct(array $attributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         parent::__construct($attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->setTable(config('pwweb.localisation.table_names.addresses')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * Accessor for linked Country model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function country(): BelongsTo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return $this->belongsTo(config('pwweb.localisation.models.country'), 'country_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Accessor for linked Address type model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function type(): BelongsTo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return $this->belongsTo(config('pwweb.localisation.models.address_type'), 'type_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * Find an address by its id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param int $id ID to be used to retrieve the address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @todo Refactor. This needs to go into the repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @throws \PWWEB\Localisation\Exceptions\AddressDoesNotExist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return \Illuminate\Database\Eloquent\Model|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public static function findById(int $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $address = static::getAddresses(['id' => $id])->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         if (null === $address) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             throw AddressDoesNotExist::withId($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return $address; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * Find an address by its type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @param string $type Address type to be used to retrieve the address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @todo Refactor. This needs to go into the repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @throws \PWWEB\Localisation\Exceptions\AddressDoesNotExist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return \Illuminate\Database\Eloquent\Model|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public static function findByType(string $type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $address = static::getAddresses(['type' => $type])->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         if (null === $address) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             throw AddressDoesNotExist::withType($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         return $address; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * Get the current cached addresses. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * @param array $params additional parameters for the database query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * @return Collection collection of addresses | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 185 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |     protected static function getAddresses(array $params = []): Collection | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |         $addresses = app(LocalisationRegistrar::class) | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |             ->setAddressClass(static::class) | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |             ->getAddresses($params); | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |         return $addresses; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 194 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 195 |  |  |  |