regions_with_cities = $citta; $this->file_name = $file_name; } public function create() { return \file_put_contents( $this->file_name, \json_encode( $this->to_encode ) ); } public function withSchema( callable $schema ) { $this->schema = $schema; } public function buildJson(): self { $schema = $this->schema; foreach ( $this->regions_with_cities as $region_key => $region ) { $cities = \explode( '---', $region ); $new_cities_arr = []; foreach ( $cities as $city ){ $pairing = \explode( 'XxX', $city ); $new_cities_arr[] = $schema( $pairing, $region_key ); } $this->to_encode[ $region_key ] = $new_cities_arr; } return $this; } public function buildJsonForLocalities(): self { $schema = $this->schema; foreach ( $this->regions_with_cities as $region_key => $region ) { $cities = \explode( '---', $region ); $new_cities_arr = []; foreach ( $cities as $city ){ $pairing = \explode( 'XxX', $city ); $this->to_encode[ 'localita' ][] = $schema( $pairing, $region_key ); } } return $this; } } $regions_with_city = ottienicitta(); //$file = new CreateFileWIthJsonData( $regions_with_city, TREBIMETEO_PATH . 'blocks/cities.json' ); //$file->withSchema( // static function ( array $pairing, $region_key ): array { // return [ // 'label' => $pairing[1], // 'value' => $pairing[0], // ]; // } //); // //$file->buildJson(); //d( $file->create() ); //d( file_get_contents( TREBIMETEO_PATH . 'blocks/cities.json' ) ); $file = new CreateFileWIthJsonData( $regions_with_city, TREBIMETEO_PATH . 'tests/_data/fixtures/api/localities.json' ); $file->withSchema( static function ( array $pairing, int $region_key ): array { static $regions = []; if ( empty( $regions ) ) { $regions = \json_decode( (string) \file_get_contents( TREBIMETEO_PATH . 'blocks/region.json' ) ); } return [ "id" => (string) $pairing[0], "localita" => \ucfirst( (string) $pairing[1] ), "prov" => \strtoupper( \substr( (string) $pairing[1], 0, 2 ) ), "lat" => 44.4056, "lon" => 8.9463000000000008, "tipo" => "L", "codice_stato" => "IT", "id_settore" => 1073, "canonical_url" => \strtolower( (string) $pairing[1] ), "nazione" => "Italia", "regione" => $regions[ $region_key ]->label, "id_localita_old" => (string) $pairing[0], "prov_estesa" => \ucfirst( (string) $pairing[1] ), "timezone" => "Europe\/Rome", ]; } ); //$file->buildJsonForLocalities(); //d( $file->create() ); //d( file_get_contents( TREBIMETEO_PATH . 'tests/_data/fixtures/api/localities.json' ) );