IpCidrRepository.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Mar 9, 2018)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace MGProvision\Proxmox\v2\repository;
  20. use \MGProvision\Proxmox\v2\ProxmoxApiException;
  21. use \MGProvision\Proxmox\v2\models\IpSet;
  22. use \MGProvision\Proxmox\v2\models\IpCidr;
  23. /**
  24. * Description of IpSetRepository
  25. *
  26. * @author Pawel Kopec <pawelk@modulesgardne.com>
  27. */
  28. class IpCidrRepository extends AbstractRepository
  29. {
  30. protected $path;
  31. public function findByPath($path)
  32. {
  33. if (!preg_match('/ipset/', $path))
  34. {
  35. throw new ProxmoxApiException(sprintf("IpCidr path ('%s') is not valid", $path));
  36. }
  37. $this->path = $path;
  38. return $this;
  39. }
  40. /**
  41. *
  42. * @return IpCidr[]
  43. * @throws ProxmoxApiException
  44. */
  45. public function fetch()
  46. {
  47. if ($this->fetch)
  48. {
  49. return $this->fetch;
  50. }
  51. if (empty($this->path))
  52. {
  53. throw new ProxmoxApiException("IpSet path is empty");
  54. }
  55. $this->fetch = [];
  56. foreach ($this->api()->get($this->path) as $entity)
  57. {
  58. $this->fetch[] = (new IpCidr($this->path . "/" . $entity['cidr']))->setAttributes($entity);
  59. }
  60. return $this->fetch;
  61. }
  62. public function find(IpCidr $ipCidr)
  63. {
  64. foreach ($this->fetch() as $cIpCidr)
  65. {
  66. if ($cIpCidr->getCidr() == $ipCidr->getCidr())
  67. {
  68. return $cIpCidr;
  69. }
  70. }
  71. return null;
  72. }
  73. }