Skip to content

Commit

Permalink
Added OPTION_DBF_ALLOW_FIELD_SIZE_255
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaspare Sganga committed Apr 9, 2020
1 parent b165764 commit b908126
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Shapefile/Shapefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ abstract class Shapefile
const OPTION_CPG_ENABLE_FOR_DEFAULT_CHARSET = 'OPTION_CPG_ENABLE_FOR_DEFAULT_CHARSET';
const OPTION_CPG_ENABLE_FOR_DEFAULT_CHARSET_DEFAULT = false;

/**
* Allows a maximum field size of 255 bytes instead of 254 bytes in DBF files.
* ShapefileReader and ShapefileWriter
* @var bool
*/
const OPTION_DBF_ALLOW_FIELD_SIZE_255 = 'OPTION_DBF_ALLOW_FIELD_SIZE_255';
const OPTION_DBF_ALLOW_FIELD_SIZE_255_DEFAULT = false;

/**
* Converts from input charset to UTF-8 all strings read from DBF files.
* ShapefileReader
Expand Down Expand Up @@ -889,6 +897,7 @@ protected function initOptions($options, $custom)
{
// Make sure compulsory options used in this abstract class are defined
$options = array_unique(array_merge($options, [
Shapefile::OPTION_DBF_ALLOW_FIELD_SIZE_255,
Shapefile::OPTION_DBF_FORCE_ALL_CAPS,
Shapefile::OPTION_SUPPRESS_M,
Shapefile::OPTION_SUPPRESS_Z,
Expand Down Expand Up @@ -1103,15 +1112,16 @@ protected function addField($name, $type, $size, $decimals)
}

// Check size
$size = intval($size);
$size = intval($size);
$max_size = $this->getOption(Shapefile::OPTION_DBF_ALLOW_FIELD_SIZE_255) ? 255 : 254;
if (
($size < 1)
|| ($type == Shapefile::DBF_TYPE_CHAR && $size > 254)
|| ($type == Shapefile::DBF_TYPE_CHAR && $size > $max_size)
|| ($type == Shapefile::DBF_TYPE_DATE && $size !== 8)
|| ($type == Shapefile::DBF_TYPE_LOGICAL && $size !== 1)
|| ($type == Shapefile::DBF_TYPE_MEMO && $size !== 10)
|| ($type == Shapefile::DBF_TYPE_NUMERIC && $size > 254)
|| ($type == Shapefile::DBF_TYPE_FLOAT && $size > 254)
|| ($type == Shapefile::DBF_TYPE_NUMERIC && $size > $max_size)
|| ($type == Shapefile::DBF_TYPE_FLOAT && $size > $max_size)
) {
throw new ShapefileException(Shapefile::ERR_DBF_FIELD_SIZE_NOT_VALID, $size);
}
Expand Down
1 change: 1 addition & 0 deletions src/Shapefile/ShapefileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function __construct($files, $options = [])
{
// Options
$this->initOptions([
Shapefile::OPTION_DBF_ALLOW_FIELD_SIZE_255,
Shapefile::OPTION_DBF_CONVERT_TO_UTF8,
Shapefile::OPTION_DBF_FORCE_ALL_CAPS,
Shapefile::OPTION_DBF_IGNORED_FIELDS,
Expand Down
1 change: 1 addition & 0 deletions src/Shapefile/ShapefileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function __construct($files, $options = [])
$this->initOptions([
Shapefile::OPTION_BUFFERED_RECORDS,
Shapefile::OPTION_CPG_ENABLE_FOR_DEFAULT_CHARSET,
Shapefile::OPTION_DBF_ALLOW_FIELD_SIZE_255,
Shapefile::OPTION_DBF_FORCE_ALL_CAPS,
Shapefile::OPTION_DBF_NULL_PADDING_CHAR,
Shapefile::OPTION_DBF_NULLIFY_INVALID_DATES,
Expand Down

0 comments on commit b908126

Please sign in to comment.