servo-php / fluidxml

FluidXML, the PHP library for manipulating XML with a concise and fluent API.
BSD 2-Clause "Simplified" License
458 stars 45 forks source link

Error when adding an empty string as a value #13

Closed FoxxMD closed 8 years ago

FoxxMD commented 8 years ago

If the value being added for a node is empty an exception is thrown: Uninitialized string offset: 0

#0 /project/vendor/servo/fluidxml/source/FluidXml/FluidHelper.php(13): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Uninitialized s...', '/project/...', 13, Array)
#1 /project/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php(113): FluidXml\FluidHelper::isAnXmlString('')
#2 /project/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php(89): FluidXml\FluidInsertionHandler->recognizeStringMixed('Telephone', '')
#3 /project/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php(26): FluidXml\FluidInsertionHandler->handleInsertion(Object(DOMElement), 'Telephone', '', Object(Closure), Array)
#4 /project/vendor/servo/fluidxml/source/FluidXml/FluidContext.php(204): FluidXml\FluidInsertionHandler->insertElement(Array, Array, Array, Object(Closure), Object(FluidXml\FluidContext))
#5 /project/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php(277): FluidXml\FluidContext->addChild(Array)
hopfi2k commented 8 years ago

Yup, same problem here.

daniele-orlando commented 8 years ago

Thank you guys for reporting the bug. I'll release a fix soon in the next maintenance release.

golddragon007 commented 8 years ago

same problem, when you want to add NULL as value. i.e. ...->add(['tag' => ['@name' => 'dog', 'desc' => NULL]]); Exception: Input type not supported. In function: FluidXml\FluidInsertionHandler->handleInsertion()

JackWH commented 8 years ago

Using NULL as a value seems to work fine for me, but I am also having the same issue with blank strings. A fix would be much appreciated!

JackWH commented 8 years ago

@daniele-orlando Do you know when you estimate the next maintenance release will be? I've just committed to using fluidxml on a project, and wondering if I should patch it myself in the mean time or just wait a few days. Thanks!

golddragon007 commented 8 years ago

For me the empty string '' works fine... LOL. but I fixed myself the NULL problem, by changing the if (\is_string($v)) { line (this is the line 112) to if (\is_string($v) || $v == NULL) { in the protected function recognizeStringMixed($k, $v) function in FluidInsertionHandler.php file. I don't know what else I break with this, but now it's working for me with NULL and '' too on PHP 5.6... Also I can get some funny errors when I build wrongly the array which I use to convert into XML. (I using array at most cases to build XML)

daniele-orlando commented 8 years ago

Hi guys. I pushed a maintenance release which includes a fix for the issue with empty-strings/null-values and more tests to prevent this from happening again. Please check it out and reopen the issue if you still have problems.

Happy coding!

golddragon007 commented 8 years ago

I found something, I don't know this issue cause it or not... DOMException: Invalid Character Error in function DOMElement->__construct() (fluidxml/source/FluidXml/FluidInsertionHandler.php 202 row).

Code which cause it:

    $arr = array(
      'data1'   => 'SEAT',
      'data2'   => '040',
      'data3'   => 'A00',
      'data4'   => '1',
      'data5'   => '',
      'data6'   => NULL,
      'data7'   => '1020',
      'data8'   => '2016-07-12 15:00',
      'data9'   => 12,
      'data10'  => 1,
      'data11'  => 'N',
      'data12'  => 'C',
      'data13'  => 0,
      'data14'  => 0,
      'data15'  => 0,
      'data16'  => 0,
      'data17'  => 0,
      'data18'  => 1197,
      'data19 ' => 77,
      'data20'  => '1212',
      'data21'  => '01',
      'data22'  => 'F',
      'data23'  => '1986-01-01',
      'data24'  => 1,
      'data25'  => '35101',
      'data26'  => '2016-07-12',
      'data27'  => '2010',
      'data28'  => 1,
      'data29'  => 0,
      'data30'  => 0,
      'data31'  => 0,
      'data32'  => 0,
      'data33'  => 0,
      'data34'  => 0,
      'data35'  => 0,
      'data36'  => 'N',
      'data37'  => 'N',
      'data38'  => 'N',
      'data39'  => 'N',
      'data40'  => 'N',
      'data41'  => 'N',
      'data42'  => 'N',
      'data43'  => 0,
      'data44'  => 'N',
      'data45'  => 'N',
      'data46'  => 0,
      'data47'  => 0,
      'data48'  => 0,
      'data49'  => NULL,
      'data50'  => 0,
      'data51'  => 0,
      'data52'  => 0,
      'data53'  => 0,
      'data54'  => NULL,
      'data55'  => NULL,
      'data56'  => NULL,
      'data57'  => NULL,
      'data58'  => NULL,
      'data59'  => NULL,
      'data60'  => 1,
      'data61'  => 'B',
      'data62'  => '2015-01-01',
      'data63'  => 2013,
      'data64'  => 'I',
    );
    $fxml = new FluidXml('d');
    $fxml->add(["data" => $arr]);

I can't reopen this issue because the collaborator closed it...

FoxxMD commented 8 years ago

@golddragon007 can you iterate through that array and do ->add() one element at a time to figure out which one is causing the problem?

golddragon007 commented 8 years ago

I don't iterate through on it, but I put a logger before the 202 row, and it says the last item which was tried is: 'data19 ' => 77, So the variables:

$name = 'data19';
$value = 77;
$uri = NULL;

Than comes the $el = new \DOMElement($name, $value, $uri); line, which falls. I can't understand whats the problem with this data.

NOTE:

Maybe this is the black magic.

golddragon007 commented 8 years ago

Oh crap, there was a space after the key....

daniele-orlando commented 8 years ago

Yes, you have a space in the tag name.

golddragon007 commented 8 years ago

If you do this, because you are sooo lazy and the 'something' condition isn't true, it will fall processing at the array place:

if (something) {
  $array = [
    "key1" => "value1",
    "key2" => "value2",
    "key3" => "value3",
  ]
}

$fxml = new FluidXml('root');
$fxml->add([
  "keys1" => "value1",
  "keys2" => "value2",
  "keys3" => "value3",
  $array,
  "keys4" => "value4",
  "keys5" => "value5",
]);

In this case the $array is NULL and you will get "Input type not supported." error. But if the $array = [] it will work (it's ignores). Possible to handle the 0 => NULL and 0 => [] types/values or whatever is it as same?