KnpPaginatorBundle return currentPageNumber as integer

64 Views Asked by At

I am using KnpPaginatorBundle in my Symfony2 project. Everytime I am requesting certain page of results, the result is returning to me looks like:

{
  "currentPageNumber": "2",
  "numItemsPerPage": 5,
  "items": [ ...
   ]
}

As you can see currentPageNumber is string here. How can I change type of this property to integer?

1

There are 1 best solutions below

0
On

You can use standarttype casting in php (documentation)

For sample you can do it like this:

$intValue = (int) "123";

And look at this case:

$stringValue = "123";
echo gettype($stringValue); // will be 'string' here

$intFromStringValue = (int) $stringValue;
echo gettype($intFromStringValue); // will be 'integer' here

// but be aware of this
echo (int) "Some words"; // will be integer but value will be 0, because type casting cannot get the number from string
echo (int) "200 and some words"; // integer 200
echo (int) "Some words and 300"; // integer 0